I am working on a Google Calendar project by which I am deleting old "events" and replacing them with new "events". Tanaike has graciously helped me to make use of the Google Calendar API and his Google Apps Script library to be able to add events in a single batch request here, greatly speeding up the process.
Because of the complications associated with multiple people editing multiple entries in multiple ways, I have decided to approach it by this route.
I have a script that successfully iterates through each Google Calendar event between the defined start date and end date. What I am struggling with now is to delete all events within two dates in the same manner as adding them to the Google Calendar, i.e., a single batch request. Is this a possibility?
Here is my current script (that works, but is in need of optimization):
function deleteEvents() {
var today = new Date();
var startDate = new Date(today.getFullYear(),today.getMonth(),today.getDate()-14);
var endDate = new Date(today.getFullYear()+1,today.getDate());
// ID of the Calendar to access
var calendarID = 'xxxxxxxxxx';
// get the Calendar
var calendar = CalendarApp.getCalendarById(calendarID);
// get all existing events between date range
var existingEvents = calendar.getEvents(startDate, endDate);
// count and log the number of found events
var numEvents = existingEvents.length;
// loop through found events ***************
for (var i = 0; i < numEvents; i++) {
// create variable for current event
var event = existingEvents[i];
// delete the event
event.deleteEvent();
}
}
I believe your goal is as follows.
In this case, how about the following modification?
In order to use the batch request, it is required to create a request body. Ref But, I thought that it might be a bit difficult. So, I created it as a Google Apps Script library. Ref In this answer, this library is used.
Please install BatchRequest of the Google Apps Script library. You can see how to install it here.
Please enable Calendar API at Advanced Google services. Ref
When your script is modified using the above library and Calendar API, it becomes as follows.
function deleteEvents() {
var calendarID = "###"; // Please set your calendar ID.
var today = new Date();
var startDate = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 14);
var endDate = new Date(today.getFullYear() + 1, today.getDate());
var calendar = CalendarApp.getCalendarById(calendarID);
var existingEvents = calendar.getEvents(startDate, endDate);
// Create a request body for the batch request.
var requests = existingEvents.map(e => ({
method: "DELETE",
endpoint: `https://www.googleapis.com/calendar/v3/calendars/${calendarID}/events/${e.getId().split("@")[0]}`,
}));
// Request the batch request using the created request body.
var result = BatchRequest.EDo({ batchPath: "batch/calendar/v3", requests });
console.log(result);
}
existingEvents
are deleted with the batch request. Please be careful about this. So, I would like to recommend testing this script using a sample event and calendar.