1

Sometimes JavaScript is playing with me (although the deal was that I would be playing with it...) This test code below keeps resisting so I'm looking for a little help from more clever people around here.

Answering to a recent question I tried to create a readable list of all the color IDs useable in Google Advanced Calendar API.

The request is very simple : Calendar.Colors.get()

The response is an object with a couple of properties, each one being other objects with other properties.

I can go down to the second level but the last -and most useful in this case - level returns a disturbing "undefined" (see partial log below)

And that's my question...

code with comments :

function getColorList(){
  var colors = Calendar.Colors.get();
  //Logger.log(JSON.stringify(colors));
  for(var cat in colors){
    Logger.log("category "+cat+" = "+JSON.stringify(colors[cat])+'\n\n')
  }
  // from there I try the "event" category
  var events = colors["event"];
  Logger.log('object colors["event"] = '+ JSON.stringify(events))
  // then I try to get every properties in this object
  for(var val in events){
    Logger.log("key "+val+" = "+JSON.stringify(events[val]))
  }
}

enter image description here

Full log is viewable here (externalized to keep this reasonably short)

Community
  • 1
  • 1
Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • 1
    In the debug window I can see an `events` object, (Underneath the colors object) with 11 properties, but when the values are logged, they are all undefined. So, I guess you are asking why they show up in the debug window, but can't be accessed with code? I don't know. I can only assume that "they" don't want anyone to be able to access those properties. Maybe it's a security thing? I don't know. There is an `Events` object: `var events = Calendar.Events.get(calendarId, eventId)` – Alan Wells Feb 20 '16 at 03:19

1 Answers1

2

Looks like (key) may be indicating a read-only definition as Sandy was eluding to. Just make your own object from colors to loop through after converting it to string:

var json = JSON.stringify(colors["event"]);
var myObj = JSON.parse(json);

for(var val in myObj){
  Logger.log("key "+ val +" = "+JSON.stringify(myObj[val]))
}
Bryan P
  • 5,031
  • 3
  • 30
  • 44
  • This works perfectly but do you have an idea why we have to rebuild the objet to let it recover it's object nature ? – Serge insas Feb 20 '16 at 10:23
  • not for sure, i would try to figure out what (key) actually is in the docs - a number, string, object or is color definition the type? `writable:false` can also be set on objects properties, but don't think that would prevent reading it. maybe tag question with javascript and google-calendar for more visibility – Bryan P Feb 20 '16 at 11:14
  • Thank you for this interesting comments, the API documentation seems to confirm it is an object since creating events with parameters takes objects as arguments... I'll follow your advice and query the JavaScript forum ;) – Serge insas Feb 20 '16 at 14:45