Acquisition of calendar
Google Apps Script has a standard CalendarApp
class, which is a library for operating Google Calendar.
This section describes how to use the CalendarApp
class to get the CalendarApp.Calendar
object, which is the starting point of various operations.
Since GAS will access Google Calendar, you will need to set the authority at the first run.
Outline of functions introduced on this page
If you want to know only the functions to acquire, you can see the sample of the function by selecting the target tab from the tablist.
- All calendars (no creator)
- All calendars (creators are you)
- Specify ID
- Specify the name
/** @type { CalendarApp.Calendar[] } */
const calendars = CalendarApp.getAllCalendars();
/** @type { CalendarApp.Calendar[] } */
const calendars = CalendarApp.getAllOwnedCalendars();
const CALENDAR_ID = "___The ID of the calendar to get___"
/** @type { CalendarApp.Calendar | null } */
const calendar = CalendarApp.getCalendarById(CALENDAR_ID);
const CALENDAR_NAME = "TODO list"
/** @type { CalendarApp.Calendar[] } */
const calendars = CalendarApp.getCalendarsByName(CALENDAR_NAME);
Get all calendars regardless of the creator
The Google Calendar displays both the calendar you have created and the calendar you have created and participated.
If you want to get all of this, use the CalendarApp.getAllCalendars
function.
Function to use
/**
* @return { CalendarApp.Calendar[] } All calendars to which the target user belongs
*/
CalendarApp.getAllCalendars;
Sample code
/** All calendars */
const calendars = CalendarApp.getAllCalendars();
console.log(`got ${calendars.length} calendars`);
for (const calendar of calendars) {
console.log(`The calendar name is "${calendar.getName()}"`);
}
Get all the calendars you created
If you want to get all calendars, excluding the calendar you have created and participated by other users, use the CalendarApp.getAllOwnedCalendars
function.
Function to use
/**
* @return { CalendarApp.Calendar[] } All calendars I created
*/
CalendarApp.getAllOwnedCalendars;
Sample code
/** All calendars */
const calendars = CalendarApp.getAllOwnedCalendars();
console.log(`got ${calendars.length} calendars`);
for (const calendar of calendars) {
console.log(`The calendar name is "${calendar.getName()}"`);
}
Get by specifying the ID
It is possible to obtain a specific one calendar by specifying the ID.
CalendarApp.getCalendarById
Uses function.
Function to use
/**
* @return { CalendarApp.Calendar | null } Target calendar
*/
CalendarApp.getCalendarById;
If the target ID does not exist, null
will be returned.
Sample code
/** Calendar ID */
const CALENDAR_ID = '___The ID of the calendar to get___';
/** Target calendar */
const calendar = CalendarApp.getCalendarById(CALENDAR_ID);
if (!calendar) {
console.log('Calendar not found');
return;
}
console.log(`The calendar name is "${calendar.getName()}"`);
Get by specifying the name
It is possible to obtain a specific calendar by specifying the name.
Use CalendarApp.getCalendarsByName
function.
Function to use
/**
* @return { CalendarApp.Calendar[] } Target calendar
*/
CalendarApp.getCalendarsByName;
Since the calendar name is allowed to overlap, the acquired calendar is an array.
If there are two calendars with the same name, both will be obtained.
Sample code
/** Calendar name */
const CALENDAR_NAME = 'ToDo list';
/** Target calendar */
const calendars = CalendarApp.getCalendarsByName(CALENDAR_NAME);
console.log(`There are ${calendars.length} calendars called ${CALENDAR_NAME}`);