Reputation: 87
I am trying to create a calendar with a specific name and summary and get the id Google is assigning to it programmatically.
I know how to do it manually, I just want to have a way to create the calendar and later post events referring to it by id.
Using the script below, as shown in CalendarApp documentation, provides a number of calendars containing the text 'US Holidays'.
// Gets the public calendar named "US Holidays".
var calendars = CalendarApp.getCalendarsByName('US Holidays');
Logger.log('Found %s matching calendars.', calendars.length);
I have replaced:
Logger.log('Found %s matching calendars.', calendars.length);
With:
Logger.log('Found %s matching calendars.', calendars.getId());
But I get:
TypeError: Cannot find function getId in object Calendar. (line 22, file "")
Upvotes: 1
Views: 265
Reputation: 45720
Remember that getCalendarsByName()
returns an array; that's why calendars
had a length
property. The getId()
method applies to an individual calendar object, e.g. calendars[0].getId().
Tip: When you're writing code in the Google Apps Script editor, watch for and take advantage of the auto-completion feature. For instance, starting with the example you've shown, position your cursor just before the dot following calendars
on the second line. If you now press ., autocomplete will pop up a list of properties and methods that apply to the object before the .
. See the video below: Since calendars
does not have a getId()
method, it will not be one of the items in the list, which is a good clue. (Those are all Array methods and properties.)
function myFunction() {
// Gets the public calendar named "US Holidays".
var calendars = CalendarApp.getCalendarsByName('US Holidays');
Logger.log('Found %s matching calendars.', calendars.length.toString() );
for (var i=0; i<calendars.length; i++) {
Logger.log('calendar[%s] id: %s.', i.toString(), calendars[i].getId() );
}
}
[15-10-16 14:46:58:664 EDT] Found 1 matching calendars.
[15-10-16 14:46:58:666 EDT] calendar[0] id: en.canadian#[email protected].
(You might notice that I didn't actually test with the US Holiday calendar!)
Upvotes: 2