How to create a time-driven trigger that runs from 11 am to 2 pm?

后端 未结 2 1027
醉梦人生
醉梦人生 2020-12-22 05:56

I am trying to build a script that should run every Tuesday, Wednesday, and Thursday between 11 am to 2 pm and after every 10 minutes.

For e.g. If today is Tuesday..

相关标签:
2条回答
  • 2020-12-22 06:36

    You can also consider having a single trigger that runs every 10 minutes and, inside the trigger function, you can check the time and weekday. The code is executed only if all conditions are met else it returns without doing anything.

    function startCustomTrigger()
    {
      ScriptApp.newTrigger('StartProcess').timeBased().everyMinutes(10).create();
    }
    
    function StartProcess() {
    
      var date = new Date();  
      var day = date.getDay();
      var hrs = date.getHours();
    
      if ((day >= 2) && (day <= 4) && (hrs >= 11) && (hrs <= 14)) {
    
         // Add your code here
    
      }
    
    }
    
    0 讨论(0)
  • 2020-12-22 06:38

    You are going to need to set up lots of triggers to do this.

    • You need three weekly triggers; each one to run on each of the three days
    • Those three triggers, will pragmatically create a trigger to run every 10 minutes.
    • You need three more weekly triggers; each one to run on each of the three days to shut down the triggers that are running every 10 minutes, otherwise, they will just continue to run forever

    So, you need seven triggers. Three to run on that specific day, that will then create a trigger to run every 10 minutes starting at 10am. One trigger that will start at 10am and keep running indefinitely (until you shut it off). And three triggers to stop the trigger that is running every 10 minutes.

    Trigger On Day

    The triggers that will run on a specific day, should be set up manually. So, 6 of the triggers will be set up manually. The trigger that runs every 10 minutes needs to be created and deleted from code.

    The six triggers that are a "Week timer", to run on a specific day, only need to run once to create the trigger that will run every 10 minutes. I'd run them an hour earlier than you want the 10 minute triggers to run, just to make sure they are set to go.

    0 讨论(0)
提交回复
热议问题