1
1

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で使える構文・メソッド(“schedule in prev”系)メモ

Posted at

今回GASを触っていて便利だなーと思ったメソッドがあったので、それ関連で今後も使えそうなものをピックアップした。

オブジェクト・配列・文字列の存在チェック

//オブジェクトにキーがあるか(値ではなくてキーを確認する)
'schedule' in prev
//prev が undefined だとエラー


//安全に存在を確認
prev?.hasOwnProperty('schedule')
//オプショナルチェーンで安全に確認。? があるとprevがなくても undefined を返す


//ネストされたキーを安全に参照
prev?.object?.schedule
//prev.object が無くても安全に undefined を返す。 ? がないとType errorになる


//配列に値が含まれるか
arr.includes('schedule')
//indexOf() より直感的。indexOf()は配列の何番目にあるかを示す


//文字列に部分一致があるか
text.includes('schedule')
//大文字小文字を区別

条件分岐を短く

//三項演算子
//条件を1行で処理
const msg = success ? 'OK' : 'Error';


//複数候補チェック
//値がいずれかに一致するか
['paid','trial'].includes(status)


//一部一致
//正規表現で'sub'が文字列のどこかに含まれているか
/sub/.test(eventType)
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?