Skip to content

Instantly share code, notes, and snippets.

@ScriptRaccoon
Last active August 12, 2023 07:50
Show Gist options
  • Save ScriptRaccoon/011a3e3afc9a0a799868b8425f39a6a5 to your computer and use it in GitHub Desktop.
Save ScriptRaccoon/011a3e3afc9a0a799868b8425f39a6a5 to your computer and use it in GitHub Desktop.
Google Apps Script that copies Google calendar entries to a Google sheet
/*
This Google script needs to be added to a Google spreadsheet.
It lists all calendar entries within one year (that time range is configurable)
and adds them to the first sheet in the spreadsheet. The script also adds
a button 'Update' in the spreadsheet's UI that executes the main update function.
*/
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const sheet = spreadsheet.getSheets()[0];
// one year in ms
const time_range = 365 * 24 * 60 * 60 * 1000;
// number of rows of table header, untouched by script
const offset = 1;
function update_events() {
sheet.getRange(1 + offset,1,sheet.getLastRow(),sheet.getLastColumn()).clearContent();
const now = new Date();
const in_one_year = new Date(now.getTime() + time_range);
const events = CalendarApp.getEvents(now,in_one_year);
if (events.length === 0) return;
const table_data = [];
for (let i = 0; i < events.length; i++) {
const event = events[i];
const title = event.getTitle();
const description = event.getDescription();
const start_time = event.getStartTime().toLocaleString();
const end_time = event.getEndTime().toLocaleString();
const location = event.getLocation();
const event_data = [title, description, start_time, end_time, location];
table_data.push(event_data);
}
const range = sheet.getRange(1 + offset,1,events.length,table_data[0].length);
range.setValues(table_data);
}
function onOpen() {
SpreadsheetApp.getUi()
.createMenu("Actions")
.addItem("Update", "update_events")
.addToUi();
}
/*
appsscript.json
*/
/*
{
"timeZone": "Europe/Berlin",
"dependencies": {
},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8",
"oauthScopes": [
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/calendar.readonly"
]
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment