运行脚本后,如何避免发送重复的电子邮件。下面提到了我创建的脚本。我只想在运行脚本后将属性ONCE发送到电子表格上的电子邮件。
var EMAIL_SENT = 'EMAIL_SENT';
function sendEmails() {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form Responses 1").activate();
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
var lr = ss.getLastRow()
var templateText = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Subject Template").getRange(1, 1).getValue();
var quotaLeft = MailApp.getRemainingDailyQuota();
if ((lr-1) > quotaLeft) {
Browser.msgBox("You have " + quotaLeft + " left and you're trying to send " + (lr-1) + " emails. Emails were not sent.")
} else {
for (var i = 2;i<=lr;i++) {
var currentEmail = ss.getRange(i, 6).getValue();
var currentSubject = ss.getRange(i, 5).getValue()
var currentUser = ss.getRange(i, 2).getValue();
var currentDivision = ss.getRange(i, 3).getValue();
var currentIssue = ss.getRange(i, 5).getValue();
var currentSeveritylevel = ss.getRange(i, 4).getValue();
var emailSent = ss.getRange(i, 9);
var messageBody = templateText.replace("{User}",currentUser).replace("{division}",currentDivision).replace("{Issue}",currentIssue).replace("{Severity level}", currentSeveritylevel);
var subjectLine = "Tech Support Issue: " + currentIssue + " for " + currentUser;
if (emailSent !== EMAIL_SENT) {
MailApp.sendEmail(currentEmail, subjectLine, messageBody);
ss.getRange(i, 9).setValue('EMAIL_SENT');
}
}
}
}
答案 0 :(得分:1)
您需要将getValue()添加到获取emailSent的行中。不是必须的,但是在检查emailSent时,单引号也需要从EMAIL_SENT中删除。
var EMAIL_SENT = 'EMAIL_SENT';
function sendEmails() {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form Responses 1").activate();
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
var lr = ss.getLastRow()
var templateText = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Subject Template").getRange(1, 1).getValue();
var quotaLeft = MailApp.getRemainingDailyQuota();
if ((lr-1) > quotaLeft) {
Browser.msgBox("You have " + quotaLeft + " left and you're trying to send " + (lr-1) + " emails. Emails were not sent.")
} else {
for (var i = 2; I <=lr; i++) {
var currentEmail = ss.getRange(i, 6).getValue();
var currentSubject = ss.getRange(i, 5).getValue()
var currentUser = ss.getRange(i, 2).getValue();
var currentDivision = ss.getRange(i, 3).getValue();
var currentIssue = ss.getRange(i, 5).getValue();
var currentSeveritylevel = ss.getRange(i, 4).getValue();
var emailSent = ss.getRange(i, 9).getValue();
var messageBody = templateText.replace("{User}",currentUser).replace("{division}",currentDivision).replace("{Issue}",currentIssue).replace("{Severity level}", currentSeveritylevel);
var subjectLine = "Tech Support Issue: " + currentIssue + " for " + currentUser;
if (emailSent !== EMAIL_SENT) {
MailApp.sendEmail(currentEmail, subjectLine, messageBody);
ss.getRange(i, 9).setValue(EMAIL_SENT);
}
}
}