问题描述
我试图构建Google脚本,该脚本应该在每周二,周三和周四的上午11点到下午2点之间以及每10分钟后运行一次。
如果今天是星期二,那么脚本将在上午11点开始执行,并在每10分钟执行一次,然后在当天下午2点停止执行。
现在我我尝试以编程方式设置触发器...像下面...
$ b $ pre $ function startCustomTrigger()
{
//首先删除所有现有触发器 - 用于安全
removeTriggers(false);
脚本将每分钟运行
ScriptApp.newTrigger('StartProcess').timeBased()。onWeekDay(ScriptApp.WeekDay.TUESDAY).onWeekDay(ScriptApp.WeekDay.WEDNESDAY) .onWeekDay(ScriptApp.WeekDay.THURSDAY).atHour(11).everyMinutes(10).create();
//脚本将每分钟运行
ScriptApp.newTrigger('StopProcess')。timeBased()。onWeekDay(ScriptApp.WeekDay.TUESDAY).onWeekDay(ScriptApp。 WeekDay.WEDNESDAY).onWeekDay(ScriptApp.WeekDay.THURSDAY).atHour(2).create();
$ b函数StopProcess()
{
//首先删除所有现有触发器 - 为了安全
removeTriggers( false);`在此处输入代码`
//脚本将每分钟运行
ScriptApp.newTrigger('startCustomTrigger')。timeBased()。onWeekDay(ScriptApp.WeekDay.TUESDAY)。 onWeekDay(ScriptApp.WeekDay.WEDNESDAY).onWeekDay(ScriptApp.WeekDay.THURSDAY).atHour(10).create();
请帮助我们了解是否有其他方法可以实现同样的。
您也可以考虑每10分钟运行一次触发器,并且在触发器功能中,您可以检查时间和工作日。
函数startCustomTrigger()
{
ScriptApp.newTrigger('StartProcess')。timeBased()。everyMinutes(10).create();
函数StartProcess(){
var date = new Date();
var day = date.getDay();
var hrs = date.getHours(); ((day> 2)&&(day< = 4)&< hrs> = 11)&&(hrs
//在此处添加您的代码
}
}
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 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
}
}
这篇关于基于时间的Google脚本 - 应该在上午11点到下午2点之间运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!