0
0

はじめに

GASには「スクリプトプロパティ」という、スクリプト全体で使用できるプロパティを保存するための機能があります。この機能を利用して、APIキーなどの機密情報を保存します。

スクリプトプロパティの設定

GASを開き、左側の歯車アイコンを選択します。
スクリプトプロパティを設定する画面が表示されるので、プロパティの欄にAPI_KEYIDなどのキーを設定し、値の欄に適切な値を入力してください。

image.png

image.png

コードから呼び出す場合、このようなプログラムを記述します。

コード.gs
function callApi() {
  var apiKey = PropertiesService.getScriptProperties().getProperty('API_KEY');
  var url = 'https://api.example.com/data?api_key=' + apiKey;
  
  var response = UrlFetchApp.fetch(url);
  var data = JSON.parse(response.getContentText());
  
  Logger.log(data);
}

値の設定: PropertiesService.getScriptProperties().setProperty('KEY', 'VALUE')

値の取得: PropertiesService.getScriptProperties().getProperty('KEY')

削除: PropertiesService.getScriptProperties().deleteProperty('KEY')

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