The Need for Calendar Analytics
Google Calendar is excellent for scheduling, but it is terrible for analytics. If a consulting firm wants to calculate exactly how many hours its employees spent in “Client Meetings” versus “Internal Reviews” over the past month, they cannot easily extract that data from the Calendar interface. To perform historical analysis, calculate billable hours, or generate timesheets, the calendar data must be extracted into a structured database.
Using Google Apps Script, you can build an automated bridge between Google Calendar and Google Sheets. This script will query your calendar, extract the details of every event within a specific timeframe (Title, Start Time, End Time, Description), and log them cleanly into a spreadsheet.
Step 1: Set Up the Google Sheet
The script needs a structured destination to dump the data.
- Create a new Google Sheet.
- Name the first tab “CalendarData”.
- In Row 1, create your headers:
- A1: Event Title
- B1: Start Date/Time
- C1: End Date/Time
- D1: Duration (Hours)
- E1: Location/Link
Step 2: Write the Apps Script
Now we will write the code to fetch the calendar data.
- In the Google Sheet, click on Extensions > Apps Script.
- Delete the default code and paste the following JavaScript:
function exportCalendarToSheet() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("CalendarData");
// Clear any old data below the headers to prevent duplicates on rerun
var lastRow = sheet.getLastRow();
if(lastRow > 1) {
sheet.getRange(2, 1, lastRow - 1, 5).clearContent();
}
// Get the default calendar for the logged-in user
var calendar = CalendarApp.getDefaultCalendar();
// Define the date range (e.g., the last 30 days)
var today = new Date();
var thirtyDaysAgo = new Date();
thirtyDaysAgo.setDate(today.getDate() - 30);
// Fetch all events within this date range
var events = calendar.getEvents(thirtyDaysAgo, today);
// Prepare an array to hold the data before writing to the sheet (faster performance)
var exportData = [];
// Loop through every event found
for (var i = 0; i < events.length; i++) {
var event = events[i];
var title = event.getTitle();
var startTime = event.getStartTime();
var endTime = event.getEndTime();
var location = event.getLocation();
// Calculate the duration in hours
var durationMs = endTime.getTime() - startTime.getTime();
var durationHours = durationMs / (1000 * 60 * 60);
// Add this event's data as a new row in our array
exportData.push([title, startTime, endTime, durationHours, location]);
}
// If we found events, write the entire array to the sheet in one batch
if (exportData.length > 0) {
sheet.getRange(2, 1, exportData.length, exportData[0].length).setValues(exportData);
}
}
Understanding the Code
CalendarApp.getDefaultCalendar()connects to the primary calendar of whoever runs the script. (You can also fetch shared calendars by usingCalendarApp.getCalendarById('[email protected]')).- The script calculates a 30-day window. You can adjust this to pull future events (e.g., the next 7 days).
- It uses a batch operation (
setValues) at the very end to write all the data at once, which is significantly faster than writing row-by-row in Google Sheets.
Click the Save (floppy disk) icon.
Step 3: Run and Authorize
To test the script:
- Click the Run button at the top of the editor.
- Google will block the script and demand authorization because it is attempting to read your private calendar data.
- Click Review Permissions, select your Google Account, click Advanced, and click Go to script.
- Click Allow.
Return to your Google Sheet. It should now be perfectly populated with the last 30 days of your calendar history, including a mathematically precise calculation of the hours spent in each meeting.
Step 4: Automate the Sync
To ensure your spreadsheet is always up to date, you can configure the script to run automatically every night.
- In the Apps Script editor, click the Clock icon (Triggers) on the left sidebar.
- Click + Add Trigger in the bottom right.
- Choose the function
exportCalendarToSheet. - Set the event source to Time-driven.
- Set the type of time based trigger to Day timer.
- Select Midnight to 1am.
- Click Save.
Conclusion
By treating Google Calendar as a raw data source and piping it into Google Sheets via Apps Script, you unlock profound analytics capabilities. You can now build PivotTables, generate automated invoicing based on meeting durations, and visualize exactly where your company’s time is being spent.