How can I store and retrieve objects from Google Apps Script Project Properties?
I am trying to store objects in google apps script properties.
Lets say I have an object: var myObject = {var1:"stuffval", var2:"stuff2val"};
If I store this as a property via scriptProperties.setProperty("myProperty", myObject );
the property is stored as a string that is {var1=stuffval, var2=stuff2val}
.
How can I retrieve my object from that string within Google Apps Script?
Answer
Convert the object to a string before putting it into Properties Service. All of the Properties Services store the data as a string. Properties Service will automatically convert non-strings to strings before storing the data, but with an object you should use the JSON
service to correctly convert the object to a string. Then convert the object as a string back to a real object with JSON.parse(theObject)
var myObject = {var1:"stuffval", var2:"stuff2val"};//Example - object literal
PropertiesService.getScriptProperties()
.setProperty("myProperty", JSON.stringify(myObject) );//stringify the object
Convert back to an object:
var returnedObj = PropertiesService.getScriptProperties("myProperty");
returnedObj = JSON.parse(returnedObj);
Don't use scriptProperties
it's deprecated.
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM