【问题标题】:Send email to recipient based on date根据日期向收件人发送电子邮件
【发布时间】:2018-07-06 14:44:33
【问题描述】:

我目前有这个脚本用于我正在处理的 Google 表格文件:

function myAlerts() { // this runs based on daily trigger
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Reminders");
  var range = sheet.getDataRange();   var values = range.getDisplayValues();
  var lastRow = range.getLastRow();

  var curDate = values[1][5]

  var anyMatches = false;
  var message = "";   var sheetUrl = ss.getUrl();
  var email = Session.getActiveUser().getEmail();
  var optionalEmail = values[2][1]; 
  if (optionalEmail != "") { email = email + "," + optionalEmail; }
  for (var i = 5; i < lastRow; i++) {
    // if today matches the alert date, send an alert
    if (values[i][3].toString() == curDate.toString()) {  
      // add a message for this row if date matches
      message = message + values[i][0] + " will expire on " + values[i][1] + "<br />\n";

      // if there is a match, set anyMatches to true so and email gets sent
      anyMatches = true;
    }
  }  // ends for loop
  // footer for message
  message = message +  "<br />\nThis is an auto-generated email to remind you of your document expiration. <br />\n"
  if (anyMatches)  { // send an email   
      MailApp.sendEmail({
        to: email,
        subject: 'Document Expiration Notice!',
        htmlBody: message});
  }
}

在我的工作表中,如果我在 B3 中输入电子邮件地址,脚本会向我(工作表的所有者)和其他人发送一封电子邮件。该脚本查看 F2,并检查 D 列以查看是否有任何日期匹配。如果他们这样做了,它会在同一行中发送一封包含其他列信息的电子邮件。

如何编辑此脚本,以便它不仅会向我发送电子邮件和 B3 中的电子邮件地址,还会发送同一行中具有匹配日期的电子邮件地址??

【问题讨论】:

    标签: email google-apps-script google-sheets automation


    【解决方案1】:

    只需从第一个 if 语句中删除您的 { email = email + "," + optionalEmail;},并将其移到第二个之后,就像您为 optionalEmail 变量所做的那样添加电子邮件值。

      if (values[i][3].toString() == curDate.toString())    {  
              email = email + "," + optionalEmail + "," + values[i][2];
    

    更新评论:

    我在提出更改建议时没有抓住的有效点。这需要更多的工作。您需要将整个 MailApp.sendEmail 块移动到第二个 if 语句中。然后你需要在每次迭代中“清除”你的变量,这样它们就不会延续到下一次迭代中。为了有效地做到这一点,您需要创建一个新变量来保存电子邮件收件人 (recip)。如果您清除 email 变量,代码将抛出错误,因为您使用填充它的逻辑。

    这是更新后的代码:

    function myAlerts() { // this runs based on daily trigger   
      var ss = SpreadsheetApp.getActiveSpreadsheet();   
      var sheet = ss.getSheetByName("Reminders");
         var range = sheet.getDataRange();   var values = range.getDisplayValues();
         var lastRow = range.getLastRow();
    
      var curDate = values[1][5]
    
     var message = "";   var sheetUrl = ss.getUrl();
     var email = Session.getActiveUser().getEmail();     var optionalEmail = values[2][1];  
        for (var i = 5; i < lastRow; i++)     {      // if today matches the alert date, send an alert
       if (values[i][3].toString() == curDate.toString())    {  
        var recip = email + "," + optionalEmail + "," + values[i][2]; 
             //changed variable to allow for clearing.
     // add a message for this row if date matches
         message = message + values[i][0] + " will expire on " + values[i][1] + "<br />\n";
         MailApp.sendEmail({
          to: recip,
          subject: 'Document Expiration Notice!',
          htmlBody: message});  
      recip = ""; // clears email recipients for next iteration.
      message = "";  // clears message for next iteration.
     }    
    
    } // ends for loop
    
    }  
         // footer for message   message = message +  "<br />\nThis is an auto-generated email to remind you of your document expiration. <br />\n"
    

    我还删除了anyMatches 变量和第一个if 语句,因为它们并不是真正需要的。

    如果它仍然不能满足您的需求,请告诉我。

    【讨论】:

    • 谢谢罗恩,这可以完美地完成我想要的一切。干杯!
    猜你喜欢
    • 2021-07-19
    • 2012-05-18
    • 1970-01-01
    • 2017-12-05
    • 2015-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多