Working hours only trigger

2019-01-15 20:29发布

I would like to run this script (embed drive list in a site) that I have used but only set the trigger to run it during office hours Monday - Friday.

I would like the script to run every 5 minutes but stop over night and at weekend. So I figured that I need to:

  1. Have a script that programatically creates a trigger to run the actual function during the day and give that a trigger to run once at the beginning of each day.

  2. Have a second script to delete the days trigger run once at the end of the day.

I am doing this as I have just learned that there is a maximum amount of CPU time you can use during a day so I do not want to waste it during times I don't need the list updated.

I can see how to create a trigger but it is not clear how to create a trigger to activate another script (function).

1条回答
我命由我不由天
2楼-- · 2019-01-15 21:03

I think it would be quite easier to insert a small routine at the beginning of your main function that checks the time of the day and simply 'returns' when out of office hours. This way you can let your trigger set to 5 minutes all the time. This 'hour check' will be very short in terms of processing time.

here is a test function to show how it works :

function officeHours(){
var nowH=new Date().getHours();
var nowD=new Date().getDay();
Logger.log('day : '+nowD+'   Hours : '+nowH)
if(nowH>17||nowH<8||nowD==6||nowD==0){return}
Browser.msgBox('time to work !');//normally your real function should begin here...
}
查看更多
登录 后发表回答