Properties Service
Stay organized with collections
Save and categorize content based on your preferences.
AI-generated Key Takeaways
-
The Properties service stores simple key-value data for Apps Script projects, scoped to a script, user, or document.
-
There are three types of property stores: Script properties (shared by all users), User properties (specific to the current user), and Document properties (specific to an add-on in a document).
-
All data is stored as strings in key-value pairs, with non-string data types automatically converted.
-
Data can be saved using
setProperty
for single values orsetProperties
for multiple values. -
Data can be retrieved using
getProperty
for single values orgetProperties
for all values in a store. -
Data can be modified by saving the updated value again using
setProperty
. -
Properties can be deleted individually with
deleteProperty
or all at once withdeleteAllProperties
. -
Script properties can be managed manually through the project settings page for up to fifty properties.
The Properties service lets you store simple data in key-value pairs scoped to one script, one user of a script, or one document in which an add-on is used. It is typically used to store developer configuration or user preferences. Properties are never shared between scripts.
To view the daily quotas and storage limits for the Properties service, see Quotas for Google Services.
Comparison of property stores
The
PropertiesService
global object offers three methods, each of which returns a similar
Properties
object but with different access rights, as shown in the following table:
Script Properties | User Properties | Document Properties | |
---|---|---|---|
Method to access | getScriptProperties() |
getUserProperties() |
getDocumentProperties() |
Data shared among | All users of a script, add-on, or web app | The current user of a script, add-on, or web app | All users of an add-on in the open document |
Typically used for | App-wide configuration data, like the username and password for the developer's external database | User-specific settings, like metric or imperial units | Document-specific data, like the source URL for an embedded chart |
Data format
The Properties service stores all data as strings in key-value pairs. Data types that are not already strings are automatically converted to strings, including methods contained within saved objects.
Saving data
To save a single value, call the method Properties.setProperty(key,
value)
of the appropriate store, as shown in the following example:
try{ // Set a property in each of the three property stores. constscriptProperties=PropertiesService.getScriptProperties(); constuserProperties=PropertiesService.getUserProperties(); constdocumentProperties=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); }
To save data in bulk, pass a map of key-value pairs to
Properties.setProperties(properties)
.
Each key-value pair of the object in the parameter is stored as a separate
property:
try{ // Set multiple script properties in one call. constscriptProperties=PropertiesService.getScriptProperties(); scriptProperties.setProperties({ 'cow':'moo', 'sheep':'baa', 'chicken':'cluck' }); }catch(err){ // TODO (developer) - Handle exception console.log('Failed with error %s',err.message); }
Reading data
To retrieve a single value that you have previously saved, call
Properties.getProperty(key)
:
try{ // Get the value for the user property 'DISPLAY_UNITS'. constuserProperties=PropertiesService.getUserProperties(); constunits=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); }
To retrieve all values in the current property store, call
Properties.getProperties()
:
try{ // Get multiple script properties in one call, then log them all. constscriptProperties=PropertiesService.getScriptProperties(); constdata=scriptProperties.getProperties(); for(constkeyindata){ console.log('Key: %s, Value: %s',key,data[key]); } }catch(err){ // TODO (developer) - Handle exception console.log('Failed with error %s',err.message); }
Modifying data
The methods getProperty()
and getProperties()
return a copy of the stored
data, not a live view, so changing the returned object will not update the value
in the property store. To update the data in the store, simply save it again:
try{ // Change the unit type in the user property 'DISPLAY_UNITS'. constuserProperties=PropertiesService.getUserProperties(); letunits=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); }
Deleting data
To delete a single value, call
Properties.deleteProperty(key)
:
try{ // Delete the user property 'DISPLAY_UNITS'. constuserProperties=PropertiesService.getUserProperties(); userProperties.deleteProperty('DISPLAY_UNITS'); }catch(err){ // TODO (developer) - Handle exception console.log('Failed with error %s',err.message); }
To delete all properties in the current store, call
Properties.deleteAllProperties()
:
try{ // Get user properties in the current script. constuserProperties=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); }
Manage script properties manually
You can manually add up to fifty custom properties, as strings in key-value pairs, from the project settings page. To add more than fifty properties, you need to add them programmatically using the methods described above in Saving data. When you set script properties from the project settings page, you can’t reference script variables.
Add script properties
- Open your Apps Script project.
- At the left, click Project Settings The icon for project settings.
- To add the first property, under Script Properties click Add script property.
- To add second and subsequent properties, under Script Properties click Edit script properties > Add script property.
- For Property, enter the key name.
- For Value, enter the value for the key.
- (Optional) To add more properties, click Add script property.
- Click Save script properties.
Edit script properties
- Open your Apps Script project.
- At the left, click Project Settings The icon for project settings.
- Under Script Properties, click Edit script properties.
- Make changes to the key name and key value for each property you want to change.
- Click Save script properties.
Delete script properties
- Open your Apps Script project.
- At the left, click Project Settings The icon for project settings.
- Under Script Properties, click Edit script properties.
- Next to the property that you want to delete, click Remove .
- Click Save script properties.