Task guide
Export Calendar events to a Google Sheet
Read events from your Google Calendar for a chosen number of upcoming days and write their titles, times, locations, and guest counts to a Google Sheet tab.
1 · Prepare
Put these pieces in place
- Create a Sheet tab named Calendar Export (the script overwrites its contents each time it runs).
- Sign in as the account whose Google Calendar you want to export.
- Keep the date window short (such as 30 days) so the export finishes well inside the 6-minute limit.
Change these values
- Sheet name
Calendar ExportExisting tab that will be cleared and refilled.
- Days ahead
30Number of days starting from today to include.
2 · Review before running
What the script can access and change
Permissions you approve
- See, edit, share, and permanently delete all the calendars you can access using Google Calendar. The code only reads events from your default calendar.
- See, edit, create, and delete all your Google Sheets spreadsheets. The code clears and rewrites only the Calendar Export tab.
Effects of one run
- Clears all existing values on the Calendar Export tab and writes one header row plus one row per event in the chosen date window.
- Leaves your Google Calendar and every other Sheet tab unchanged.
3 · Copy
Paste this into the Apps Script editor
Create or open a script file, replace the example values marked above, then save the project.
const SHEET_NAME = 'Calendar Export'; // Existing tab that will be cleared and refilled.
const DAYS_AHEAD = 30; // Number of days starting from today to include.
function exportCalendarEventsToSheet() {
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const sheet = spreadsheet.getSheetByName(SHEET_NAME);
if (!sheet) {
throw new Error('Add a Sheet tab named ' + SHEET_NAME + ' before running this function.');
}
const timeZone = spreadsheet.getSpreadsheetTimeZone();
const startDate = new Date();
startDate.setHours(0, 0, 0, 0);
const endDate = new Date(startDate.getTime() + DAYS_AHEAD * 24 * 60 * 60 * 1000);
const calendar = CalendarApp.getDefaultCalendar();
const events = calendar.getEvents(startDate, endDate);
const rows = [['Title', 'Start', 'End', 'All day', 'Location', 'Guests', 'Event ID']];
events.forEach((event) => {
const title = event.getTitle();
const location = event.getLocation();
rows.push([
/^[=+@-]/.test(title) ? "'" + title : title,
Utilities.formatDate(event.getStartTime(), timeZone, 'yyyy-MM-dd HH:mm'),
Utilities.formatDate(event.getEndTime(), timeZone, 'yyyy-MM-dd HH:mm'),
event.isAllDayEvent() ? 'Yes' : 'No',
/^[=+@-]/.test(location) ? "'" + location : location,
String(event.getGuestList().length),
event.getId(),
]);
});
sheet.clearContents();
sheet.getRange(1, 1, rows.length, rows[0].length).setValues(rows);
Logger.log('Exported ' + events.length + ' calendar events to ' + SHEET_NAME + '.');
} 4 · Run
Follow these steps in order
Create the output tab
Add a blank tab named Calendar Export to your Google Sheet so existing data on other tabs is untouched.
Paste the script and save
Choose Extensions, then Apps Script, replace the starter function with this code, and press Ctrl+S or Cmd+S.
Run once and approve access
Select exportCalendarEventsToSheet in the dropdown beside Run, click Run, and approve access to Google Calendar and Google Sheets.
Review the exported table
Switch back to the Calendar Export tab and verify the headers and event rows. Adjust DAYS_AHEAD or swap getDefaultCalendar() for CalendarApp.getCalendarById() if you need a shared calendar.
5 · Confirm
How to tell it worked
The Calendar Export tab shows one row per upcoming event with formatted start and end times, location, guest count, and event ID.
If the result is different
Troubleshooting
- The exported times are shifted by several hours.
- Open File, then Settings in Google Sheets and check the spreadsheet Time zone. The script formats timestamps using the spreadsheet time zone.
- Shared calendar events do not appear.
- getDefaultCalendar() reads only your primary calendar. Replace it with CalendarApp.getCalendarById("your-calendar-id") using the ID from Google Calendar settings.
Technical details
Names used in this script
Open these details when you want to adapt the example or look up an exact service or method name.
Show Apps Script symbols and service pages
Apps Script symbols
Service pages
Keep going
Related tasks, basics, and apps
- Task guide Create Calendar events from Sheet rows
Turn rows on an Events tab into events on your Google Calendar. The script creates an event for each row that has no event ID yet, then writes the new ID back into the row so a later run skips it.
- Task guide Email yourself tomorrow's calendar agenda
Get an email each evening listing tomorrow's events on your main Google Calendar. Run the setup function once to install the daily trigger.
- Task guide Create Calendar events from Form submissions
Schedule a Google Calendar event whenever someone submits a Google Form, using the title, date, and duration from the submission.
- Object reference Calendar
A calendar holds a set of events. Most scripts use your default calendar, but you can also open another calendar you have access to by its ID.
- Object reference Calendar event
A calendar event is one entry on a calendar, such as a meeting or a reminder. It carries a title, a start and end time, and an optional guest list.
- Object reference Sheet
A Sheet is one tab inside a Google spreadsheet. Get it with getSheetByName() or getActiveSheet(). Use getLastRow() to find the last row with content, or getRange() to select cells to read.
- Object reference Range
A Range represents one cell such as A2 or a rectangular block of rows and columns in a Sheet. Use getValue() for one cell and getValues() or setValues() for a 2D array of rows.
- Google app Google Calendar
CalendarApp scripts read events with getEvents() or getEventsForDay(), export schedules to Google Sheets, and create CalendarEvent entries from Sheet rows or Form submissions.
- Google app Google Sheets
SpreadsheetApp lets a Google Sheets script read a Sheet or Range, add a custom menu or formula, and act on each row, such as sending one email per row.
- Diagnostic The number of columns in the data does not match the number of columns in the range
Why every row passed to setValues() must have the same column count as the header row.