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?

GASで名前付き引数のfunctionをトリガー実行すると予期せぬ挙動になる

Posted at

事象

例えば下記のfunctionをトリガー実行すると予期せぬエラーが起こる。

function hogehoge(offsetDays = -1) {
  Logger.log(offsetDays)
  const baseDate = new Date(); 
  baseDate.setDate(baseDate.getDate() + offsetDays); // offsetDaysを適用

  console.log(baseDate)

}

トリガー実行時の結果の例

{month=2.0, hour=14.0, day-of-week=4.0, timezone=UTC, week-of-year=6.0, day-of-month=6.0, year=2025.0, authMode=FULL, second=5.0, minute=5.0, triggerUid=~~~~~~~}

解決策

function hogehoge() {
  offsetDays = -1 // ★ここに入れる
  Logger.log(offsetDays)
  const baseDate = new Date(); 
  baseDate.setDate(baseDate.getDate() + offsetDays); // offsetDaysを適用

  console.log(baseDate)

}

結論

トリガー実行するときは無名関数にしておいた方が安全で

0
0
5

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?