GASのスクリプトプロパティをjsonで取得したい

2023.03.15

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

ファイルに保存しときたいけど、毎回忘れてしまうやつ。

PropertiesServiceクラスのgetScriptProperties()を使用することでスクリプトプロパティの内容を取得できる。

jsonにするのはJSON.stringify()を使ってjson文字列にすることができます。

const properties = PropertiesService.getScriptProperties().getProperties();
console.log(JSON.stringify(properties))

ちなみに、jsonファイルをGoogle Driveに保存するには、

DriveAppクラスを使うことで可能です。

const properties = PropertiesService.getScriptProperties().getProperties();
const jsonStr = JSON.stringify(properties);

// 保存
const folder = DriveApp.getFolderById('<<フォルダID>>'); //フォルダIDを指定
folder.createFile('property.json', jsonStr, MimeType.PLAIN_TEXT);

createFile()で新規にファイルを保存してみました。