使用MailApp.sendEmail发送电子邮件两次 [英] Sending emails twice with MailApp.sendEmail

查看:303
本文介绍了使用MailApp.sendEmail发送电子邮件两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个Google Apps脚本,以在提交Google表单后自动执行电子邮件发送.该脚本非常简单:

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. 是否已提交数据(所有工作均按预期进行)
  2. 为空(例如,主题为联系表单回复–")

我什至从头开始使用另一个我拥有的Google帐户,并且发生了同样的问题.

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

感谢任何建议!

推荐答案

问题似乎是由将表单响应同步到电子表格的内部过程引起的.在某些情况下,它会对先前提交的表单响应的时间戳"列进行些微更新,这将导致针对这些行再次触发onFormSubmit触发器(尽管事件对象不完整).

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(){ 如果(e.range.columnStart == e.range.columnEnd)返回;

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

//其余代码 //... }

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

这篇关于使用MailApp.sendEmail发送电子邮件两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆