1

I am still learning Apps Script. here is sample google sheet and the code for creating calendar events from the Google sheet's custom menu. I want to add delete events & calendar functions into the menu option.

But I am not able to get the event list by the to & from date. I took help from this link

function delete_events()
{
  //Please note: Months are represented from 0-11 (January=0, February=1). Ensure dates are correct below before running the script.
  var fromDate = new Date(); //This represents June 1st 2020
  var toDate = new Date(); //This represents June 30th 2020
  toDate.setHours(toDate.getHours()+24);
  var calendarID = 'c_9ngacs4sflij848armjj9i91dk@group.calendar.google.com'; //Enter your calendar ID here

  var calendar = CalendarApp.getCalendarById(calendarID);
   Logger.log('calendar: '+calendar.getName());
  //Search for events between fromdate and todate with given search criteria
  var events = calendar.getEvents(fromDate, toDate);
  Logger.log('Event: '+events.length);
  // events.forEach(function(e,i){Logger.log(e[i])});
  for(var i=0; i<events.length;i++) //loop through all events
  {
    var ev = events[i];
    Logger.log('Event: '+ev.getTitle()+' found on '+ev.getStartTime()); // Log event name and title
      ev.deleteEvent(); // delete event
  }
}

My appscript.json file has following scopes added -

"oauthScopes": [
  "https://www.googleapis.com/auth/calendar",
  "https://www.googleapis.com/auth/script.external_request",
  "https://www.googleapis.com/auth/spreadsheets.currentonly",
  "https://www.googleapis.com/auth/spreadsheets"
],

But when I run the code above, I get following -

enter image description here

Then I see the actual calendar, which has full to events.

enter image description here

Why is that my function not working properly? Event id, CalId is Ok. So what is the cause of empty events?

Yuri Khristich
  • 13,448
  • 2
  • 8
  • 23
k.b
  • 157
  • 1
  • 2
  • 13
  • 1
    I'm not an expert in Calendar, so just a guess. It looks like the problem is with `getTitle()` function https://developers.google.com/apps-script/reference/calendar/calendar-event#getTitle() Probably, I'm not sure, you need to add `https://www.google.com/calendar/feeds` into your json – Yuri Khristich Jun 26 '21 at 10:55
  • 1
    Actually I think the problem is that setHours() just sets the hour for that date. So your fromDate and toDate are the same and hence no events. Try adding a day. `toDate=new Date(newDate().getFullYear(),new Date().getMonth(),new Date().getDate() + 1);` – Cooper Jun 26 '21 at 12:58
  • 1
    @Cooper, `setHours()` works fine for me. You can try it by yourself. Here is another similar example with `setDate()`: https://stackoverflow.com/questions/68088181/making-a-sheet-with-the-name-of-the-date-3-days-later/68089880#68089880 He can use `toDate.setDate(toDate.getDate()+1);` it's all the same – Yuri Khristich Jun 26 '21 at 18:11
  • I guess the problem is not with adding dates or hours. I have tried all those already. I need to try some other tricks. – k.b Jun 27 '21 at 12:13
  • @yuri-khristich Thanks for the link. it is nice one. – k.b Jun 27 '21 at 12:14
  • Hello there @k.b! If the previous comments solved your question, please write an answer showing how for documentation purposes. – Jacques-Guzel Heron Jun 28 '21 at 08:51

1 Answers1

0

I was able to solve using following post's code. Here is the [link][1]. The part of the code which solved my delete event function is following -

try {
  var event = cal.getEventSeriesById(id);
  event.deleteEventSeries();
  row[9] = '';  // Remove event ID    
}
catch (e) {
  // do nothing - we just want to avoid the exception when event doesn't exist
}

Thank you all for your inputs. [1]: Google Script: How can I Create/Update/Delete Calendar Event and prevent duplicate

k.b
  • 157
  • 1
  • 2
  • 13