Search code examples
google-apps-scripttriggersgoogle-calendar-api

Automatically add guest to new calendar event


This is the first Google Apps Script I have created, and I am having some trouble. I used ChatGPT to give me the code framework, which appears to be primarily correct; however, there is a problem right at the start of the script with it not capturing the event that triggered the script. Reading through the documentation hasn't revealed what is wrong, and most examples I can find are using a Google sheet as the starting point and creating calendar events, which isn't my scenario.

The "Event object is not defined" console error message is showing in the logs. The errors were observed after actual calendar event creation, not the run button. I have the trigger set to run when a calendar event is updated (created, updated, deleted).

Here is the code:

function addGuestToNewMeetings(e) {
  var calendarId = 'bob@companyA.com'; // Replace with your Google Calendar ID
  var guestEmail = 'bob@companyB.com';

  try {
    var event = e.calendarEvent;

    if (!event) {
      console.error('Event object is undefined.');
      return;
    }

    // Check if the event title contains "Systems Review Scheduled"
    var eventTitle = event.getTitle();
    if (eventTitle.indexOf('Systems Review Scheduled') !== -1) {
      // Check if the guest is not already added
      var guestList = event.getGuestList();
      if (guestList && guestList.indexOf(guestEmail) === -1) {
        event.addGuest(guestEmail);
        console.log('Guest added to the event:', guestEmail);
      } else {
        console.log('Guest already exists in the event:', guestEmail);
      }
    } else {
      console.log('Event does not match the title:', eventTitle);
    }
  } catch (error) {
    console.error('Error:', error.toString());
  }
}

Solution

  • As you are new to Google Apps Script and new to JavaScript, I suggest you avoid using ChatGPT without the guidance of a human programming couch or tutor that guides you through designing the prompts and overseeing your conversations with this tool, as it is prone to have AI hallucinations.

    As you can see on the first line of the function declaration, the letter e is inside parenthesis. This letter is a variable, also called a function parameter. A JavaScript function could have none, one or multiple parameters.

    The JavaScript line that is writing the error is

    console.error('Event object is undefined.');
    

    Because the event object assigned to e doesn't have the property calendarEvent. ChatGPT has hallucinated that property.

    Instead of starting at ChatGPT, start at https://developers.google.com/apps-script. Pay special attention to the part that explains that Google Apps Script uses JavaScript as a programming language and to the page that explains the events objects, more specifically the section about the Calendar Events event object. https://developers.google.com/apps-script/guides/triggers/events#Google%20Calendar-events.