Google apps script 如何创建年度时间驱动触发器?
我正在尝试创建一个基于时间的触发器,以在指定日期的凌晨1点永久执行我的Google apps script 如何创建年度时间驱动触发器?,google-apps-script,triggers,google-sheets-api,Google Apps Script,Triggers,Google Sheets Api,我正在尝试创建一个基于时间的触发器,以在指定日期的凌晨1点永久执行我的incrementCell函数一次。尝试在下面运行时 ScriptApp.newTrigger("incrementCell").timeBased().atDate(2018, 1, 4).atHour(1).everyWeeks(52).create(); 我收到一个“已经用at()或atDate()选择了一个特定的日期时间。”错误 有趣的是,下面这一行没有出错: ScriptApp.newTrigg
incrementCell
函数一次。尝试在下面运行时
ScriptApp.newTrigger("incrementCell").timeBased().atDate(2018, 1, 4).atHour(1).everyWeeks(52).create();
我收到一个“已经用at()或atDate()选择了一个特定的日期时间。”
错误
有趣的是,下面这一行没有出错:
ScriptApp.newTrigger("incrementCell").timeBased().atDate(2018, 1, 4).create();
Google应用程序脚本不支持年度触发器,但您可以使用变通方法。创建每月1号运行的每月触发器,如果月份是1月,则运行实际函数
function createYearlyTrigger() {
ScriptApp.newTrigger("shouldTriggerRun")
.timeBased().onMonthDay(1).atHour(1).create();
}
function shouldTriggerRun() {
var date = new Date();
if (date.getMonth() === 0) {
incrementCell();
}
}
明白了,谢谢。对于if比较,最好使用两个相等,而不是三个相等,因为getMonth返回一个Month对象,而0是一个整数?