Sending emails twice with MailApp.sendEmail

2020-04-12 16:16发布

I have created a google apps script to automate emails once a Google Form is submitted. The script is quite simple:

function AutoConfirmation(e){
  var theName = e.values[1];
  var theEmail = e.values[2];
  var theSubject= e.values[3];
  var myEmail = "myemail@gmail.com";
  var theMessage = e.values[4];
  var subject = "Contact form response – " +  theSubject;
  var message = theMessage;
  MailApp.sendEmail (myEmail, subject,message);
}

However, fo some reason I can't figure out, every time a form is submitted I get two instant emails:

  1. Has the data submitted (all works as expected)
  2. Is empty (e.g. subject is "Contact form response –")

I even started from scratch in a different Google account I have and the same issue happens.

Appreciate any suggestions!

1条回答
霸刀☆藐视天下
2楼-- · 2020-04-12 17:02

It appears the issue is being caused by the internal process that syncs form responses to the spreadsheet. Under some circumstances it makes slight updates to the "Timestamp" column of previously submitted form responses, which cause the onFormSubmit triggers to fire again for those rows (albeit with incomplete event objects).

The engineering team is still working on a fix, but in the mean time you can work around this issue by filtering out form submit events that only affect the timestamp column. Since you can reorder the columns in a Form Responses sheet, the best way would be to check if the event's range only covers a single column:

function onFormSubmit() { if (e.range.columnStart == e.range.columnEnd) return;

// The rest of your code // ... }

查看更多
登录 后发表回答