脚本在电子邮件已经发送后运行太久了

发布于 2025-02-13 15:48:07 字数 1999 浏览 0 评论 0原文

我试图让此代码更快地发送电子邮件,而不会挂断电子邮件。花时间结束脚本。如何发送电子邮件并更快地结束电子邮件?这样,如果我需要发送更多电子邮件,它可以再次运行整个脚本。

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);

}

I am trying to have this code send emails faster and not hang up so much. Taking along time to end the script. How can I send the emails and end it quicker? That way if I need to send out more emails, it can run the whole script again.

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);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千鲤 2025-02-20 15:48:08

看看这是否提高了您的效率。

    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');
      }
  }

See If this improves your efficiency.

    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');
      }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文