Google Apps 脚本:如何在编辑单元格后发送邮件?

问题描述

我有一个空的 Google 电子表格和以下有效的 Apps 脚本代码

// This function SUCCESSFULLY opens a message Box whenever a cell is edited,it shows the row of the edited cell.
function onEdit(e) {
  browser.msgBox(e.range.getRow());
}

// This function SUCCESSFULLY sends an email to 'john@doe.com' (this email address is changed for an actual registered address) upon being called.
function myTestFunction() {
  MailApp.sendEmail('john@doe.com','Test','Cell changed');
}

但是,如果我将“onEdit”功能更改为以下内容,则无法正常工作:

// This function SUCCESSFULLY opens a message Box whenever a cell is edited,BUT IT DOES NOT SEND AN EMAIL.
function onEdit(e) {
  browser.msgBox(e.range.getRow());
  MailApp.sendEmail('info@bpbl.de','Cell changed');
}

为什么后者的“onEdit”功能不发送电子邮件

(是的,我检查了我的 remaining daily email quota,还有很多。)

解决方法

这个问题已经贴出来了,很遗憾,你不能在简单的触发器 onEdit 中调用 sendEmail 函数:(

Google-apps-script will not send email inside an onEdit function

https://developers.google.com/apps-script/guides/triggers