[英]Google Apps Script: Time Driven Triggers timezone issue
我正在嘗試在 Google Apps 腳本中構建一個基本的時間驅動觸發器,以在指定的日期和時間運行函數。
我的代碼如下:
function createTimeDrivenTriggers() {
ScriptApp.newTrigger('myFunction')
.timeBased()
.atDate(2019, 12, 29)
.atHour(12)
.nearMinute(5)
.create();
Logger.log("Trigger started!");
}
function myFunction() {
Logger.log("I was triggered!");
}
但是,每次我嘗試運行 createTimeDrivenTriggers 函數時,都會收到以下錯誤:
You cannot schedule an event in the past. (line 7, file "Triggers")
在撰寫本文時 2019-12-29 12:05 不是過去(在我的時區)。 這讓我覺得腳本在與我不同的時區運行..是這樣嗎? 如果是這樣,如何將腳本設置為默認為用戶的時區?
此腳本是 Google Sheets Add On 的一部分,我確認我的 Google Sheets 時區設置為我的本地時區。
這個答案怎么樣? 不幸的是,不能同時使用atDate
和atHour
。 在您的情況下,使用atDate
並嘗試將觸發器安裝為2019-12-29 00:00
。 所以會出現這樣的錯誤。
當你想安裝2019-12-29 12:05
的時間驅動觸發器2019-12-29 12:05
,如何修改如下?
ScriptApp.newTrigger('myFunction') .timeBased() .atDate(2019, 12, 29) .atHour(12) .nearMinute(5) .create();
至:
ScriptApp.newTrigger("myFunction") .timeBased() .at(new Date(2019, 11, 29, 12, 5)) .create();
在這種情況下,請為 12 月使用11
。 因為 Date 的月份的初始索引是0
。
monthIndex:表示月份的整數值,從一月的 0 開始到十二月的 11。 參考
如果這不是您想要的方向,我深表歉意。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.