LoginSignup
3
2

More than 5 years have passed since last update.

Google Apps Scriptのプロジェクトのトリガーの取り扱い

Last updated at Posted at 2017-12-11

Google Apps Scriptの現在のプロジェクトのトリガーは、分/時/日/週/月タイマー、及び特定の日時が指定できる。

曖昧な時間に実行可能なものはタイマー指定で問題はないが、厳密に実行時間を指定したい場合は特定の日時を指定することになる。
とは言え、トリガーに登録可能な数は最大で20個までである。

例えば、1時間に1回、厳密な時間に実行する処理が必要な場合、24個のトリガーが必要になり、最大20個の制約上、登録ができない。
しかも、トリガーは実行後に削除されず残り続ける。
なので、以下のようにトリガーの削除と登録を行うことで最大20個の制約を回避する。

// 全登録トリガーを削除
function DeleteAllTrigger() {
  var triggers = ScriptApp.getProjectTriggers();
  for(var i = 0; i < triggers.length; i++) {
    ScriptApp.deleteTrigger(triggers[i]);
  }
}
// トリガーを登録(12:34に特定の日時で)
var triggerDay = new Date();
triggerDay.setHours(12);
triggerDay.setMinutes(34);
trigger = ScriptApp.newTrigger("TriggerName").timeBased().at(triggerDay).create();
3
2
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
3
2