Last active
October 18, 2017 22:39
-
-
Save ex-hota911/93585cd18531d67d6995146ca67ae516 to your computer and use it in GitHub Desktop.
Add events to Google Calendar listed in Google Spreadsheet.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | 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("✓"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to run the script.