物業服務

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

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

兩家房源商店的比較

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

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

資料格式

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

正在儲存資料

如要儲存單一值,請呼叫適當商店的 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 個自訂屬性 (鍵/值組合中的字串)。如要新增超過 50 個屬性,您需要使用上述儲存資料中所述的方法,以程式輔助方式新增屬性。在專案設定頁面設定指令碼屬性時,無法參照指令碼變數。

新增指令碼屬性

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

編輯指令碼屬性

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

刪除指令碼屬性

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