最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I store and retrieve objects from Google Apps Script Project Properties? - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question asked Dec 16, 2015 at 0:37 Douglas GaskellDouglas Gaskell 10.1k12 gold badges80 silver badges135 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 16

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)

  1. 暂无评论