Automatically adding a guest to new calendar events

44 views
Skip to first unread message

Matthew Metzinger

unread,
Sep 8, 2023, 9:56:20 AM9/8/23
to Google Apps Script Community

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. I have the trigger to run when a calendar event is updated (created, updated, deleted).

Here is the code:

function addGuestToNewMeetings(e) {
  var calendarId = 'b...@companyA.com'; // Replace with your Google Calendar ID
  var guestEmail = 'b...@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());
  }
}
Reply all
Reply to author
Forward
0 new messages