I'm having some trouble with my Google Apps Script code trying to have it take the information on one row of my sheet and create a single, Calendar event, with beginning and ending times.
//push new events to calendar
function pushToCalendar() {
//spreadsheet variables
var sheet = SpreadsheetApp.getActiveSheet();
var lastRow = sheet.getLastRow();
var range = sheet.getRange(2,1,lastRow,16);
var values = range.getValues();
var numValues = 0;
for (var i = 0; i < values.length; i++) {
//check to see if Age, Competition, Start Date, Start Time and End Date & Time are filled out
if ((values[i][0].length == '<>') && (values[i][1].length == '<>') && (values[i][4].length == '<>') && (values[i][5].length == '<>') && (values[i][6].length == '<>') && (values[i][7].length == '<>')) {
//check if it's been entered before
if ((values[i][8] == '')||(values[i][8] == null)) {
//create event https://developers.google.com/apps-script/class_calendarapp#createEvent
var newEventTitle = 'Game: ' + values[i][0] + ' - ' + values[i][1];
var startDay = Utilities.formatDate(new Date(values[i][3]), 'America/Chicago', 'MMMM dd, yyyy');
var startTime = Utilities.formatDate(new Date(values[i][4]), 'America/Chicago', 'HH:mm');
var start = startDay + ' ' + startTime;
var endDay = Utilities.formatDate(new Date(values[i][5]), 'America/Chicago', 'MMMM dd, yyyy');
var endTime = Utilities.formatDate(new Date(values[i][6]), 'America/Chicago', 'HH:mm');
var end = endDay+ ' ' + endTime;
var newEvent = CalendarApp.getDefaultCalendar().createEvent(newEventTitle, new Date(start), new Date(end), {location: values[i][12]});
//get ID
var newEventId = newEvent.getId();
//mark as entered, enter ID
sheet.getRange(i+2,8).setValue(newEventId);
} //could edit here with an else statement
}
numValues++;
}
}
I have pertinent information (used in the event title) in the first three columns, and Start Date in column 4, Start Time in column 5, End Date in column 6, and End Time in column 7. The other columns contain information that I'll add to the description of the event.
The problem I'm mostly running into is filtering the data set into events that have not been pushed to the Calendar yet, as well as the script accurately recording once it does push an event over, such that it will be able to filter it on the next cycle of the script.
I modified the code a little but not much. Here it is. I was able to get all six events on the calendar with very few changes.
and here's my test data.
I got it to work. I was referencing the wrong columns initially in the value check line. Silly me.