Delete Mass events (data coming from google sheets) on my calendar

918 views
Skip to first unread message

Laurel McPherson

unread,
Jul 19, 2022, 2:26:18 PM7/19/22
to Google Apps Script Community
I am new to coding and so far I have been able to add mass events from a spreadsheet into a google calendar and also add a menu item that enables me to click the button to run the code.

I am looking to add an additional function that will allow me to delete all the events on my spreadsheet from my calendar (basically to reverse the first code if I need to). I would then enable this function to be accessed through the new menu option as well.

Any ideas on how I should move forward? I have read a bunch of posts regarding deleting mass events from calendars but have hit road blocks everytime. 
code.png

Laurie Nason

unread,
Jul 20, 2022, 3:25:49 AM7/20/22
to google-apps-sc...@googlegroups.com
Hi Laurel,

This function below works for me - I have a separate function that logs the event creation and deletion (commented out) which writes to a sheet for error checking.
You may run into problems if people are allowed to create events outside of your create routine in the same calendar, as this will delete ALL events, not just ones you created automatically. I get around this by creating the event and adding a "Tag" to the event, which I then check for before deleting the event.
Hope this helps.
Laurie

// Remove all Events between dates given
// if allDay flag set, remove those as well
function delete_TKS_Events(startDate=new Date(),endDate=new Date(),deleteAllDay=true){
var myCalID=SpreadsheetApp.getActiveSpreadsheet().getRangeByName('Selected_Calendar_ID').getValue();
var myCal=CalendarApp.getCalendarById(myCalID);
//logInfo('Deleting Events','Start:'+ startDate +' End:'+endDate+' AllDay:'+deleteAllDay,true);
var allEvents=myCal.getEvents(startDate,endDate);
//logInfo('Delete:','Deleting - '+allEvents.length+' (Includes Day Numbers if appropriate)',true);
for (var e=0;e<allEvents.length;e++){
//Check for event
if (allEvents[e].isAllDayEvent()){
//are we deleting all day events too?
if(deleteAllDay){
allEvents[e].deleteEvent();
}
}else{
//not all day delete it
allEvents[e].deleteEvent();
}
}
//logInfo('Deleted Events','Deleted '+allEvents.length+' events',true);
return true;
}



--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/6eff3ca4-e2a2-4cc8-b407-e22ea76b4cf5n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages