0

I am trying to store a list of strings as a global variable and be able to append to it in different scripts. According to this this answer one way of achieving the same result is with a PropertyService and Properties. I have defined a global document properties variable and can get and set the properties in it. However, if I set a key to hold an array, when I next get the properties, it does not act like an array.

MWE:

var ps = PropertiesService.getDocumentProperties();

function propTest() {
  var d = ps.getProperties();
  if ("a" in d){
    // a is already defined
    var list = d.a;
    list.push("Hello");  // This causes TypeError: Cannot find function push in object [Lnumber;@57220e4c.
//    d["a"].push("Hello"); // This line gives exactly the same error
  } else {
    // a has not yet been defined
    var a = [];
    a.push("Hello");  // Works fin as an array before being set in the Properties.
  }
  d.a = a;
  ps.setProperties(d)
}

function reset(){
  ps.deleteAllProperties();
}

Any ideas on how to solve this?

Kajsa
  • 409
  • 6
  • 16

1 Answers1

1

After a bit more examination, I realised that the values are stored as strings.

Converting with JSON like this solved the problem

var ps = PropertiesService.getDocumentProperties();

function propTest() {
  var d = ps.getProperties();
  if ("a" in d){
    var a = JSON.parse(d.a); // <- parsing
    a.push("Hello")
  } else {
    var a = [];
    a.push("Hello");
  }
  d.a = JSON.stringify(a);   // <- stringifying
  ps.setProperties(d)
}

function reset(){
  ps.deleteAllProperties();
}
Kajsa
  • 409
  • 6
  • 16