Skip to content

Instantly share code, notes, and snippets.

@chriskyfung
Created May 6, 2024 09:19
Show Gist options
  • Save chriskyfung/8e67548528a3d03bd8bc8855a6075e34 to your computer and use it in GitHub Desktop.
Save chriskyfung/8e67548528a3d03bd8bc8855a6075e34 to your computer and use it in GitHub Desktop.
This Google Apps Script automatically searches a calendar you specify for events within the last year that contains specific text and writes their details to a Google Spreadsheet. You can further modify and extend this script as needed.
function importCalendarEventsToSheet() {
//Set the calendar ID and text to search for
var calendarId = 'your_calendar_id@group.calendar.google.com';
var searchText = 'your_search_text';
// Get calendar events
var events = getCalendarEvents(calendarId, searchText);
//Write event data to the spreadsheet
writeEventsToSheet(events);
}
function getCalendarEvents(calendarId, searchText) {
var calendar = CalendarApp.getCalendarById(calendarId);
var now = new Date();
var oneYearAgo = new Date(now.getFullYear() -1 , now.getMonth(), now.getDate());
var events = calendar.getEvents(oneYearAgo, now, {search: searchText});
return events;
}
function writeEventsToSheet(events) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
sheet.clear();
//Write title row
sheet.getRange(1, 1).setValue('Event name');
sheet.getRange(1, 2).setValue('Start time');
sheet.getRange(1, 3).setValue('End time');
sheet.getRange(1, 4).setValue('Description');
//Write event data
for (var i = 0; i < events.length; i++) {
sheet.getRange(i + 2, 1).setValue(events[i].getTitle());
sheet.getRange(i + 2, 2).setValue(events[i].getStartTime());
sheet.getRange(i + 2, 3).setValue(events[i].getEndTime());
sheet.getRange(i + 2, 4).setValue(events[i].getDescription());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment