LoginSignup
0
1

GASで環境変数を利用する

Posted at

スクリプトプロパティを利用することで実現できます。
プロジェクトの設定 > スクリプト プロパティで設定します。

image.png

値は次のコードで取得・変更できます。

//取得
PropertiesService.getScriptProperties().getProperty("key");
//変更
PropertiesService.getScriptProperties().setProperty("key","value");

いちいちPropertiesService.getScriptProperties()と書くのが面倒なので、Node.jsのenv風に利用できるようにしてみました。

const env = (() =>  new Proxy({e : PropertiesService.getScriptProperties()}, {
      get: (t, p) => t.e ? t.e.getProperty(p) : void 0,
      set: (t, k, v) => t.e ? t.e.setProperty(k, v) : void 0
  }))();

//取得
const value = env.API_TOKEN;
//変更
env.API_TOKEN = value;
0
1
2

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
1