I need to run a script to delete a Google Calendar event on deleting a row from Google Spreadsheets.
Do you have any way of getting the data from the row which was deleted?
I have this code to create a Calendar event when a row is changed, but it does not work when it is deleted.
function SaveToCalendar(e) {
const eventCal = CalendarApp.getOwnedCalendarById(CALENDAR_ID);
const sheet = e.source.getActiveSheet();
const row = e.source.getActiveRange().getRow();
const cell = e.source.getActiveRange();
const name = sheet.getRange(row, 2, 1, 1).getValue();
const eventName = sheet.getRange(row, 3, 1, 1).getValue();
const description = sheet.getRange(row, 4, 1, 1).getValue();
const startDate = sheet.getRange(row, 5, 1, 1).getValue();
const endDate = sheet.getRange(row, 6, 1, 1).getValue();
const department = sheet.getRange(row, 7, 1, 1).getValue();
const eventTitle = `${eventName} | ${department}`;
if(cell.getValue() == APROVADO) {
eventCal.createEvent(eventTitle, new Date(startDate), new Date(endDate), {
description: description
});
} else {
var events = eventCal.getEvents(new Date(startDate), new Date(endDate))
var foundEvent = events?.find(e => e.getTitle() === eventTitle);
foundEvent?.deleteEvent();
}
}