Ilya Perelman
Ilya Perelman

Reputation: 45

How to pass array of objects as global variable in Google Script

I have an array of objects that I have passed from JS to GS. Here is an example of how the array of object may look like (changes depending on user input)

var playersArray = 
[
{number=1, 
role=Arsonist, 
deathStatus=false, 
guardianTargetStatus=false, 
executionerTargetStatus=false, 
roleblockStatus=false}, 

{number=2, 
role=Arsonist, 
deathStatus=false, 
guardianTargetStatus=false, 
executionerTargetStatus=false, 
roleblockStatus=false}, 

{number=3, role=Arsonist, 
deathStatus=false, 
guardianTargetStatus=false, 
executionerTargetStatus=false, 
roleblockStatus=false}
]

Throughout my code I need to call some data from this array. For example

var roleList = playersArray.map(function(role) {return role.role;});
Browser.msgBox(playersArray[1]["role"]);

When I pass this playersArray directly between functions, everything worls fine. Unfortunately for one of my functions I need to pull this array without passing it directly.

I have therefore set playerArray as a Property within GS

PropertiesService.getScriptProperties().setProperty('playersArray', JSON.stringify(playersArray)); 

I am then using getProperty inside relevant function to get this array:

var playersArray = PropertiesService.getScriptProperties().getProperty('playersArray'); 
  playersArray = playersArray.replace(/\"/g, "").replace(/\:/g, "=");
  playersArray = playersArray.split(",");

For some reason the function cannot see the array properly when I do that:

var roleList = playersArray.map(function(role) {return role.role;});
Browser.msgBox(playersArray[1]["role"]);

Please help me solve this. If the solution I am using for passing the array as global is not viable, I am happy to see other solutions. But it has to be global because I cannot pass the array directly between functions.

Upvotes: 1

Views: 720

Answers (1)

Tanaike
Tanaike

Reputation: 201358

  • You want to put the array object to PropertiesService.
  • When you retrieve the array object from PropertiesService, you want to use playersArray as an array object.

If my understanding is correct, how about this modification?

From:

var playersArray = PropertiesService.getScriptProperties().getProperty('playersArray');
playersArray = playersArray.replace(/\"/g, "").replace(/\:/g, "=");
playersArray = playersArray.split(",");

To:

var playersArray = PropertiesService.getScriptProperties().getProperty('playersArray');
playersArray = JSON.parse(playersArray);

Note:

  • Please be careful the quotas of PropertiesService.
    • Properties value size is 9kB / val
    • Properties total storage is 500kB / property store
  • I thought that in your script, playersArray might be like var playersArray = [{number: 1, role: "Arsonist", deathStatus: false, guardianTargetStatus: false, executionerTargetStatus: false, roleblockStatus: false},,,]. Because before the object is put to PropertiesService, Browser.msgBox(playersArray[1]["role"]) works. And when you see playersArray by Logger.log(playersArray), you might see like var playersArray = [{number=1, role=Arsonist, deathStatus=false, guardianTargetStatus=false, executionerTargetStatus=false, roleblockStatus=false},,,]. When playersArray is object, when the object is seen by Logger.log(playersArray), : is converted to =. I think that this is the specification of Google Apps Script. So the object put to PropertiesService using JSON.stringify() can be converted to the object using JSON.parse().

References:

If I misunderstood your question, I apologize.

Upvotes: 1

Related Questions