Sven Dugojevic
Sven Dugojevic

Reputation: 35

"Invalid number of arguments" error when trying to update an event

I am trying to update calendar events from google apps script. I have the calendar ID, event ID, and objects I am trying to update as a variable:

   var eventinfo = {
   "calendarId": calID
      "eventId": eventID,
      "resource": {
        "description": "1234"
      }
   };

 //update the event description and location

   var updater;
   try {
    updater = Calendar.Events.update(eventinfo);
    Logger.log('Successfully updated event: ' + i);
   } catch (e) {
    Logger.log('Fetch threw an exception: ' + e);
    } 

I am getting this error:

Fetch threw an exception: Exception: Invalid number of arguments provided. Expected 3-5 only

Previously, I had attempted to invoke the update method this way .update(calID, eventID, eventinfo) where event info was an object with just a description, but that was returning the error saying bad call.

I think I am missing something in my object argument.

Upvotes: 1

Views: 4254

Answers (1)

Marios
Marios

Reputation: 27400

Issues:

  • First of all, you forgot a comma in the definition of eventinfo between the first and the second row.

  • However, I don't think your approach will work, because you don't pass an event object in the Calendar.Events.update() function. The structure should be like that:

    Calendar.Events.update(
       event,
       calendarId,
       event.id
     ); 
    

Solution/Example:

  • The following example updates the very first event in the future. In particular, it updates the title (summary), description and place but feel free modify that if you want:

    function updateNextEvent() {
      const calendarId = 'primary';
      const now = new Date();
      const events = Calendar.Events.list(calendarId, {
        timeMin: now.toISOString(),
        singleEvents: true,
        orderBy: 'startTime',
        maxResults: 1
      });
    
     var event = events.items[0]; //use your own event object here if you want
    
     event.location = 'The Coffee Shop';
     event.description = '1234';
     event.summary = 'New event';
     event = Calendar.Events.update(
          event,
          calendarId,
          event.id
        ); 
    }
    

Of course, don't forget to switch on the Calendar API from Resources => Advanced Google services.

References:

Upvotes: 3

Related Questions