根据日期发送电子邮件给收件人

时间:2018-07-06 14:44:33

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

我目前正在使用的脚本用于正在处理的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中的电子邮件地址,还可以将同一行中的电子邮件地址发送给我,并具有匹配的日期?

1 个答案:

答案 0 :(得分:0)

只需从第一个{ email = email + "," + optionalEmail;}语句中删除if,然后将其移至第二个语句之后,就像添加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语句,因为实际上并不需要它们。

让我知道它是否仍然无法满足您的需求。