0

I want to send a Google calendar invite using the google apps script but I am encountering an error in the last line of the following code snippet:

function testnotification(){
         var calendarId = "####";
         var eventId = "###";
         var email = ""###@gmail.com
         addGuestAndSendEmail(calendarId,eventId,email)
       }
    
    
      function addGuestAndSendEmail(calendarId, eventId, newGuest) {
    
      var calendar = CalendarApp.getCalendarById(calendarId);    
      var event = calendar.getEventById(eventId);
      var attendees = [];
      attendees.push({email: newGuest});
      var resource = { attendees: attendees };
      var args = { sendUpdates: "all" };
    
      calendar.createEvent.patch(resource, calendarId, eventId, args);
    }

I have modified last line from Calendar.Events.patch(event, calendar, event.id, args); to calendar.createEvent.patch(resource, calendarId, eventId, args), however when I run, it says:

calendar.createEvent.patch is not a function

I reviewed stackoverflow questions (Q1 , Q2) but couldn't find a way. @Tanaike proposed the following answer, when I ran this script:

function testNotification(){

         var calendarId = "###";
         var eventId = "###";
         var email = "###@gmail.com"
            addGuestAndSendEmail(calendarId,eventId,email)
       }
           
        function addGuestAndSendEmail(calendarId, eventId, newGuest) {
           Calendar.Events.patch({ attendees: [{ email: newGuest }] }, calendarId, eventId, { sendUpdates: "all" });
    }

I received the following error:

GoogleJsonResponseException: API call to calendar.events.patch failed with error: Not Found

This calendar is shared with me however, I have the following permissions to this calendar:

Permission

  • From discussion with the owner of this question, it was found that when the calendar IDs of shared calendar and OP's own calendar are used with the Calendar API, the same error like `API call to calendar.events.patch failed with error: Not Found` occurs. In this case, it is considered that the reason for the current issue depends on the setting of Google Workspace belonging to the owner of this question. I think that the modified script is correct. – Tanaike Mar 04 '23 at 10:11
  • But, if the calendar cannot be accessed from Calendar API, I'm worried that an error of `Not Found` occurs. Although I could understand your 1st issue, unfortunately, I cannot resolve your 2nd issue In this case, my answer has already not been useful. So, I have to delete my answer. I apologize for my poor skill. – Tanaike Mar 04 '23 at 10:11
  • Can we somehow make it work using: `CalendarApp.getCalendarById(calendarId)`? –  Mar 04 '23 at 10:15
  • @Tanaike, thankfully your script is working fine now, you can repost your answer or let me know and I will repost it, –  Mar 05 '23 at 03:27
  • Thank you for replying. I'm glad your issue was resolved. If you think that our discussions in my answer will be useful for other users, I can reopen my answer. – Tanaike Mar 05 '23 at 06:28
  • Sure, please do it, I am removing my answer –  Mar 05 '23 at 06:37
  • Thank you for replying. Now, I reopened my answer. – Tanaike Mar 05 '23 at 06:40

1 Answers1

1

I think that calendar.createEvent.patch(resource, calendarId, eventId, args); is not correct. I think that the reason for your current issue of calendar.createEvent.patch is not a function is due to this. If you want to achieve your goal, how about the following modification?

Modified script:

In this modification, your addGuestAndSendEmail is modified.

function addGuestAndSendEmail(calendarId, eventId, newGuest) {
  Calendar.Events.patch({ attendees: [{ email: newGuest }] }, calendarId, eventId, { sendUpdates: "all" });
}
  • When the values of calendarId, eventId, newGuest are valid values, an email is sent to newGuest.

Note:

Reference:

Added:

About your 2nd issue of API call to calendar.events.patch failed with error: Not Found using my proposed script,

  • In the case that the permission of calendar is "See only free/busy (hide details)" and "See all event details", an error like GoogleJsonResponseException: API call to calendar.events.patch failed with error: Forbidden occurs.

  • In the case that the permission of the calendar is "Make changes to events", no error occurs.

  • In the case that the permission of calendar is "Make changes and manage sharing", no error occurs.

  • In the case that the invalid value is used as calendarId, an error like GoogleJsonResponseException: API call to calendar.events.patch failed with error: Not Found occurs.

    • It seems that this error message is the same with your error message.

From your updated question of I have also updated permission screenshot in my question, please review it, I noticed that your permission is "Make changes and manage sharing". In this case, I think that the script works. But even when you change the permission from "Make changes and manage sharing" to "Make changes to events", the same error of GoogleJsonResponseException: API call to calendar.events.patch failed with error: Not Found occurs. In this case, I would like to recommend confirming the calendar ID again.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thank you for your answer however it shows following error: `API call to calendar.events.patch failed with error: Not Found` –  Mar 04 '23 at 08:42
  • @Roomi Thank you for replying. From `API call to calendar.events.patch failed with error: Not Found`, I'm worried that you have still disabled Calendar API. How about this? And also, from `API call to calendar.events.patch failed with error: Not Found`, I'm worried that you miscopied my script. How about this? In my proposed script `Calendar.Events.patch`. Please confirm it again. If you cannot understand my comment, can you provide your Google Apps Script project for correctly replicating your issue? By this, I would like to confirm it. – Tanaike Mar 04 '23 at 08:45
  • @Roomi By the way, when I tested my proposed script using the sample values of `calendarId, eventId, newGuest` by enabling Calendar API, no error occurs. I apologize for this situation. – Tanaike Mar 04 '23 at 08:46
  • just to let you know, this is not my primary caldendar, this is shared calendar to which I have access. could that be the reason, I also updated my question with your script, I have already enabled calendar api, thank you –  Mar 04 '23 at 08:50
  • @Roomi Thank you for replying. I understood your question as you wanted to resolve your issue of `calendar.createEvent.patch is not a function`. I answered your this question. But in your reply, in your reply, you say `this is not my primary caldendar, this is shared calendar to which I have access.`. I couldn't notice it from your initial question. I apologize for my poor English skill. In this case, your question has 2 issues. One is your wrong script. And, another is the issue of permission. – Tanaike Mar 04 '23 at 09:00
  • @Roomi About your 1st issue, I think that my proposed script can be used. About your 2nd issue, in this case, it is required to confirm the permission for changing the event to the calendar of `calendarId`. But, I can't know your situation. So, I would like to recommend resolving from your 1st issue. So, please test my proposed script using the valid calendar ID. After you confirmed the script can be used, as the next step, please confirm the permission for the calendar ID of your current issue. – Tanaike Mar 04 '23 at 09:00
  • thank you , I have also updated permission screenshot in my question, please review it –  Mar 04 '23 at 09:11
  • @Roomi Thank you for replying. From your updated question of `I have also updated permission screenshot in my question, please review it`, I noticed that your permission is "Make changes and manage sharing". So, I updated my answer. Please confirm it. In that case, if my understanding of your situation is correct, I would like to recommend changing the permission. – Tanaike Mar 04 '23 at 09:24
  • Thank you for your answer, I have changed the permission to "Make changes to events" and double checked the calendar id, unfortunately still the same error –  Mar 04 '23 at 09:25
  • @Roomi Sorry. I noticed that I test the wrong script. I will update my answer. Please wait for it. – Tanaike Mar 04 '23 at 09:28
  • @Roomi I have to apologize for my poor skill. I have tested the wrong script. By using the latest script, I tested the script with several situations. By this, I updated my answer. Please confirm it. I guessed that the reason for your current issue is due to the invalid calendar ID. Please confirm your calendar ID again. – Tanaike Mar 04 '23 at 09:31
  • Sorry, same error. I have also added my calendar id in the question, –  Mar 04 '23 at 09:36
  • @Roomi Thank you for replying. About `Sorry, same error. I have also added my calendar id in the question, `, I think that in this case, you are using an invalid calendar ID. Can you provide the sample calendar ID by hiding the personal information? When the calendar ID is a valid value, the script works with the permission of "Make changes to events" and "Make changes and manage sharing". – Tanaike Mar 04 '23 at 09:38
  • I am using this calendar id: 46fd39be3fc508ac49579b595ba574667a79eecbfdcfaed83cf2af77b401ab@group.calendar.google.com –  Mar 04 '23 at 09:40
  • The calendar is accessible when I use this : var calendar=CalendarApp.getCalendarById(calendarId); –  Mar 04 '23 at 09:43
  • @Roomi Thank you for replying. About `The calendar is accessible when I use this : var calendar=CalendarApp.getCalendarById(calendarId);`, when `Logger.log(CalendarApp.getCalendarById(calendarId))` is run, I think that `null` is shown. – Tanaike Mar 04 '23 at 09:49
  • @Roomi By the way, about `Sorry, same error. I have also added my calendar id in the question`, in this case, when you use your calendar ID with `Logger.log(CalendarApp.getCalendarById(calendarId).getName())`, you can see the correct calendar title can be seen in the log? – Tanaike Mar 04 '23 at 09:50
  • @Roomi And, in your situation, your account is in Google Workspace and you are trying to access a calendar in Google Workspace? – Tanaike Mar 04 '23 at 09:51
  • yes, my account is in google workspace, when I logged it, it showed me correct name which is "UX Reviews", you can also test with the following calendar id: "46fd39be3fc508ac49579b595ba574667a70c9eecbfdcfaed83cf2af77b401ab@group.calendar.google.com" –  Mar 04 '23 at 09:53
  • @Roomi Thank you for replying. About `Sorry, same error. I have also added my calendar id in the question`, in this case, when you use your calendar ID with `Logger.log(CalendarApp.getCalendarById(calendarId).getName())`, you can see the correct calendar title can be seen in the log? – Tanaike Mar 04 '23 at 09:54
  • yes, the correct title can be seen when I use logger.log –  Mar 04 '23 at 09:55
  • @Roomi Thank you for replying. About `yes, my account is in google workspace`, in this case, it is required to confirm your situation to the administrator. Because the calendar might not be able to be accessed by Calendar API. Unfortunately, I cannot know the setting in your situation. I apologize for this. So, first, I would like to recommend confirming whether the script works using valid calendar ID. – Tanaike Mar 04 '23 at 09:56
  • I can access the calendar with getCalendarsById() method –  Mar 04 '23 at 09:57
  • @Roomi Thank you for replying. I have to apologize for my poor English skill. About `I can access the calendar with getCalendarsById() method`, you cannot misunderstand my comment. Please test my script with the calendar ID of your own calendar. If the same error occurs, I'm worried that Calendar API might not be able to be used in your situation. At that time, please confirm about it to your administrator. – Tanaike Mar 04 '23 at 09:59
  • yes, I am having same error even in my own calendar, I have tried it with 2 different google accounts –  Mar 04 '23 at 10:04
  • @Roomi Thank you for replying. From `yes, I am having same error even in my own calendar`, I think that your 2nd issue is due to the setting of Google Workspace. In this case, I would like to ask about your current issue to your administrator. – Tanaike Mar 04 '23 at 10:06
  • @Roomi I think that from discussion with the owner of this question, it is considered that the reason for the current issue of `API call to calendar.events.patch failed with error: Not Found` depends on the setting of Google Workspace belonging to the owner of this question. I think that the modified script is correct. – Tanaike Mar 04 '23 at 10:07
  • @Roomi But, if the calendar cannot be accessed from Calendar API, I'm worried that an error of `Not Found` occurs. Although I could understand your 1st issue, unfortunately, I cannot resolve your 2nd issue In this case, my answer has already not been useful. So, I have to delete my answer. I apologize for my poor skill. – Tanaike Mar 04 '23 at 10:07
  • Can we send calendar notification by accessing calendar using getCalendarsById()? –  Mar 04 '23 at 10:08