javascriptgoogle-apps-scriptgoogle-calendar-api

How to use Calendar.Events.patch in Google Apps Script?


I have searched and searched trying to find the answer to this but from all the examples and answers I've read I can't see what my problem is.

I am trying to update the extended properties of a series of events selected by a user. The IDs of the calendar events (from the Advanced API, not the built in CalendarApp) are stored in an array as they're selected. I'm then passing this array to a function to be looped through and update the extended properties of each event identified in the array.

It simply doesn't work, I just get null returned from the patch call, not even an error that I could work with. Here is a screenshot of logged output, the null at the end if what the patch request returns.

My function is:

function updateLessonsStatus(arrCalIds){
  try {
    Logger.log(arrCalIds);
    arrCalIds.forEach(function(row){
      Logger.log(row);
      var objEventPatch = {
        "resource": {
          "extendedProperties": {
            "shared": {
              "status": "accounted"
            }
          }
        }
      };
      Logger.log(objEventPatch);
      return Calendar.Events.patch(objEventPatch, "primary", row);
      //return true;
    });
  }

  catch(err) {
    Logger.log(err);
    throw err;
  }
}

I have also tried setting objEventPatch like this as per Google's Calendar API patch 'Try this API' reference https://developers.google.com/calendar/v3/reference/events/patch :

      var objEventPatch = {
        "calendarId": "primary",
        "eventId": row,
        "resource": {
          "extendedProperties": {
            "shared": {
              "status": "accounted"
            }
          }
        }
      };

I still get null from Google Apps Script. I can succesfully get and patch events using the IDs I have using the 'Try this API' feature.

There are very few examples of using the advanced APIs with Google Apps Script in Google's documentation and nothing I could find for patch so I have used the Javascript examples to construct what I have written so far.

Can anybody help with what I am doing wrong?

(UPDATED for clarity and included image of null returned logger output)


Solution

  • You are providing one nested object too much

    Modify

          var objEventPatch = {
            "resource": {
              "extendedProperties": {
                "shared": {
                  "status": "accounteddd"
                }
              }
            }
          };
    

    to

          var objEventPatch = {
              "extendedProperties": {
                "shared": {
                  "status": "accounteddd"
                }
              }
            };
    

    Side note:

    If you want to explore the request response for troubleshooting purposes, I recommend you to perform your request with a call to the request URL with the UrlFetchApp instead of using the prebuilt Calendar service.