Class Properties

資源

屬性物件可做為存取使用者、文件或指令碼屬性的介面。具體的屬性類型取決於指令碼呼叫的 PropertiesService 的三種方法:PropertiesService.getDocumentProperties()PropertiesService.getUserProperties()PropertiesService.getScriptProperties()。屬性無法在指令碼間共用。如要進一步瞭解資源類型,請參閱資源服務指南

方法

方法傳回類型簡短說明
deleteAllProperties()Properties刪除目前 Properties 儲存庫中的所有屬性。
deleteProperty(key)Properties在目前的 Properties 商店中刪除具有指定鍵的資源。
getKeys()String[]取得目前 Properties 儲存庫中的所有鍵。
getProperties()Object取得目前 Properties 儲存庫中的所有鍵/值組合副本。
getProperty(key)String在目前的 Properties 儲存庫中,取得與指定鍵相關聯的值,如果不存在此鍵,則傳回 null
setProperties(properties)Properties在目前的 Properties 儲存庫中,設定指定物件的所有鍵/值組合。
setProperties(properties, deleteAllOthers)Properties在目前的 Properties 儲存庫中,設定指定物件的所有鍵/值組合,並視需要刪除儲存庫中的所有其他屬性。
setProperty(key, value)Properties在目前的 Properties 儲存庫中設定指定的鍵/值組合。

內容詳盡的說明文件

deleteAllProperties()

刪除目前 Properties 儲存庫中的所有屬性。

// Deletes all user properties.
const userProperties = PropertiesService.getUserProperties();
userProperties.deleteAllProperties();

回攻員

Properties:這個 Properties 儲存器,用於鏈結


deleteProperty(key)

在目前的 Properties 商店中刪除具有指定鍵的資源。

// Deletes the user property 'nickname'.
const userProperties = PropertiesService.getUserProperties();
userProperties.deleteProperty('nickname');

參數

名稱類型說明
keyString要刪除的資源鍵

回攻員

Properties:這個 Properties 儲存器,用於鏈結


getKeys()

取得目前 Properties 儲存庫中的所有鍵。

// Sets several properties, then logs the value of each key.
const scriptProperties = PropertiesService.getScriptProperties();
scriptProperties.setProperties({
  cow: 'moo',
  sheep: 'baa',
  chicken: 'cluck',
});
const keys = scriptProperties.getKeys();
Logger.log('Animals known:');
for (let i = 0; i < keys.length; i++) {
  Logger.log(keys[i]);
}

回攻員

String[]:目前 Properties 儲存庫中的所有鍵陣列


getProperties()

取得目前 Properties 儲存庫中的所有鍵/值組合副本。請注意,傳回的物件並非商店的即時檢視畫面。因此,變更傳回物件的屬性不會自動更新儲存空間中的屬性,反之亦然。

// Sets several script properties, then retrieves them and logs them.
const scriptProperties = PropertiesService.getScriptProperties();
scriptProperties.setProperties({
  cow: 'moo',
  sheep: 'baa',
  chicken: 'cluck',
});

const animalSounds = scriptProperties.getProperties();

// Logs:
// A chicken goes cluck!
// A cow goes moo!
// A sheep goes baa!
for (const kind in animalSounds) {
  Logger.log('A %s goes %s!', kind, animalSounds[kind]);
}

回攻員

Object:目前 Properties 儲存庫中的所有鍵/值組合副本


getProperty(key)

在目前的 Properties 儲存庫中,取得與指定鍵相關聯的值,如果不存在此鍵,則傳回 null

// Gets the user property 'nickname'.
const userProperties = PropertiesService.getUserProperties();
const nickname = userProperties.getProperty('nickname');
Logger.log(nickname);

參數

名稱類型說明
keyString要擷取的屬性值鍵

回攻員

String:與目前 Properties 商店中指定鍵相關聯的值


setProperties(properties)

在目前的 Properties 儲存庫中,設定指定物件的所有鍵/值組合。

// Sets multiple user properties at once.
const userProperties = PropertiesService.getUserProperties();
const newProperties = {
  nickname: 'Bob',
  region: 'US',
  language: 'EN'
};
userProperties.setProperties(newProperties);

參數

名稱類型說明
propertiesObject包含要設定的鍵/值組合物件

回攻員

Properties:這個 Properties 儲存器,用於鏈結


setProperties(properties, deleteAllOthers)

在目前的 Properties 儲存庫中,設定指定物件的所有鍵/值組合,並視需要刪除儲存庫中的所有其他屬性。

// Sets multiple user properties at once while deleting all other user
// properties.
const userProperties = PropertiesService.getUserProperties();
const newProperties = {
  nickname: 'Bob',
  region: 'US',
  language: 'EN'
};
userProperties.setProperties(newProperties, true);

參數

名稱類型說明
propertiesObject包含要設定的鍵/值組合物件
deleteAllOthersBooleantrue 可刪除屬性物件中的所有其他鍵/值組合;false 則不會刪除

回攻員

Properties:這個 Properties 儲存器,用於鏈結


setProperty(key, value)

在目前的 Properties 儲存庫中設定指定的鍵/值組合。

// Sets the user property 'nickname' to 'Bobby'.
const userProperties = PropertiesService.getUserProperties();
userProperties.setProperty('nickname', 'Bobby');

參數

名稱類型說明
keyString屬性的鍵
valueString要與鍵建立關聯的值

回攻員

Properties:這個 Properties 儲存器,用於鏈結