我在以下链接中有一个Google电子表格:
我想发送由单元格中的日期自动触发的电子邮件。我已经创建了代码,但是无法正常工作。
电子邮件的正文位于“电子邮件警报”表的单元格A1中。
在“ H&S评论”表中:
Column K = the trigger date
Column I = the email address
Column J = the "name" for the email
Column C = the "Task" for the body of the email
Column G = the "Date" the review is due for the body of the email
Column D = the "Description" for the body of the email
该代码从下面的第339行到367行称为函数sendEmails:
function sendEmails() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName("H&S Reviews").activate();
var lastRow = sheet.getLastRow();
var message = spreadsheet.getSheetByName("Email Alerts").getRange(1,1).getValue();
// I modified below script.
var values = sheet.getRange(4, 1, lastRow - 3, 11).getValues();
var todaysDate = sheet.getRange(1, 11).getValue();
for (var i = 0; i < values.length; i++) {
var [,,task,description,,,date,,emailAddress,firstName,sendDate] = values[i];
var messageBody = message.replace("{name}",firstName).replace("{Task}",task).replace("{Description}",description).replace("{Date}",date);
var subject = "Health & Safety Review Task";
var sheetDate = new Date(sendDate);
Sdate = Utilities.formatDate(todaysDate,"GMT+0200","dd-MM-yyyy");
SsheetDate = Utilities.formatDate(sheetDate,"GMT+0200", "dd-MM-yyyy");
if (Sdate == SsheetDate){
var subject = "Health & Safety Review Task";
emailAddress = "tga.assist@uzabus.co.nz";
MailApp.sendEmail(emailAddress, subject, messageBody);
}
}
}
同样,当我在脚本编辑器中运行代码时,也会收到以下消息:
“方法SpreadsheetApp.Range.getValue被脚本大量使用。 文件:代码行:344 该脚本使用一种被认为很昂贵的方法。每次调用都会生成对远程服务器的耗时调用。这可能会对脚本的执行时间(尤其是大数据)产生严重影响。如果脚本性能不佳,则应考虑使用其他方法,例如Range.getValues()。”
我非常感谢您提供帮助以使其正常工作。
答案 0 :(得分:2)
此修改如何?请认为这只是几个可能的答案之一。
getValue()
用于for循环。由此,我认为这发生了这样的问题。因此,在此修改中,使用getValues()
代替错误消息中提到的getValue()
。function sendEmails() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName("H&S Reviews").activate();
var lastRow = sheet.getLastRow();
var message = spreadsheet.getSheetByName("Email Alerts").getRange(1,1).getValue();
// I modified below script.
var values = sheet.getRange(4, 1, lastRow - 3, 11).getValues();
var todaysDate = sheet.getRange(1, 11).getValue();
for (var i = 0; i < values.length; i++) {
var [,,task,description,,,date,,emailAddress,firstName,sendDate] = values[i];
var messageBody = message.replace("{name}",firstName).replace("{Task}",task).replace("{Description}",description).replace("{Date}",date);
var subject = "Health & Safety Review Task";
var sheetDate = new Date(sendDate);
Sdate = Utilities.formatDate(todaysDate,"GMT+0200","dd-MM-yyyy");
SsheetDate = Utilities.formatDate(sheetDate,"GMT+0200", "dd-MM-yyyy");
if (Sdate == SsheetDate){
var subject = "Health & Safety Review Task";
MailApp.sendEmail(emailAddress, subject, messageBody);
}
}
}
H&S Reviews
的范围,从第4行中检索值。因此,我将其反映到了修改后的脚本中。如果要从第3行中检索值,请将var values = sheet.getRange(4, 1, lastRow - 3, 11).getValues();
修改为var values = sheet.getRange(3, 1, lastRow - 2, 11).getValues();
。如果我误解了您的问题,而这不是您想要的方向,我深表歉意。