我正试图让这个代码更快地发送电子邮件,而不是挂断这么多。花在时间结束脚本。我怎样才能更快地发送和结束邮件?这样,如果我需要发送更多的邮件,它可以再次运行整个脚本。
function sendEmails() {
//Current spreadsheet emails are sent from
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Search Index").activate();
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lr = ss.getLastRow();
var RowCountEmailPresent = 0
for (var i = 5;i<=lr;i++) {
var currentEmail = ss.getRange(i, 2).getValue();
if (currentEmail.includes('@')) {
RowCountEmailPresent = RowCountEmailPresent + 1
}
}
var templateText = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Template").getRange(1, 1).getValue();
//How Many Sends We have left
var quotaLeft = MailApp.getRemainingDailyQuota();
if((RowCountEmailPresent) > quotaLeft){
Browser.msgBox("You have " + quotaLeft + "left and you're trying to send " + (lr-1) + " emails. Emails were not sent.");
}
else {
for (var i = 5;i<=lr;i++) {
//What values are being placed in the email
var currentEmail = ss.getRange(i, 2).getValue();
var ccmail = ss.getRange(i, 1).getValue();
var AlreadySent = ss.getRange(i, 3).getValue();
var currentStatus = ss.getRange(i, 7).getValue();
var currentOrdernumber = ss.getRange(i, 6).getValue();
var currentMon = ss.getRange(i, 6).getValue();
var code = ss.getRange(i, 13).getValue();
//Actual email being sent to Reps and TLs
var messageBody = templateText.replace("{Order Number}", currentMon).replace("{Jep Code}", code);
var subjectLine = "Offline Support - Order Status: " + currentStatus + " - Mon#: " + currentOrdernumber;
if (currentEmail.includes('@')) {
if (AlreadySent < 1){
MailApp.sendEmail(currentEmail, subjectLine, messageBody, {cc: ccmail})
ss.getRange(i, 3).setValue('1');
}
}
} // close for loop
} //close else statment
SpreadsheetApp.getUi().alert("Congratulations, your email has been sent", SpreadsheetApp.getUi().ButtonSet.OK);
}
看看这是否能提高你的效率。
var AlreadySent = ss.getRange(i, 3).getValue();
if (AlreadySent < 1){
var currentEmail = ss.getRange(i, 2).getValue();
if (currentEmail.includes('@')) {
var ccmail = ss.getRange(i, 1).getValue();
var currentStatus = ss.getRange(i, 7).getValue();
var currentOrdernumber = ss.getRange(i, 6).getValue();
var currentMon = ss.getRange(i, 6).getValue();
var code = ss.getRange(i, 13).getValue();
var messageBody = templateText.replace("{Order Number}", currentMon).replace("{Jep Code}", code);
var subjectLine = "Offline Support - Order Status: " + currentStatus + " - Mon#: " + currentOrdernumber;
MailApp.sendEmail(currentEmail, subjectLine, messageBody, {cc: ccmail})
ss.getRange(i, 3).setValue('1');
}
}