はじめに
GASには「スクリプトプロパティ」という、スクリプト全体で使用できるプロパティを保存するための機能があります。この機能を利用して、APIキーなどの機密情報を保存します。
スクリプトプロパティの設定
GASを開き、左側の歯車アイコンを選択します。
スクリプトプロパティを設定する画面が表示されるので、プロパティの欄にAPI_KEY
、ID
などのキーを設定し、値の欄に適切な値を入力してください。
コードから呼び出す場合、このようなプログラムを記述します。
コード.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')