How to run a script every hour, on the hour only on weekdays using time-based trigger?

后端 未结 1 1622
南旧
南旧 2021-01-16 10:35

I need to run a Google App Script script every hour on the hour only on weekdays.

It seems easy to do one of the two, but to combine it I\'m not sure...

The ho

相关标签:
1条回答
  • 2021-01-16 11:15

    Your best choice, I think, would be setting a Trigger to each hour you want the script to run at, using a second loop inside the one that sets weekdays, since everyHours() does not accept start and end arguments.

    Please, note that this will create separate triggers that fire on the same day of the week.

    function setWeekdaysTrigger() {
    
      var startHour = 9;
      var endHour   = 13;
      var daysWeek  = []; //put Enums for days of the week here;
    
      //set trigger to specific week days;
      daysWeek.forEach(function(day){
        //set trigger for 9AM to 13PM;
        for(var h=startHour; h<endHour; h++) {
          var tg = ScriptApp.newTrigger('doSomething').timeBased();
              tg.onWeekDay(day);
              tg.atHour(h).nearMinute(15).create(); //arbitrary precision (minutes);
        }        
      });
    
    }
    

    As an update to the answer - please, note that, unfortunately, there are quotas on how many triggers you may create for the project on behalf of one user (currently equals to 20 per user per project).

    Configurable triggers

    As clarified in comments, configuring custom-days + custom-hours triggers requires a more sophisticated handling. Proposed solution (not counting explicit date-time checks) is dynamic trigger configuration (since one cannot set both the onWeekDay(day) + everyHours(n) due to recurrence overlapping [I found that onWeekDay(day) sets implicit everyWeeks(1) recurrence pattern]):

    First, create a main trigger function that sets days recurrence

    function custom24trigger() {
      var days  = [ScriptApp.WeekDay.TUESDAY]; //put Enum values here;
    
      //create trigger for each date (arbitrary hours);
      days.forEach(function(day){
        for(var offset = 0; offset < 23; offset=offset+4) { //every 4 hours;
          var tg = ScriptApp.newTrigger('changeTriggers').timeBased();
              tg.onWeekDay(day).atHour(offset).create(); //change every N hours;
        }
      });
    
    }
    

    Secondly, create a trigger change function that will be fired with set recurrence, which clears previously set triggers and creates new ones:

    function changeTriggers() {
      var triggers   = ScriptApp.getProjectTriggers(); //get project's triggers;
      var props      = PropertiesService.getScriptProperties(); //get script properties store;
      var triggerIds = props.getProperty('triggerIds'); //get arbitrary property to set Ids to;
    
      //if ids are not empty, split into Array of Ids, else set up empty Array;
      if(triggerIds!==null) { triggerIds = triggerIds.split(','); }else { triggerIds = []; }
    
      //loop over triggers and delete saved;
      triggerIds.forEach(function(id){
        triggers.forEach(function(trigger){
          if(trigger.getUniqueId() == id) { 
            ScriptApp.deleteTrigger(trigger); 
          }
        });
      });
    
      var today = new Date(); //current date;
      var start = today.getHours(); //current hour;
    
      var stop = start+3; //hour to stop (sets N triggers);
    
      //create hourly trigger for each hour;
      var newId = '';
      for(var h = start; h < stop; h++) {
        var trg = ScriptApp.newTrigger('doSomething').timeBased();
    
        //make trigger fire once at specific date and hour;
        today.setHours(h);
        today.setMinutes(0);
    
        var created = trg.at(today).create();   
        newId += ','+created.getUniqueId(); //get new trigger Id;
      }
    
      props.setProperty('triggerIds', newId); //set trigger property;
    }
    
    0 讨论(0)
提交回复
热议问题