Last active
June 3, 2023 02:40
-
-
Save brunopk/84340d51387a04ade57d484fc0b7e886 to your computer and use it in GitHub Desktop.
Playing with Google Calendar and Apps Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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