物業服務

屬性服務可讓您將簡單資料儲存在鍵/值組合中,並將範圍限定為一個指令碼、一個指令碼使用者,或使用外掛程式的一個文件。這通常用於儲存開發人員設定或使用者偏好設定。屬性不會在指令碼間共用。

如要查看 Properties 服務的每日配額和儲存空間限制,請參閱「Google 服務配額」。

比較資源商店

PropertiesService 全域物件提供三種方法,每種方法都會傳回類似的 Properties 物件,但存取權不同,如下表所示:

指令碼內容 使用者屬性 文件內容
存取方式 getScriptProperties() getUserProperties() getDocumentProperties()
共用資料 指令碼、外掛程式或網頁應用程式的所有使用者 指令碼、外掛程式或網頁應用程式的目前使用者 開啟文件中外掛程式的所有使用者
通常用於 應用程式全域設定資料,例如開發人員外部資料庫的使用者名稱和密碼 使用者專屬設定,例如公制或英制單位 文件專屬資料,例如內嵌圖表的來源網址

資料格式

Properties 服務會將所有資料儲存為鍵/值組合中的字串。系統會自動將非字串的資料類型轉換為字串,包括儲存物件內含的方法。

儲存資料

如要儲存單一值,請呼叫適當儲存區的 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);
}

如要大量儲存資料,請將鍵/值組合的地圖傳遞至 Properties.setProperties(properties)。參數中物件的每個鍵/值組合都會儲存為個別屬性:

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);
}

讀取資料

如要擷取先前儲存的單一值,請呼叫 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 個自訂屬性 (字串)。如要新增超過五十個屬性,請使用儲存資料一節中說明的程式輔助方法。從專案設定頁面設定指令碼屬性時,您無法參照指令碼變數。

新增指令碼屬性

  1. 開啟 Apps Script 專案。
  2. 按一下左側的「專案設定」圖示 專案設定圖示
  3. 如要新增第一個屬性,請在「指令碼屬性」下方點選「新增指令碼屬性」
  4. 如要新增第二個以上的屬性,請依序點選「指令碼屬性」下方的「編輯指令碼屬性」>「新增指令碼屬性」
  5. 在「Property」中輸入金鑰名稱。
  6. 在「值」中輸入索引鍵的值。
  7. (選用) 如要新增更多屬性,請按一下「新增指令碼屬性」
  8. 按一下「儲存指令碼屬性」

編輯指令碼屬性

  1. 開啟 Apps Script 專案。
  2. 按一下左側的「專案設定」圖示 專案設定圖示
  3. 在「指令碼屬性」下方,按一下「編輯指令碼屬性」
  4. 針對要變更的每個屬性,修改鍵名和鍵值。
  5. 按一下「儲存指令碼屬性」

刪除指令碼屬性

  1. 開啟 Apps Script 專案。
  2. 按一下左側的「專案設定」圖示 專案設定圖示
  3. 在「指令碼屬性」下方,按一下「編輯指令碼屬性」
  4. 找出要刪除的資源,然後按一下旁邊的「移除」圖示
  5. 按一下「儲存指令碼屬性」