Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 如何创建年度时间驱动触发器?_Google Apps Script_Triggers_Google Sheets Api - Fatal编程技术网

Google apps script 如何创建年度时间驱动触发器?

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

我正在尝试创建一个基于时间的触发器,以在指定日期的凌晨1点永久执行我的
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是一个整数?