r/GoogleAppsScript • u/frederick_beer_guy • Aug 15 '19
Help With Calendar Sync From Spreadsheet
I am a complete novice at writing Google app scripts but have completed the following via some YouTube exploration:
function addEvents() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lr = ss.getLastRow();
var cal = CalendarApp.getCalendarById("****");
var data = ss.getRange("B2:G" + lr).getValues();
var complete = "Done";
for(var i = 0;i<data.length;i++){
if (data[i][0] == "") {
break
}
cal.createEvent(data[i][0], data[i][3], data[i][4], {location: data[i][1], description: data[i][2]});
}
for(var i = 0;i<data.length;i++){
var dataRange = ss.getRange(2, 1, data.length, 7)
var data = dataRange.getValues();
if (data[i][5]!= 'ADDED_EVENT') { // Prevents adding duplicates
ss.getRange(2 + i, 7).setValue("ADDED_EVENT"); //Set status "ADDED"
}
}
}
Everything is working except I am having issues with events duplicating on the calendar and would like to prevent this. The script is currently triggered by a form submission and I'd like to use this to allow members of our organization to populate a calendar via the form.
Any help is greatly appreciated.
1
u/PrzeOr82 Aug 16 '19
Duplicates are due to submiting same event twice or by not setting the Event Added flag?
1
u/frederick_beer_guy Aug 16 '19
It appears that the script would read the entire spreadsheet and was not coded properly to read and skip the items including the "EVENT_ADDED" piece.
1
1
u/momotron2000 Aug 15 '19
Wow I didn't even know this is possible! Can someone help him so I can copy this script please?
1
u/frederick_beer_guy Aug 16 '19
Check out the comment above from /u/YunJaFei. His video is excellent, thorough, and produces a working result.
3
u/YunJiaFei Aug 16 '19
Please check my video about this topic.
https://www.youtube.com/watch?v=l63JyzJv-Yo