Google app script property service getProperties () returns value as undefined
I have a key value map with JSON in it
var prop = {
0 : [{"start":0, "end":10}, {"start":15, "end" : 30}],
1 : [{"start":3, "end":11}],
2 : [{"start":6, "end":9},{"start":9,"end" :17},{"start":32,"end":39}],
4 : [],
5 : [{"start":19,"end":27}]
};
I wanted to store this object in PropertiesService
userProperties
, so I backed up the values first (because it PropertiesService
only allows string values) and then saved it.
for(var i in prop){
prop[i] = JSON.stringify(prop[i]);
}
var userProperties = PropertiesService.getUserProperties();
userProperties.setProperties(prop, true);
Later when I return the property using the method Properties.getProperties()
, it returns the property object.
var userProperties = PropertiesService.getUserProperties();
var pro = userProperties.getProperties();
The problem is Logger.log(pro)
showing me an object prop
, but when I try to access property values using pro[index]
as stated in the documentation it returns undefined
.
When I try to access the value with userProperties.getProperty(i)
, it returns the value to me.
My question is why can't I access properties like this:
for(var i in pro){
Logger.log(pro[i]); // returns undefined
}
but this code below works
for(var i in pro){
Logger.log(userProperties.getProperty(i)); //successfully returns value
}
source to share
I found the answer, the key problem, PropertiesService
only allow keys and values to be of type String
.
var prop = {
"p0" : [{"start":0, "end":10}, {"start":15, "end" : 30}],
"p1" : [{"start":3, "end":11}],
"p2" : [{"start":6, "end":9},{"start":9,"end" :17},{"start":32,"end":39}],
"p4" : [],
"p5" : [{"start":19,"end":27}]
};
and now when I accessed the values with keys it worked
var userProperties = PropertiesService.getUserProperties();
var pro = userProperties.getProperties();
for(var i in pro){
Logger.log(pro[i]); // returns value successfully
}
so keys and values must be of string type
source to share