I need help with this script I use to add data from a Sheet to a Google Calendar. I admit, I found it online, and I edited it to fit my sheet. It would work perfectly, the only problem is the sheet is populated from data entered in a JotForm that we use at my job for people who need to notify us of upcoming events that we need to provide support for.
That being said, not everyone is careful enough to make sure that their event End Time is after their event Start Time. This causes the code to break and not execute due to the incorrect times (you cannot add an event to Google Calendar if your End Time is before your Start Time.
Here is the code I have so far:
function createCalendarEvent() {
var sheet = SpreadsheetApp.getActiveSheet();
var calendar = CalendarApp.getCalendarById('CalendarIDHere');
var startRow = 2; // First row of data to process - 2 exempts my header row
var numRows = sheet.getLastRow(); // Number of rows to process
var numColumns = sheet.getLastColumn();
var dataRange = sheet.getRange(startRow, 1, numRows-1, numColumns);
var data = dataRange.getValues();
var complete = "Done";
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var name = row[1]; //Event Name
var sDate = new Date(row[2]); //Start date/time
var eDate = new Date(row[3]); //End date/time
var attendees = row[4]; //Number of Guests
var location = row[5]; //Event Location
var organizer = row[6]; //Event Organizer
var contact = row[7]; //Organizer e-mail
var department = row[8]; //Department
var setup = row[9]; //Room Set-up Needed
var config = row[10]; //Room Configuration Requested
var rSetup = row[11]; //Details about Room Set-up
var food = row[12]; //Food Services Needed
var additional = row[13]; //Additional Info
var eventID = row[14]; //event marked Done
if (eventID != complete) {
var currentCell = sheet.getRange(startRow + i, numColumns);
calendar.createEvent(name, sDate, eDate,
{
description: 'Department: ' + department + '\r' + 'Organizer: ' + organizer + '\r' + 'Organizer E-mail: ' + contact + '\r' + '\r' + 'Attendees: ' + attendees + '\r' + 'Room Configuration Type: ' + config + '\r' + '\r' + 'Room Set Up, Security, Cleaning details: ' + '\r' + rSetup + '\r' + '\r' + 'Food Services?: ' + food + '\r' + '\r' + 'Additional Information: ' + additional
});
currentCell.setValue(complete);
}
}
}
What can I add to this that will cause the script to check the data and skip any rows that have date/time in the eDate cell before the date/time in the sDate cell?
