根据电子表格数据发送 1 封电子邮件通知

问题描述 投票:0回答:1

我有一些脚本可以运行以查找“E”列中的空单元格,如果找到则发送电子邮件。它运行并工作。问题是,它会为“E”列为空的每一行发送一封单独的电子邮件。如果在该列中找到空单元格,我只需要发送一封电子邮件。

如何实现?

 function sendSupervisorEmail(e) {
   var ss= SpreadsheetApp.getActiveSpreadsheet();
   var sheet = ss.getSheetByName("Form Responses 1");
   var data = sheet.getDataRange().getValues();
   for(var i = 1; i < data.length; i++)
   {
     if(data[i][4] == "")
     {var msg = "There are time off requests that have an open approval status.  \n Click the following link to approve \n '>>>>URL<<<<'";
     MailApp.sendEmail(">>>>EMAIL<<<<", "Open Approval Status - Time Off Requests", msg, {noReply: true} )}
   }

}
google-apps-script
1个回答
0
投票

如果E列有任何空白,您想发送电子邮件。

  • getLastRow
    = 工作表中的行数
  • sheet.getRange("e1:e").getValues().filter(String).length
    = 获取 E 列中的记录数(忽略空白单元格)。
  • 测试两个值是否相等
    • 如果“否”,则发送电子邮件
    • 如果“是”,则什么都不做

function sendSupervisorEmail(e) {
  var ss= SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Form Responses 1");
  var data = sheet.getDataRange().getValues();

  // get the number of records in Column E
  var eRecords = sheet.getRange("e1:e").getValues().filter(String).length;
  // get the last row in the sheet
  var lastRow = sheet.getLastRow()
  // send an email if the last row <> number of records
  if (lastRow != eRecords){
    var msg = "There are time off requests that have an open approval status.  \n Click the following link to approve \n '>>>>URL<<<<'";
    MailApp.sendEmail(">>>>EMAIL<<<<", "Open Approval Status - Time Off Requests", msg, {noReply: true} )
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.