如何创建时间驱动的年度触发器? [英] How to create a yearly time-driven trigger?

查看:103
本文介绍了如何创建时间驱动的年度触发器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个基于时间的触发器来永久性地在每年的指定日期执行我的 incrementCell 函数一次。尝试在下面运行时

I'm attempting to create a time-based trigger to execute my incrementCell function once a year on a specified date at 1 AM in perpetuity. When attempting to run below

ScriptApp.newTrigger("incrementCell").timeBased().atDate(2018, 1, 4).atHour(1).everyWeeks(52).create();

我收到了,已经用at()或atDate()选择了特定的日期时间 c。

有趣的是,紧接在下面的行不会出错:

Interestingly, the line immediately below does not error out:

ScriptApp.newTrigger("incrementCell").timeBased().atDate(2018, 1, 4).create();


推荐答案

Google Apps脚本不支持年度触发器,但是您可以使用解决方法。创建一个在每月的1号运行的每月触发器,如果​​该月是1月,则运行实际函数。

Google Apps Script doesn't support yearly triggers but you can use a workaround. Create a monthly trigger that runs on the 1st of every month, and if the month is January, run the actual function.

function createYearlyTrigger() {
  ScriptApp.newTrigger("shouldTriggerRun")
  .timeBased().onMonthDay(1).atHour(1).create();
}

function shouldTriggerRun() {
  var date = new Date();
  if (date.getMonth() === 0) {
    incrementCell();
  }  
}

这篇关于如何创建时间驱动的年度触发器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆