0% found this document useful (0 votes)
60 views2 pages

Fetch and Insert Google Calendar Events Into Spreadsheet

This document provides instructions on how to import Google Calendar events into a Google Sheet using Apps Script. It includes a script that fetches calendar events between set dates and inserts them into the active sheet with headers for date, day of week, title, start time, end time, and location. The instructions explain how to open the Apps Script editor in Sheets, copy the provided script, run it to authorize access to Calendar and Sheets, and see the calendar events populated in the spreadsheet.

Uploaded by

Emerson Galano
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views2 pages

Fetch and Insert Google Calendar Events Into Spreadsheet

This document provides instructions on how to import Google Calendar events into a Google Sheet using Apps Script. It includes a script that fetches calendar events between set dates and inserts them into the active sheet with headers for date, day of week, title, start time, end time, and location. The instructions explain how to open the Apps Script editor in Sheets, copy the provided script, run it to authorize access to Calendar and Sheets, and see the calendar events populated in the spreadsheet.

Uploaded by

Emerson Galano
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

TIKTOK TUTORIAL - Watch this first

1. Open Google Sheets and select or create the spreadsheet where you wish to import
your calendar events.
2. Click on "Extensions" > "Apps Script" in the Google Sheets menu to open the Apps
Script editor.
3. Copy the following script into the Apps Script editor:

/**
* Function to fetch calendar events and insert into active spreadsheet.
*/
function fetchAndInsertCalendarEvents() {

try {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const headers = ['Date of the event', 'Day of the week of the event', 'Title', 'Start Time of the
Events', 'End Time of the Events', 'Location of the Event'];
sheet.getRange('A1:F1').setValues([headers]);

const startDate = new Date(2023, 4, 29);


const endDate = new Date(2023, 5, 12);
const calendar = CalendarApp.getDefaultCalendar();
const events = calendar.getEvents(startDate, endDate);
const daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',
'Saturday'];

for (let i = 0; i < events.length; i++) {


const event = events[i];
const date = event.getStartTime();
const dayOfWeek = daysOfWeek[date.getDay()];
const title = event.getTitle();
const startTime = Utilities.formatDate(event.getStartTime(), Session.getScriptTimeZone(),
'HH:mm');
const endTime = Utilities.formatDate(event.getEndTime(), Session.getScriptTimeZone(),
'HH:mm');
const location = event.getLocation() || 'N/A';
const rowData = [Utilities.formatDate(date, Session.getScriptTimeZone(), 'yyyy-MM-dd'),
dayOfWeek, title, startTime, endTime, location];
sheet.appendRow(rowData);
}
} catch (error) {
Logger.log(`Error: ${error}`);
}
}
Running the Script

1. To run the script, save your changes in the Apps Script editor and then click on the
play/run button.
2. You may need to authorize the script to access your Google Calendar and Google
Spreadsheet. Follow the prompts to do this.
3. Once the script has run successfully, you should see your Google Calendar events
populated into your spreadsheet.

You might also like