0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

GASでトリガーを作成する際の注意点

Posted at

GASでトリガーを作成する際の注意点

GASで任意の日時にトリガーを作成するファンクションで
詰まったことがあったのでメモ。

任意の日付にトリガーを作成

setTrigger.gs
function setTrigger(){
  var trigger = new Date();

  //トリガー設定時間
  trigger.setHours(10);
  trigger.setMinutes(00);
  
  //セットしたトリガーの時間を確認
  console.log(trigger);
  //起動させたいfunctionを設定
  ScriptApp.newTrigger('test_func').timeBased().at(trigger).create();    
}

実行した結果はこちら
image.png

ふむ、見た感じは想定してる時間でログが出てる。

実際に作成されたトリガーがこちら。

image.png

image.png

むむ?トリガーの起動時間がおかしい。。。

まずは設定する前の現在時間をログに出してみよう。
※現在時刻 10:00

setTrigger
function setTrigger(){
  var trigger = new Date();
  
  //とりあえず何も編集しない時間を確認
  console.log(trigger);  
}

image.png

あれ?全然違う時間が表示されたぞ。
というか右のタイムゾーンが日本時間になっていない。。。

appsscript.jsonをエディタで表示にチェックを入れてタイムゾーンを確認してみよう。
※プロジェクトの設定からチェックを入れることで確認できます

図13.png

やはりtimeZoneが日本時間になっていない。。

appsscript.json
{
  "timeZone": "America/New_York",
  "dependencies": {
    "enabledAdvancedServices": [
      {
        "userSymbol": "Drive",
        "version": "v2",
        "serviceId": "drive"
      }
    ]
  },
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8"
}

ということでタイムゾーンを以下のように日本時間に変更します

"timeZone": "Asia/Tokyo",

では実際にログを出力してみよう。
image.png

うん!想定通りで日本時間になってる!

では10:15に時間をセットして最初に記述したsetTrigger.gsを起動してみます

image.png

想定通りの時間でトリガーが作成されている。
あとは正常に起動するのを確認するだけ。

図15.png

よし!正常に稼働してる!
これで好きな時間に毎月任意の時間にファンクションを起動させる等が可能になります!

意図しない時間にトリガーが作成されたらまずはタイムゾーンを確認しましょう。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?