以下脚本正确地向符合脚本中指定条件的每个人每周发送一封电子邮件。触发器是基于时间的,设置为每周一早上运行。今天早上,脚本运行了4次,同一个人收到了4次相同的电子邮件。
我唯一能想到的是,上周我将工作表的快捷方式放到了共享文件夹中。该文件夹有5个人可以访问其中的任何内容-我和其他4个人。我100%肯定没有其他人打开工作表或脚本,更不用说授权它运行或创建另一个触发器了。
为什么会发生这种情况,我该怎么解决?非常感谢您的协助!
下面的完整脚本
function sendEmailLoop() {
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
sheets.forEach(function(sheet) {
var range = sheet.getDataRange();
if (sheet.getName() == "Summary") //Disregard tab named 'Summary'
{
}
else {
var range = sheet.getDataRange(); //to set the range as array
var values = range.getDisplayValues(); //to get the value in the array
var lastRow = range.getLastRow();
var ss = SpreadsheetApp.getActiveSpreadsheet(); //declare the spreadsheet
var sheet = ss.getSheetByName("Sheet1");
var message = "";
var i;
var logContent = '';
for (i = 3; i < lastRow; i++) {
if (values[i][8] == 'TRUE') {
var EmpName = values[i][0]; //[Name] cell A++
var EmpEmail = values[i][1]; // [Email] cell B++
var SupName = values[i][2]; //[Supervisor Name] cell C++
var SupEmail = values[i][3]; //[Supervisor Email] cell D++
var LastComplete = values[i][4]; //[Last Completed Date] cell E++
var DueDate = values[i][5]; //[Due date] cell F++
var Title = values[0][0]; //[Title] cell A1
var URL = values[0][1]; //[URL] cell B1
var CertTo = values[1][1]; // [Certificate goes to] cell B2
var curDate = values[0][4];
console.log(EmpEmail);
Logger.log('to: ' + EmpEmail);
Logger.log('subject: ' + EmpName + Title + 'Test');
Logger.log('message: ' + 'This is a test message for the training that can be found at ' + URL);
if (EmpEmail == "") {
continue;
};
message = "Dear " + EmpName + ","+
"<br/><br/>This is a reminder that you must complete the " + Title + " training by " + DueDate + " in order to comply with the annual training requirement. You last completed the course on " +
LastComplete + ". " +
"<p>Please complete the course at <a href=\ " + URL + ">this link</a> prior to the due date. You will continue to receive email reminders until you complete it. Once completed, please email a PDF of your completion certificate to your supervisor and " + CertTo + ".</p>" +
"<em><br/><br/>**This email is auto-generated. If you already completed this training, please let your supervisor know.**</em>";
MailApp.sendEmail({
to: EmpEmail,
cc: SupEmail,
subject: 'Annual ' + Title + ' Training Reminder - Due ' + DueDate,
htmlBody: message});
}
}; //end for loop - email tab data
}; // end 'else'
}); // end function(sheet)
} // end SendEmailLoop()
答案 0 :(得分:1)
作为解决方法,此问题已修复:
if
语句,以执行其余代码。示例:
function sendEmailLoop() {
if(!PropertiesService.getScriptProperties().getProperty("lastExecution")){
PropertiesService.getScriptProperties().setProperty("lastExecution", new Date().getTime());
}
var lastExecution = PropertiesService.getScriptProperties().getProperty("lastExecution");
var now = new Date().getTime();
var oneWeek = 1000*3600*24*7;
if(now-lastExecution >= oneWeek){
// paste here the rest of your code
PropertiesService.getScriptProperties().setProperty("lastExecution", now);
}
}