google-sheetsgoogle-apps-scriptgoogle-calendar-api

How to Delete Today's Google Calendar Entries in Google Apps Script


I am trying to delete Google Calendar entries for today in Google Apps Script. I create the entry from a Google Spreadsheet. In the cells, the dates are formatted mm/dd/yyyy 00:00:00 where the start date and end date are equal, creating what displays as an all day entry on Google Calendar.

Here is the code I use to create the entry:

function addEntries() {
  
  var calendarId = "calID"; // Please set your calendar ID.

  // Retrieve values from Spreadsheet.
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Calendar_Data");
  var lastRow = sheet.getLastRow();
  var data = ss.getRange("A2:E" + lastRow).getValues();
  
  // Create a request body for the batch request.
  var colorObj = { "PALE_BLUE": 1, "PALE_GREEN": 2, "MAUVE": 3, "PALE_RED": 4, "YELLOW": 5, "ORANGE": 6, "CYAN": 7, "GRAY": 8, "BLUE": 9, "GREEN": 10, "RED": 11 };
  var requests = data.map(([summary, description, dateObj, , color]) => {
    const date = Utilities.formatDate(dateObj, Session.getScriptTimeZone(), "yyyy-MM-dd");
    return {
      method: "POST",
      endpoint: `https://www.googleapis.com/calendar/v3/calendars/${calendarId}/events`,
      requestBody: { end: { date }, start: { date }, description, colorId: colorObj[color], summary }
    }
  })
  
  // Request the batch request using the created request body.
  var result = BatchRequest.EDo({ batchPath: "batch/calendar/v3", requests });
  console.log(result);
}

When I try to delete today's entries, the code runs without errors, but no entries are deleted or logged. I assume it has something to do with my start and end date in the deleteTodayEntries() function. Here is the code I have tried:

function deleteTodayEntries() {
  var calendarID = "calID"; // Please set your calendar ID.

  var today = new Date();
  var startDate = new Date(today.getDate());
  var endDate = new Date(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);
}

Could someone possible point out what I am missing here? Thank you.


Solution

  • Reading through the Class Calendar documentation, I came upon this function: getEventsForDay(). I have changed my code to:

    function deleteTodayEntries() {
      var calendarID = "calID"; // Please set your calendar ID.
    
      
      var today = new Date();
      var calendar = CalendarApp.getCalendarById(calendarID);
      var existingEvents = calendar.getEventsForDay(today);
    
    
      // 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);
    }
    

    And now my code is working.