我正在使用Google工作表来跟踪程序详细信息,并根据工作表中的信息发送电子邮件。每个Google工作表标签都可以发送3-5种不同的电子邮件。电子邮件由一个简单的脚本和一个“ onEdit”触发器控制,该触发器查找“ Send!”。在列中选择,然后发送适当的电子邮件。问题是,当快速对工作表进行大量更改时,所有不同的触发器都会触发,并且应用程序脚本会失败,并显示错误消息“服务占用一天的计算机时间过多”或“同时调用太多”。达到这些Google App脚本限制后,它们将需要24小时才能重置。
过去,我添加了具有不同时间间隔的“ Utilities.sleep(2000)”,以避免“过多的同时...”,但这只会浪费更多的运行时间,并使我陷入“过多的计算机时间。 ..”错误。因此,当“发送!”时,如何修改脚本代码或触发器以使其仍能运行?是在电子表格中选择的,但没有其他Google工作表超时?
在此先感谢您的帮助。
这里是电子邮件脚本之一的示例。触发器是“编辑中”类型。
function SendDD01Confirmation() {
Utilities.sleep(1000)
var sheetNameToWatch = "DD";
var columnNumberToWatch = 24; // column A = 1, B = 2, etc.
var valueToWatch = "Send!";
var emailAddressColumn = 5;
var namecolumn = 3;
var datecolumn = 1;
var datewrittencolumn = 6;
var schoolcolumn = 2;
var timescolumn = 7;
var statuscolumn = 25;
var statusValue = "✓";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();
var emailAddress = sheet.getRange(range.getRow(), emailAddressColumn).getValue();
var name = sheet.getRange(range.getRow(), namecolumn).getValue();
var date = sheet.getRange(range.getRow(), datecolumn).getValue();
var datewritten = sheet.getRange(range.getRow(), datewrittencolumn).getValue();
var school = sheet.getRange(range.getRow(), schoolcolumn).getValue();
var times = sheet.getRange(range.getRow(), timescolumn).getValue();
var status = sheet.getRange(range.getRow(),statuscolumn)
var emailSubject = "Program has been scheduled for " +date+ "!";
var body = "Program is coming to your class! You are scheduled to receive a one-hour presentation on " +datewritten+ " at " +school+ ", with the following time(s):\n \n" +times+ "\n (Rest of email removed for brevities sake)";
if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && range.getValue() == valueToWatch) {
GmailApp.sendEmail(emailAddress, emailSubject, body)
status.setValue(statusValue);
}
}
答案 0 :(得分:1)
尝试一下:
您在代码中做了很多您不需要做的事情。一方面,将用于检查正确工作表的代码放在顶部,这样您就不必做一堆无用的声明。其次,像我对getValues()一样,一次获取所有数据,最好不要对数据值使用中间变量,而只使用诸如data [0] [1]等实际数据元素。
function SendDD01Confirmation(e) {
var sh=e.range.getSheet();
var n=sh.getName();
if(n!="DD")return;//This is preferable close to the beginning of the function
var statusValue = "✓";
var data=sheet.getRange(e.range.rowStart,1,1,25).getValues();
var emailAddress = data[0][4];//it would be better to remove these intermediate variables as well but it's a bit more difficult to trouble shoot.
var name = data[0][2];
var date = data[0][0];
var datewritten = data[0][5];
var school = data[0][1];
var times = data[0][6];
var status = data[0][24];
var emailSubject = "Program has been scheduled for " +date+ "!";
var body = "Program is coming to your class! You are scheduled to receive a one-hour presentation on " +datewritten+ " at " +school+ ", with the following time(s):\n \n" +times+ "\n (Rest of email removed for brevities sake)";
if (e.range.columnStart == 24 && e.value == "Send!") {
GmailApp.sendEmail(emailAddress, emailSubject, body);
sh.getRange(e.range.rowStart,25).setValue(statusValue);
}
}
您可以将其缩小为:
function SendDD01Confirmation(e) {
var sh=e.range.getSheet();
var n=sh.getName();
if(n!="DD")return;
var statusValue = "✓";
var data=sheet.getRange(e.range.rowStart,1,1,25).getValues();
var emailAddress = data[0][4];//it would be better to remove these intermediate variables as well but it's a bit more difficult to trouble shoot.
var emailSubject = "Program has been scheduled for " + data[0][0] + "!";
var body = "Program is coming to your class! You are scheduled to receive a one-hour presentation on " + data[0][5] + " at " + data[0][1] + ", with the following time(s):\n \n" + data[0][6] + "\n (Rest of email removed for brevities sake)";
if (e.range.columnStart==24 && e.value=="Send!") {
GmailApp.sendEmail(emailAddress, emailSubject, body);
sh.getRange(e.range.rowStart,25).setValue(statusValue);
}
}