我觉得我错过了Spreadsheet对象脚本。
我正在尝试自动发送电子邮件给onEdit。我在测试中显式运行脚本时成功通过电子邮件发送,但onEdit事件似乎永远不会被触发(甚至看不到日志消息)。脚本看起来非常简单。
function onEdit(e) {
var sheet = e.source;
var viewers = sheet.getViewers();
var ct = viewers.length;
var recipients = [];
for(var i=0;i<ct;i++){
recipients.push(viewers[i].getEmail());
};
var subject = 'Update to '+sheet.getName();
var body = sheet.getName() + ' has been updated. Visit ' + sheet.getUrl() + ' to view the changes ' + e.range;
Logger.log('Running onedit');
MailApp.sendEmail(recipients, subject, body);
};
答案 0 :(得分:5)
简单的onEdit函数有一组非常有限的可能操作,因为它在没有潜在用户授权的情况下运行。您必须创建另一个函数并在此函数上设置特定的触发器。(可安装的触发器)
以this post为例。它显示了简单的onEdit和可安装编辑的示例(用于电子邮件发送) 这在the documentation here中解释。
编辑:这是您的代码:
function sendAlert() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var cell = ss.getActiveCell().getA1Notation()
var viewers = ss.getViewers();
var ct = viewers.length;
var recipients = [];
for(var i=0;i<ct;i++){
recipients.push(viewers[i].getEmail());
};
var subject = 'Update to '+sheet.getName();
var body = sheet.getName() + ' has been updated. Visit ' + ss.getUrl() + ' to view the changes on cell ' + cell;
MailApp.sendEmail(recipients, subject, body);
};