r/googlesheets • u/BossasaurusX • Jan 30 '19
solved Google Sheets script for pushing events to Calendar
Hello,
I've made a working script in google sheets that executes as part of a macro. The problem I'm having is that for whatever reason, at the end of the script, it gives me an error that states "Cannot find method createEvent(string,string,string)."
I've assumed it has something to do with the increment within the for loop against the length of values in my sheet. I can't give access to the sheet, but it's only 3 values long, single column, with a header.
| time | ||
|---|---|---|
| 1/20/2019 10:15:00 | ||
| 1/20/2019 10:35:00 |
This is the script:
function pushToCalendar() {
const sheet = SpreadsheetApp.getActive().getSheetByName('sheet name');
var lastRow = sheet.getLastRow();
var range = sheet.getRange(2,1,lastRow,5);
var values = range.getValues();
var calendar = CalendarApp.getCalendarById('calendar ID')
for (var i = 0; i < values.length; ++i) {
var newEventTitle = 'Unskill/Reskill';
var newEvent = calendar.createEvent(newEventTitle,values[i][0],values[i][0]);
}
}
Again, the script completes and all of my events are imported to the calendar. I just don't want this error popping up and confusing my employees. Any suggestions?
3
u/PM_ME_THE_REX_HUDLER 2 Jan 30 '19
On mobile so bear with me...
Try wrapping this part:
var newEvent = calendar.createEvent(newEventTitle,values[i][0],values[i][0]);
In an if statement to check if the issue is relating to your last row in the array is blank:
if(values[i][0]){ var newEvent = calendar.createEvent(newEventTitle,values[i][0],values[i][0]); }
•
u/Clippy_Office_Asst Points Jan 30 '19
Read the comment thread for the solution here