安排特定重复窗口的Google App脚本功能 [英] Scheduling Google App Script function for specific recurring windows
问题描述
我正在尝试安排Apps脚本功能.我需要该电子邮件从上午11点至晚上9点开始每2小时触发一次星期一.
I'm trying to schedule an Apps Script function. I need the email to trigger Mon-Sat every 2 hours starting at 11am-9pm.
我编写了以下代码来每天创建触发器:
I've written the following code to create triggers for each day:
function createTriggers() {
var days = [ScriptApp.WeekDay.MONDAY,
ScriptApp.WeekDay.TUESDAY,
ScriptApp.WeekDay.WEDNESDAY,
ScriptApp.WeekDay.THURSDAY,
ScriptApp.WeekDay.FRIDAY,
ScriptApp.WeekDay.SATURDAY];
for (var i=0; i<days.length; i++) {
ScriptApp.newTrigger('emailDashboard')
.timeBased().onWeekDay(days[i])
.everyWeeks(1).everyHours(2).create();
}
}
运行上面的内容时,出现以下错误:
When I run what I have above, I get the following error:
已经设置了时钟触发器的重复间隔.(第79行,文件代码")"
"The recurrence interval on the clock trigger has already been set. (line 79, file "Code")"
我找不到与该响应有关的任何内容,它实际上并没有创建触发器.我假设上述情况每隔2小时就会给我发送一封电子邮件,即周一至周六.我不确定如何从中部时间上午11点开始,到中部时间晚上9点结束.
I cant find anything about that response, and it doesn't actually create the trigger. I assume what I have above would get me the email Mon-Sat, every 2 hours. I'm not sure how to go about starting at 11am central and ending at 9pm central.
推荐答案
您的问题来自过度规范.您使用的许多选项(例如 onWeekDay
和 everyWeeks
)都是互斥的.您可以在 Apps脚本参考页.
Your issue comes from over-specificity. Many of the options you use, like onWeekDay
and everyWeeks
, are mutually exclusive. You can read more about the class on its Apps Script reference page.
一种更简单的方法是安排所需的粒度:
A much simpler approach is to schedule the granularity you desire:
ScriptApp.newTrigger("myFunction").timeBased().everyHours(2).create();
,然后检查当前时间是否符合您的条件:
and then check whether the current time meets the criteria you have:
- 不是星期天
- 当地时间上午11点以上
- 当地时间不超过晚上9点
值得注意的是,当您使用小时级别的特异性时,Google会在选择的分钟数值上运行您的功能.
It's worth noting that Google will run your function at a minute value of its choosing when you use the hour level of specificity.
与其他触发函数一样,还有一个可用的事件对象
As with other triggered functions, there is an available event object
一个示例鉴别符,假定您只关心UTC值(事件对象中所有值的语言环境):
An example discriminator that assumes you only care about UTC values (which are the locale for all values in the event object):
function getsCalledByClockTrigger(e) {
if(!e) throw new Error("Called from the Script Editor");
else console.log(e); // Show this event object in Stackdriver logs.
// Check that it isn't UTC Sunday.
if(e["day-of-week"] === 7)
return;
// Checking the UTC hour is between 11 AM and 9 PM.
if(e.hour < 11 || e.hour > 21)
return;
/* Do stuff */
}
使用 var now = new Date()
并检查各种 now.getDay()
.
A discriminator for non-UTC values would probably be simplest with var now = new Date()
and checking various Date
properties like now.getDay()
.
这篇关于安排特定重复窗口的Google App脚本功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!