Sending emails twice with MailApp.sendEmail

让人想犯罪 __ 提交于 2019-12-02 04:46:41

问题


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:


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 // ... }



来源:https://stackoverflow.com/questions/58781729/sending-emails-twice-with-mailapp-sendemail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!