Duplicate emails sent from Apps Script project that has a weekly time-based trigger

后端 未结 1 1812
闹比i
闹比i 2020-12-20 08:57

The below script was correctly sending one weekly email to each person who met the conditions specified in the script. The trigger is time-based, set to run weekly on Monday

相关标签:
1条回答
  • 2020-12-20 09:22

    This seems to be a bug

    Untl it's fixed, as a workaround:

    • Use Script Properties to save the last execution time
    • Implement an if statement at the beginning of your code that executes the rest of the code only if the last execution time (retrieved from the script properties) is not less than one week ago

    Sample:

    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);
      }
    }
    
    0 讨论(0)
提交回复
热议问题