Time based Google Script - Should run between 11am

2019-01-29 07:40发布

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.

2条回答
姐就是有狂的资本
2楼-- · 2019-01-29 08:13

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.

查看更多
戒情不戒烟
3楼-- · 2019-01-29 08:28

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

  }

}
查看更多
登录 后发表回答