Skip to content

Instantly share code, notes, and snippets.

@brunopk
Last active June 3, 2023 02:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunopk/84340d51387a04ade57d484fc0b7e886 to your computer and use it in GitHub Desktop.
Save brunopk/84340d51387a04ade57d484fc0b7e886 to your computer and use it in GitHub Desktop.
Playing with Google Calendar and Apps Script

Google Calendar and Google Apps Script

How to use Google APIs as Google Calendar API in Google Apps Script

Screen Shot 2023-05-02 at 22 09 03

/**
* Perform a full sync to list all calendars and logs the result
*/
function listCalendars() {
let pageToken: string | null | undefined = null
let calendars: calendar_v3.Schema$CalendarList | undefined
do {
calendars = Calendar.CalendarList?.list({pageToken})
for (let i = 0; i < (calendars?.items ? calendars?.items?.length : 0); i++) {
const item = calendars?.items ? calendars?.items[i] : undefined
console.log(`summary: ${item?.summary}, id: ${item?.id}`)
}
pageToken = calendars?.nextPageToken
} while (pageToken != null)
}
/**
* Returns all events in an specific calendar in a given period of time in the past
* @param calendarId calendar id to search events
* @param minutesInThePast start time of events should be no older than this number of minutes (and up to now)
* @returns
*/
function listEvents(calendarId: string, maxHoursInThePast: number) {
let nextPageToken: string | undefined ;
let now = new Date();
let result : calendar_v3.Schema$Event[] = []
do{
let page = Calendar.Events?.list(
calendarId,
{
timeMin: minusHours(now, maxHoursInThePast).toISOString(),
timeMax: now.toISOString(),
});
if(page?.items && page.items.length > 0){
for(var i = 0; i< page.items.length ; i++){
result.push(page?.items[i] as calendar_v3.Schema$Event)
}
}
nextPageToken = page?.nextPageToken;
}while(nextPageToken)
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment