How to save a 2D array to a Script Property in Properties Service in Google App Script?

∥☆過路亽.° 提交于 2021-02-04 16:22:26

问题


I have a 2D array that I need to save as a property in Google App Script. How would I do this?

var array = [['value 1', 1.0, 'A'],['value 2', 2.0, 'B'],['value 3', 3.0, 'C']]

PropertiesService.getScriptProperties().setProperty('myArray', array)

When I run the code as listed above I get [Ljava.lang.Object;@40ac055f as the value.

When I us array.toString() the property value negates the square brackets.

Thanks in advance!


回答1:


  • Note that Properties Store is meant to save specific properties and key values. It's not meant to be a replacement for the entire spreadsheet.

  • Current quotas1 limit the property value size per key to 9kB and total storage per store to 500kB.

  • If your requirement doesn't cross the limit described above, the easy way is to use JSON.stringify2, Which will convert it to a parsable string.

    var array = [['value 1', 1.0, 'A'],['value 2', 2.0, 'B'],['value 3', 3.0, 'C']];
    var jarray = JSON.stringify(array);
    PropertiesService.getScriptProperties().setProperty('myArray', jarray);
    
  • You can then use JSON.parse3 to retrieve the array

    var array = JSON.parse(jarray);
    


来源:https://stackoverflow.com/questions/53260460/how-to-save-a-2d-array-to-a-script-property-in-properties-service-in-google-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!