物業服務

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

如要查看資源服務的每日配額與儲存空間上限,請參閱 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. 如要新增第一項屬性,請按一下「指令碼屬性」下方的 新增指令碼屬性
  4. 如要新增第二和後續屬性,請按一下「指令碼屬性」下方的 [編輯指令碼屬性] >> [新增指令碼屬性]
  5. 在「Property」(屬性) 部分輸入鍵名。
  6. 在「Value」(值) 部分輸入該鍵的值。
  7. (選用) 如要新增更多屬性,請按一下「新增指令碼屬性」
  8. 按一下「儲存指令碼屬性」

編輯指令碼屬性

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

刪除指令碼屬性

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