Technica
Technica

Reputation: 37

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

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.. then the script will start executing at 11 am morning and will execute after every 10 min and then should stop executing at 2 pm on that day.

Now I am trying with programmatically setting the triggers like below:

function startCustomTrigger()
{
  
  //first remove all existing triggers - for safty
   removeTriggers(false);
  
  //script will run every minute defined  
  ScriptApp.newTrigger('StartProcess').timeBased().onWeekDay(ScriptApp.WeekDay.TUESDAY).onWeekDay(ScriptApp.WeekDay.WEDNESDAY).onWeekDay(ScriptApp.WeekDay.THURSDAY).atHour(11).everyMinutes(10).create();
  
  
  //script will run every minute defined  
  ScriptApp.newTrigger('StopProcess').timeBased().onWeekDay(ScriptApp.WeekDay.TUESDAY).onWeekDay(ScriptApp.WeekDay.WEDNESDAY).onWeekDay(ScriptApp.WeekDay.THURSDAY).atHour(2).create();
 
}


function StopProcess()
{
   //first remove all existing triggers - for safety
   removeTriggers(false);`enter code here`
  
  //script will run every minute defined  
  ScriptApp.newTrigger('startCustomTrigger').timeBased().onWeekDay(ScriptApp.WeekDay.TUESDAY).onWeekDay(ScriptApp.WeekDay.WEDNESDAY).onWeekDay(ScriptApp.WeekDay.THURSDAY).atHour(10).create();
  
}

Upvotes: 0

Views: 3767

Answers (2)

Amit Agarwal
Amit Agarwal

Reputation: 11268

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

  }

}

Upvotes: 4

Alan Wells
Alan Wells

Reputation: 31300

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.

Upvotes: 0

Related Questions