【问题标题】:Duplicate emails sent from Apps Script project that has a weekly time-based trigger从具有每周基于时间的触发器的 Apps 脚本项目发送的重复电子邮件
【发布时间】:2020-06-08 13:33:26
【问题描述】:

以下脚本正确地每周向满足脚本中指定条件的每个人发送一封电子邮件。触发器是基于时间的,设置为每周一上午运行。今天早上,脚本运行了 4 次,同一个人收到了 4 次相同的电子邮件。

我唯一能想到的是上周我将工作表的快捷方式放入共享文件夹中。该文件夹有 5 个人可以访问其中的任何内容 - 我和其他 4 个人。我 100% 确定没有其他人打开工作表或脚本,更不用说授权它运行或创建另一个触发器了。

为什么会发生这种情况,我可以做些什么来解决它?非常感谢任何帮助!

根据 Stackdriver 日志运行 4 次 - “我的执行”区域

只配置了 1 个触发器

完整脚本如下

function sendEmailLoop() {

  var sheets =   SpreadsheetApp.getActiveSpreadsheet().getSheets();
  sheets.forEach(function(sheet) {
    var range = sheet.getDataRange();

    if (sheet.getName() == "Summary") //Disregard tab named 'Summary' 
    {      
    }

    else {    
      var range = sheet.getDataRange(); //to set the range as array
      var values = range.getDisplayValues(); //to get the value in the array
      var lastRow = range.getLastRow();
      var ss = SpreadsheetApp.getActiveSpreadsheet();  //declare the spreadsheet
      var sheet = ss.getSheetByName("Sheet1");
      var message = "";
      var i;
      var logContent = '';


      for (i = 3; i < lastRow; i++) { 
        if (values[i][8] == 'TRUE') {

      var EmpName = values[i][0];        //[Name] cell A++
      var EmpEmail = values[i][1];       // [Email] cell B++
      var SupName = values[i][2];       //[Supervisor Name] cell C++
      var SupEmail = values[i][3];      //[Supervisor Email] cell D++
      var LastComplete = values[i][4];  //[Last Completed Date] cell E++
      var DueDate = values[i][5];       //[Due date] cell F++
      var Title = values[0][0];         //[Title] cell A1
      var URL = values[0][1];         //[URL] cell B1
      var CertTo = values[1][1];      // [Certificate goes to] cell B2
      var curDate = values[0][4];
      console.log(EmpEmail);

        Logger.log('to: ' + EmpEmail);
        Logger.log('subject: ' + EmpName + Title + 'Test');
        Logger.log('message: ' + 'This is a test message for the training that can be found at ' + URL);


        if (EmpEmail == "") {
          continue;
        };

        message = "Dear " + EmpName + ","+
            "<br/><br/>This is a reminder that you must complete the " + Title + " training by " + DueDate + " in order to comply with the annual training requirement. You last completed the course on " +
              LastComplete + ". " + 
                "<p>Please complete the course at <a href=\ " + URL + ">this link</a> prior to the due date. You will continue to receive email reminders until you complete it. Once completed, please email a PDF of your completion certificate to your supervisor and " + CertTo + ".</p>" +
                "<em><br/><br/>**This email is auto-generated. If you already completed this training, please let your supervisor know.**</em>";

        MailApp.sendEmail({
          to: EmpEmail,
          cc: SupEmail,
          subject: 'Annual ' + Title + ' Training Reminder - Due ' + DueDate,
          htmlBody: message});
      }
      }; //end for loop - email tab data
    };   // end 'else'
  }); // end function(sheet)       
} // end SendEmailLoop()  


【问题讨论】:

  • 我只想在每个列中添加一个列,其中包含上次发送电子邮件的日期,如果该日期是今天,则不​​要发送它,您可以在每次发送电子邮件时更新该日期。您必须为有时会发生的虚假触发做好计划
  • 谢谢,Cooper - 这个解决方案也可以。我是初学者,因此需要努力将脚本运行的日期重新填充到 Google 表格中。还不熟悉gets/sets。将开始修补此选项。谢谢!

标签: google-apps-script google-sheets triggers


【解决方案1】:

这似乎是bug

直到它被修复,作为一种解决方法:

  • 使用Script Properties保存上次执行时间
  • 在代码开头实施 if 语句,仅当最后执行时间(从脚本属性中检索)不少于一周前时才执行其余代码

示例:

function sendEmailLoop() {
  if(!PropertiesService.getScriptProperties().getProperty("lastExecution")){
    PropertiesService.getScriptProperties().setProperty("lastExecution", new Date().getTime());
  }
  var lastExecution = PropertiesService.getScriptProperties().getProperty("lastExecution");
  var now = new Date().getTime();
  var oneWeek = 1000*3600*24*7;
  if(now-lastExecution >= oneWeek){
    // paste here the rest of your code
    PropertiesService.getScriptProperties().setProperty("lastExecution", now);
  }
}

【讨论】:

  • 为什么,是的 - 它看起来确实像一个错误。感谢您的帮助和代码示例。这是救命稻草!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-21
  • 2014-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多