1

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.

1 Answer 1

1
  • 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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! This solves my problem. I have spent 4 days trying to go around it and the solution was so close :)
@Ilya Perelman Thank you for replying. I'm glad your issue was resolved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.