【问题标题】:How can I store and retrieve objects from Google Apps Script Project Properties?如何从 Google Apps 脚本项目属性中存储和检索对象?
【发布时间】:2015-12-16 00:37:00
【问题描述】:

我正在尝试将对象存储在 Google Apps 脚本属性中。

假设我有一个对象:var myObject = {var1:"stuffval", var2:"stuff2val"};

如果我通过scriptProperties.setProperty("myProperty", myObject ); 将其存储为属性,则该属性将存储为{var1=stuffval, var2=stuff2val} 的字符串。

如何在 Google Apps 脚本中从该字符串中检索我的对象?

【问题讨论】:

    标签: javascript google-apps-script


    【解决方案1】:

    在将对象放入属性服务之前将其转换为字符串。所有属性服务都将数据存储为字符串。属性服务会在存储数据之前自动将非字符串转换为字符串,但对于对象,您应该使用JSON 服务将对象正确转换为字符串。然后使用JSON.parse(theObject)将对象作为字符串转换回真实对象

    var myObject = {var1:"stuffval", var2:"stuff2val"};//Example - object literal
    
    PropertiesService.getScriptProperties()
      .setProperty("myProperty", JSON.stringify(myObject) );//stringify the object
    

    转换回对象:

    var returnedObj = PropertiesService.getScriptProperties("myProperty");
    returnedObj = JSON.parse(returnedObj);
    

    不要使用scriptProperties,它已被弃用。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多