How can I store and retrieve objects from Google Apps Script Project Properties?

后端 未结 1 1333
后悔当初
后悔当初 2021-02-06 08:49

I am trying to store objects in google apps script properties.

Lets say I have an object: var myObject = {var1:\"stuffval\", var2:\"stuff2val\"};

If

相关标签:
1条回答
  • 2021-02-06 09:24

    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.

    0 讨论(0)
提交回复
热议问题