【问题标题】:How to create a yearly time-driven trigger?如何创建年度时间驱动触发器?
【发布时间】:2018-01-04 06:10:08
【问题描述】:

我正在尝试创建一个基于时间的触发器来执行我的incrementCell 函数,每年在指定日期凌晨 1 点永久执行一次。尝试在下方运行时

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

我收到了"Already chosen a specific date time with at() or atDate()." 错误。

有趣的是,下面的行并没有出错:

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

【问题讨论】:

    标签: google-apps-script triggers google-sheets-api


    【解决方案1】:

    Google Apps 脚本不支持年度触发器,但您可以使用一种解决方法。创建一个每月 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 是一个整数?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-02
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多