first a few errors in your code...
- You update row[3] but forgot to update data with its corresponding row value, I simplified by using only data as variable.
- you used
row[3]
for third column, it isrow[2]
(nowdata[i][2]
) - You updated the sheet at a wrong place in the loop
The working code is below (I changed the cal ID to one I own for test purpose... restore yours of course)
As for the time limit, you should proceed by limited batch of events, take a look at the execution transcript to see how long it takes for 1 and multiply...to get a value well below 5 minutes which is the maximum execution time in Google Script. Then you should keep somewhere the value of the row index (i) and using a time trigger proceed with the next batch starting at this index.
The documentation on trigger creation will help you and the scriptProperties is the ideal place to keep the index value in a key/value schema. See doc as well.
If you have issues with that don't hesitate to come back with a new question .
function exportEvents() {
var sheet = SpreadsheetApp.getActiveSheet();
var headerRows = 1; // Number of rows of header info (to skip)
var range = sheet.getDataRange();
var data = range.getValues();
var calId = "h22nevo15tm0nojb6ul4hu7ft8@group.calendar.google.com";
var cal = CalendarApp.getCalendarById(calId);
for (i in data) {
if (i < headerRows) continue; // Skip header row(s)
var date = new Date(data[i][0]); // First column
var title = data[i][1]; // Second column
var id = data[i][2]; // third column == eventId
// Check if event already exists, update it if it does
try {
var event = cal.getEventSeriesById(id);
}
catch (e) {
// do nothing - we just want to avoid the exception when event doesn't exist
}
if (!event) {
Logger.log(event);
var newEvent = cal.createAllDayEvent(title, date);
data[i][2] = newEvent.getId(); // Update the data array with event ID
}
else {
event.setTitle(title);
}
}
Logger.log(data);
sheet.getRange(1,1,data.length,data[0].length).setValues(data);
}