Coeb
Coeb

Reputation: 25

TypeError: calendar.getEvents is not a function

I'm using google apps script and attempting to check if an event exists. I'm using some code from this question and I'm not sure why the script is telling me that getEvents() is not a function as it is here in google documentation My code looks like this

function doesEventExist() {
  var fromDate = new Date(); //This is Today
  var toDate = new Date();
  toDate.setDate(toDate.getDate()+20);
  Logger.log("From "+fromDate+" to "+toDate);
  var calendar = CalendarApp.getCalendarsByName("CalendarName");
  if(calendar == null){
    Logger.log("Unable to connect to calendar");
    exit();  
  }
  var events = calendar.getEvents(fromDate,toDate);

  for(var i=0; i<events.length;i++)
    {
      var ev = events[i];
      var title = calendar.getEventSeriesById(ev.getId()).getTitle();


      if (title.indexOf(Event Name)>-1){
        var start = ev.getStartTime();
        Logger.log("Found Event");
        return true;
          var id = ev.getId();
          var date = ev.getStartTime();
          var desc = ev.getDescription();

          }


    }
  }

Does the getEvents() function no longer exist or is there an issue with my code?

Edit: as someone commented getCalendarsByName() returns an array to I ended up setting the variable calendar to calendars[0]

Upvotes: 1

Views: 1874

Answers (1)

Jescanellas
Jescanellas

Reputation: 2608

I'm leaving @nbookmans comment as an answer for Stack workflow purposes.

As stated in the documentation, getCalendarsbyName() returns:

Calendar[] — all calendars with this name that the user can access

Which is an array of Calendars. In order to get the events, you have to iterate over the array.

Upvotes: 1

Related Questions