プロパティ サービス

プロパティ サービスを使用すると、1 つのスクリプト、1 つのスクリプトのユーザー、アドオンが使用されている 1 つのドキュメントをスコープとする Key-Value ペアで単純なデータを保存できます。通常は、デベロッパーの構成やユーザーの設定を保存するために使用されます。プロパティがスクリプト間で共有されることはありません。

プロパティ サービスの 1 日あたりの割り当てとストレージの上限を確認するには、Google サービスの割り当てをご覧ください。

宿泊施設の比較

次の表に示すように、PropertiesService グローバル オブジェクトには 3 つのメソッドがあり、それぞれ同様の Properties オブジェクトを返しますが、アクセス権が異なります。

スクリプト プロパティ ユーザー プロパティ ドキュメント プロパティ
アクセス方法 getScriptProperties() getUserProperties() getDocumentProperties()
データの共有先 スクリプト、アドオン、ウェブアプリのすべてのユーザー スクリプト、アドオン、ウェブアプリの現在のユーザー 開いているドキュメント内のアドオンのすべてのユーザー
一般的な用途 アプリ全体の構成データ(デベロッパーの外部データベースのユーザー名とパスワードなど) メートル単位やヤード単位など、ユーザー固有の設定 ドキュメント固有のデータ(埋め込みグラフのソース URL など)

データ形式

プロパティ サービスでは、すべてのデータが Key-Value ペアの文字列として保存されます。まだ文字列ではないデータ型は、保存されたオブジェクト内に含まれるメソッドを含め、自動的に文字列に変換されます。

データの保存

単一の値を保存するには、該当するストアのメソッド Properties.setProperty(key, value) を呼び出します。次の例をご覧ください。

service/propertyService.gs
try {
  // Set a property in each of the three property stores.
  const scriptProperties = PropertiesService.getScriptProperties();
  const userProperties = PropertiesService.getUserProperties();
  const documentProperties = PropertiesService.getDocumentProperties();

  scriptProperties.setProperty('SERVER_URL', 'http://www.example.com/');
  userProperties.setProperty('DISPLAY_UNITS', 'metric');
  documentProperties.setProperty('SOURCE_DATA_ID',
      '1j3GgabZvXUF177W0Zs_2v--H6SPCQb4pmZ6HsTZYT5k');
} catch (err) {
  // TODO (developer) - Handle exception
  console.log('Failed with error %s', err.message);
}

データを一括で保存するには、Key-Value ペアのマップを Properties.setProperties(properties) に渡します。パラメータ内のオブジェクトの Key-Value ペアは、それぞれ個別のプロパティとして保存されます。

service/propertyService.gs
try {
  // Set multiple script properties in one call.
  const scriptProperties = PropertiesService.getScriptProperties();
  scriptProperties.setProperties({
    'cow': 'moo',
    'sheep': 'baa',
    'chicken': 'cluck'
  });
} catch (err) {
  // TODO (developer) - Handle exception
  console.log('Failed with error %s', err.message);
}

データの読み取り

以前に保存した値を 1 つ取得するには、Properties.getProperty(key) を呼び出します。

service/propertyService.gs
try {
  // Get the value for the user property 'DISPLAY_UNITS'.
  const userProperties = PropertiesService.getUserProperties();
  const units = userProperties.getProperty('DISPLAY_UNITS');
  console.log('values of units %s', units);
} catch (err) {
  // TODO (developer) - Handle exception
  console.log('Failed with error %s', err.message);
}

現在のプロパティ ストア内のすべての値を取得するには、Properties.getProperties() を呼び出します。

service/propertyService.gs
try {
  // Get multiple script properties in one call, then log them all.
  const scriptProperties = PropertiesService.getScriptProperties();
  const data = scriptProperties.getProperties();
  for (const key in data) {
    console.log('Key: %s, Value: %s', key, data[key]);
  }
} catch (err) {
  // TODO (developer) - Handle exception
  console.log('Failed with error %s', err.message);
}

データの修正

メソッド getProperty()getProperties() は、ライブビューではなく保存済みデータのコピーを返すため、返されるオブジェクトを変更してもプロパティ ストア内の値は更新されません。ストア内のデータを更新するには、もう一度保存するだけです。

service/propertyService.gs
try {
  // Change the unit type in the user property 'DISPLAY_UNITS'.
  const userProperties = PropertiesService.getUserProperties();
  let units = userProperties.getProperty('DISPLAY_UNITS');
  units = 'imperial'; // Only changes local value, not stored value.
  userProperties.setProperty('DISPLAY_UNITS', units); // Updates stored value.
} catch (err) {
  // TODO (developer) - Handle exception
  console.log('Failed with error %s', err.message);
}

データを削除する

単一の値を削除するには、Properties.deleteProperty(key) を呼び出します。

service/propertyService.gs
try {
  // Delete the user property 'DISPLAY_UNITS'.
  const userProperties = PropertiesService.getUserProperties();
  userProperties.deleteProperty('DISPLAY_UNITS');
} catch (err) {
  // TODO (developer) - Handle exception
  console.log('Failed with error %s', err.message);
}

現在のストア内のすべてのプロパティを削除するには、Properties.deleteAllProperties() を呼び出します。

service/propertyService.gs
try {
  // Get user properties in the current script.
  const userProperties = PropertiesService.getUserProperties();
  // Delete all user properties in the current script.
  userProperties.deleteAllProperties();
} catch (err) {
  // TODO (developer) - Handle exception
  console.log('Failed with error %s', err.message);
}

スクリプト プロパティを手動で管理する

プロジェクトの設定ページから、最大 50 個のカスタム プロパティを Key-Value ペアの文字列として手動で追加できます。50 個を超えるプロパティを追加するには、上記のデータの保存で説明されている方法で、プログラムで追加する必要があります。プロジェクト設定ページからスクリプトのプロパティを設定する場合、スクリプト変数を参照することはできません。

スクリプトのプロパティを追加する

  1. Apps Script プロジェクトを開きます。
  2. 左側の [プロジェクトの設定] プロジェクト設定のアイコン をクリックします。
  3. 最初のプロパティを追加するには、[スクリプトのプロパティ] の下にある [スクリプト プロパティを追加] をクリックします。
  4. 2 番目以降のプロパティを追加するには、[スクリプトのプロパティ] で [スクリプトのプロパティを編集] > [スクリプトのプロパティを追加] をクリックします。
  5. [プロパティ] にキー名を入力します。
  6. [] にキーの値を入力します。
  7. (省略可)さらにプロパティを追加するには、[スクリプトのプロパティを追加] をクリックします。
  8. [スクリプトのプロパティを保存] をクリックします。

スクリプトのプロパティを編集する

  1. Apps Script プロジェクトを開きます。
  2. 左側の [プロジェクトの設定] プロジェクト設定のアイコン をクリックします。
  3. [スクリプトのプロパティ] で [スクリプトのプロパティを編集] をクリックします。
  4. 変更する各プロパティのキー名と Key-Value を変更します。
  5. [スクリプトのプロパティを保存] をクリックします。

スクリプトのプロパティを削除する

  1. Apps Script プロジェクトを開きます。
  2. 左側の [プロジェクトの設定] プロジェクト設定のアイコン をクリックします。
  3. [スクリプトのプロパティ] で [スクリプトのプロパティを編集] をクリックします。
  4. 削除するプロパティの横にある削除アイコン をクリックします。
  5. [スクリプトのプロパティを保存] をクリックします。