基于时间的可安装触发器 - 文档中的示例不起作用
Time-based installable trigger - example from documentation doesn't work
在表格插件中,我尝试创建基于时间的触发器,它会在电子表格中最后一次编辑后的某个时间运行。
try
{
// create new trigger
ScriptApp.newTrigger(triggerFunctionName)
.timeBased()
.after(timeout)
.create();
}
catch(err)
{
console.log(err);
}
为了测试它,我将超时设置为 30 毫秒:
let timeout = 30 * 1000;
但是在 Cloud Logging 中使用此日志失败:
{ [Exception: Clock events must be scheduled at least 1 hour(s) apart.] name: 'Exception' }
我检查了 after
的文档并将超时从 30 秒更改为 10 分钟,就像文档示例
let timeout = 10 * 60 * 1000;
但运气不好 - 抛出相同的异常:
{ [Exception: Clock events must be scheduled at least 1 hour(s) apart.] name: 'Exception' }
有什么我遗漏的吗?为什么无法在 10 分钟后将基于时间的触发器创建到 运行?
很大程度上取决于创建时间驱动触发器的上下文。
由于此触发器是从加载项创建的,因此不能以小于一小时的间隔触发。这是来自 official documentation 的引述,请注意以粗体突出显示的文本:
A time-driven trigger (also called a clock trigger) is similar to a cron job in Unix. Time-driven triggers let scripts execute at a particular time or on a recurring interval, as frequently as every minute or as infrequently as once per month. (Note that an add-on can use a time-driven trigger once per hour at most.) The time may be slightly randomized — for example, if you create a recurring 9 a.m. trigger, Apps Script chooses a time between 9 a.m. and 10 a.m., then keeps that timing consistent from day to day so that 24 hours elapse before the trigger fires again.
在表格插件中,我尝试创建基于时间的触发器,它会在电子表格中最后一次编辑后的某个时间运行。
try
{
// create new trigger
ScriptApp.newTrigger(triggerFunctionName)
.timeBased()
.after(timeout)
.create();
}
catch(err)
{
console.log(err);
}
为了测试它,我将超时设置为 30 毫秒:
let timeout = 30 * 1000;
但是在 Cloud Logging 中使用此日志失败:
{ [Exception: Clock events must be scheduled at least 1 hour(s) apart.] name: 'Exception' }
我检查了 after
的文档并将超时从 30 秒更改为 10 分钟,就像文档示例
let timeout = 10 * 60 * 1000;
但运气不好 - 抛出相同的异常:
{ [Exception: Clock events must be scheduled at least 1 hour(s) apart.] name: 'Exception' }
有什么我遗漏的吗?为什么无法在 10 分钟后将基于时间的触发器创建到 运行?
很大程度上取决于创建时间驱动触发器的上下文。
由于此触发器是从加载项创建的,因此不能以小于一小时的间隔触发。这是来自 official documentation 的引述,请注意以粗体突出显示的文本:
A time-driven trigger (also called a clock trigger) is similar to a cron job in Unix. Time-driven triggers let scripts execute at a particular time or on a recurring interval, as frequently as every minute or as infrequently as once per month. (Note that an add-on can use a time-driven trigger once per hour at most.) The time may be slightly randomized — for example, if you create a recurring 9 a.m. trigger, Apps Script chooses a time between 9 a.m. and 10 a.m., then keeps that timing consistent from day to day so that 24 hours elapse before the trigger fires again.