【发布时间】:2022-01-13 15:06:04
【问题描述】:
我有一个 Google Apps 脚本函数,当从编辑器运行和在时间驱动脚本中运行时,它的解释似乎不同。当作为预定脚本运行时,它返回以下错误,坦率地说我不明白为什么。 错误异常:无效参数:yyyy-MM-dd'T'[object Object]:00Z at isBetweenTimeWithTz(Code:24:24)
当函数从编辑器运行时,我得到了我期望的输出,例如 0 '2022-01-13T00:50:00+0100' '2022-01-13T01:30:00+0100' 2022 年 1 月 12 日星期三 18:50:00 GMT-0500(东部标准时间) 1 月 12 日星期三2022 年 19:30:00 GMT-0500(东部标准时间)。
function isBetweenTimeWithTz(from_t="00:50", to_t="01:30", tz="Europe/Prague"){
now = new Date();
from_str = Utilities.formatDate(now, tz, `yyyy-MM-dd'T'${from_t}:00Z`);
to_str = Utilities.formatDate(now, tz, `yyyy-MM-dd'T'${to_t}:00Z`);
from_dt = new Date(from_str);
to_dt = new Date(to_str);
is_between = from_dt <= now & now <= to_dt;
console.log(is_between, from_str, to_str, from_dt, to_dt);
return is_between;
}
对于调度,我使用这个函数:
function createTimeDrivenTriggers() {
ScriptApp.newTrigger('isBetweenTimeWithTz')
.timeBased()
.everyMinutes(1)
.create();
console.log("Trigger triggered");
}
【问题讨论】: