Skip to content

Instantly share code, notes, and snippets.

@ex-hota911
Last active October 18, 2017 22:39
Show Gist options
  • Save ex-hota911/93585cd18531d67d6995146ca67ae516 to your computer and use it in GitHub Desktop.
Save ex-hota911/93585cd18531d67d6995146ca67ae516 to your computer and use it in GitHub Desktop.
Add events to Google Calendar listed in Google Spreadsheet.
/**
* | Start | End | | Calendar | Title | Done |
* |------------|------------|---|-----------|-------|------|
* | 3/26 13:00 | 3/26 14:00 | | | Lunch | |
* |------------|------------|---|-----------|-------|------|
* | 3/26 | | v | Birthdays | Alice | |
*/
var INDEX = {
START: 0,
END: 1,
WHOLE_DAY: 2,
TITLE: 3,
CAL: 4,
DONE: 5,
}
function registerEvents() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// This represents ALL the data
var range = sheet.getDataRange();
var values = range.getValues();
for (var i = 0; i < values.length; i++) {
var row = values[i];
// Find the calendar if the name is specified, otherwise use the default calnder.
var cal = CalendarApp.getCalendarsByName(row[INDEX.CAL])[0] || CalendarApp.getDefaultCalendar();
// Skip if the event is already registered.
if (row[INDEX.DONE] != "") {
continue;
}
var start = new Date(row[INDEX.START]);
var title = row[INDEX.TITLE];
if (row[INDEX.WHOLE_DAY] == "") {
var end = new Date(row[INDEX.END]);
cal.createEvent(title, start, end);
} else {
cal.createAllDayEvent(title, start);
}
// Mark as done.
var done = range.getCell(i + 1, INDEX.DONE + 1);
done.setValue("✓");
}
}
@ex-hota911
Copy link
Author

ex-hota911 commented Mar 25, 2017

How to run the script.

  1. Create a new spreadsheet in Google Docs
  2. Add a table of events in the following format:
Start End All Day Calendar Title Done
3/26 13:00 3/26 14:00 Lunch
3/26 v Birthdays Alice
  1. Click [Tool] > [Script Editor]
  2. Copy-paste this script.
  3. Run the registerEvents function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment