I am trying to build Google script that should run every Tuesday, Wednesday and Thursday between 11am to 2 pm and after every 10 minutes.
For e.g. If today is Tuesday.. then the script will start executing at 11am morning and will execute after every 10 min and then should stop executing at 2pm on that day.
Now I am trying with programmatically setting the 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();
}
Please help in knowing if there is any other way for achieving the same.
You are going to need to set up lots of triggers to do this.
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.
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.
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.