【问题标题】:Run trigger at specific date and time在特定日期和时间运行触发器
【发布时间】:2017-04-01 13:50:35
【问题描述】:

我想在特定日期和时间(+ 或 - 15 分钟)运行触发器。

我有一个 YYYY-MM-DD HH:MM 格式的最小日期字符串 2017-04-01 18:05 我将如何修改以下脚本以在 18 小时 5 分钟时运行.

 var triggerDay = new Date(2017, 04, 01);
 ScriptApp.newTrigger("myFunction")
   .timeBased()
   .at(triggerDay)
   .create();

【问题讨论】:

    标签: date google-apps-script triggers google-sheets google-docs


    【解决方案1】:

    你可以试试这个:

    function createTimeDrivenTriggers() {
    // Trigger on 2017-04-01 around 18:00 (±15 min).
    ScriptApp.newTrigger('myFunction')
        .timeBased()
        .atDate(2017, 04, 01)
        .atHour(18)
        .create();
    }
    

    https://developers.google.com/apps-script/reference/script/clock-trigger-builder

    【讨论】:

    • 您可以添加 .nearMinute(5) 但由于您的 +- 15 分钟,您可能无法确定它是否真的有效。
    • after(durationMilliseconds)的用法怎么样,究竟是什么意思呢?它是自纪元以来我们必须指定的毫数?
    【解决方案2】:
    // the full Date constructor is:
    // new Date( year, month, day, hour, min, sec, ms )
    var triggerDay = new Date(2017, 3, 01, 18, 5);
    ScriptApp.newTrigger("myFunction")
      .timeBased()
      .at(triggerDay)
      .create();
    

    我很确定构造函数中的month 是从零开始的(January==0),所以我将04 更改为3。

    【讨论】:

    • 注意:如果您的字符串是符合 ISO 标准的日期格式,您可以使用 new Date(string)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-24
    • 2013-10-05
    • 1970-01-01
    相关资源
    最近更新 更多