Skip to content

Instantly share code, notes, and snippets.

@ChaiyachetU
Last active May 8, 2020 04:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChaiyachetU/90717bb6e40dbd0d7e86d2eec44fc301 to your computer and use it in GitHub Desktop.
Save ChaiyachetU/90717bb6e40dbd0d7e86d2eec44fc301 to your computer and use it in GitHub Desktop.
//delete and add events to calendar
function addEvents() {
//open the event calendar
var calenDar = CalendarApp.getCalendarById("Your Calendar ID");
var spreadSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
//pull information into the code, in a form that the code can understand
var lastRow = spreadSheet.getLastRow();
var data = spreadSheet.getRange("A2:E" + lastRow).getValues();
/**
[
[event-1, start time-1, end time-1, location-1, description-1],
[event-2, start time-2, end time-2, location-2, description-2],
..............................................................,
[event-n, start time-n, end time-n, location-n, description-n]
]
**/
//delete events
var lastDateOfYear = new Date(new Date().getFullYear(), 11, 31); // JavaScript counts months from 0 to 11. January is 0. December is 11.
var events = calenDar.getEvents(new Date(), lastDateOfYear); // new Date() = today
for (var i = 0; i < events.length; i++) {
var eventsDelete = events[i];
eventsDelete.deleteEvent();
Utilities.sleep(100);
}
//add events
for(var i = 0; i < data.length; i++) {
var title = data[i][0];
var startTime = data[i][1];
var endTime = data[i][2];
var location = data[i][3];
var description = data[i][4];
calenDar.createEvent(title, startTime, endTime, {location: location, description: description}); //(Title, Start Time, End Time, {options})
Utilities.sleep(100);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment