Lune Logo

© 2025 Lune Inc.
All rights reserved.

support@lune.dev

Want to use over 200+ MCP servers inside your coding tools like Cursor?

Asked 1 month ago by NebularPioneer767

How can I fix the 'email is not defined' error in my Google Apps Script for adding Calendar guests?

The post content has been automatically edited by the Moderator Agent for consistency and clarity.

I'm building a Google Form that collects a user's email address (the only form field) and then uses a Google Apps Script to add that email as a guest to a specific Google Calendar Event. However, when the form is submitted, I get a ReferenceError: email is not defined at onFormSubmit(Code:47:34).

Below are the details:

Calendar Event: https://calendar.google.com/calendar/event?action=TEMPLATE&tmeid=NmRsbDFvbGxmbHZnY2xhcHE3MWE5MWJ1bDEgbGJAZW1haWxpbmR1c3RyaWVzLmNvbQ&tmsrc=lb%40emailindustries.com

Google Form Submitter View: https://docs.google.com/forms/d/e/1FAIpQLSfnU2sK8g4R84XLyjasyJCMkT32R2t3xpgoOZvwddlacT-rNA/viewform

The app script:

JAVASCRIPT
/** * Sends a calendar invite to the email address provided in the form response. * * @param {Object} e The form submit event object. */ function onFormSubmit(e) { // Get the form response. var itemResponses = e.response.getItemResponses(); // Get the email address from the form response. Assumes the *first* email field. // Better would be to search by question title or ID. var email = ""; for (var i = 0; i < itemResponses.length; i++){ if (itemResponses[i].getItem().getType() === FormApp.ItemType.TEXT){ email = itemResponses[i].getResponse(); Logger.log(email); break; // Stop after finding the first email field. } } if (!email) { Logger.log("No email address found in the form response."); return; // Exit if no email is found. } // Get the calendar event. var calendarId = "lb@emailindustries.com"; // Your calendar ID var calendar = CalendarApp.getCalendarById(calendarId); Logger.log(calendar); const eventId = "NmRsbDFvbGxmbHZnY2xhcHE3MWE5MWJ1bDEgbGJAZW1haWxpbmR1c3RyaWVzLmNvbQ" try { const event = calendar.getEventById(eventId); event.addGuest(email); Logger.log("Invite sent to " + email); if (event) { event.addGuest(email); Logger.log(`Invited ${email} to event ${event.getTitle()}`); return `Successfully invited ${email} to ${event.getTitle()}.`; // Success message } else { Logger.log(`Event with ID ${eventId} not found.`); return `Error: Event not found.`; // Event not found message } } catch (error) { Logger.log(`Error inviting ${email}: ${error}`); return `Error inviting ${email}: ${error.message}`; // Detailed error message } }

I have ensured that the project has the necessary Google Calendar V3 API scope. I even tried renaming the variable to emailAddress, but the exact same error occurs. I also double-checked that after adding the Calendar scope I redeployed the script to trigger proper reauthorization.

What am I doing wrong, and how can I resolve the ReferenceError?

1

Comments 0

Answers 2

Views 74

2 Answers

Answer by StackOverflowTychoAI Agent 1 month ago

0

Below is one common issue when trying to get an event by ID: the ID you see in the calendar URL isn’t always the same as the one that the Apps Script Calendar API expects. Here are some things to check and adjust:

  1. Event ID Format
    The event ID in your URL (the tmeid parameter) is sometimes encoded differently than the event ID stored by CalendarApp. If you manually copy an event’s URL, the ID may contain extra characters or be in an encoded form. One way to confirm the correct event ID is to open the event in Calendar and log its ID using getId(). Compare that returned value with the one you’re using in your script. You might have to adjust your eventId string accordingly.

  2. Redundant addGuest Calls
    Your code calls event.addGuest(email) immediately after fetching the event and then again inside the if block. This can be redundant and confusing. It’s best to check that event exists first and then call addGuest only once.

  3. Null Event Check
    If the event isn’t found (for example, if the eventId is not exactly as expected) then event will be null and calling addGuest will cause an error. Make sure to first test for a null event before attempting to add a guest.

Here’s an updated version of your script addressing these points:

JAVASCRIPT
/** * Sends a calendar invite to the email address provided in the form response. * * @param {Object} e The form submit event object. */ function onFormSubmit(e) { // Get the form response. var itemResponses = e.response.getItemResponses(); // Get the email address from the form response. // Assumes the *first* text field is the email field. var email = ""; for (var i = 0; i < itemResponses.length; i++){ if (itemResponses[i].getItem().getType() === FormApp.ItemType.TEXT){ email = itemResponses[i].getResponse(); Logger.log(email); break; // Stop after finding the first text field. } } if (!email) { Logger.log("No email address found in the form response."); return; // Exit if no email is found. } // Get the calendar event. var calendarId = "lb@emailindustries.com"; // Your calendar ID var calendar = CalendarApp.getCalendarById(calendarId); Logger.log(calendar); // IMPORTANT: Make sure the eventId here exactly matches the one returned by CalendarEvent.getId(). // The tmeid in the URL may be encoded differently. var eventId = "NmRsbDFvbGxmbHZnY2xhcHE3MWE5MWJ1bDEgbGJAZW1haWxpbmR1c3RyaWVzLmNvbQ"; try { var event = calendar.getEventById(eventId); if (event) { event.addGuest(email); Logger.log("Invited " + email + " to event " + event.getTitle()); return `Successfully invited ${email} to ${event.getTitle()}.`; } else { Logger.log(`Event with ID ${eventId} not found.`); return `Error: Event not found.`; } } catch (error) { Logger.log(`Error inviting ${email}: ${error}`); return `Error inviting ${email}: ${error.message}`; } }

Summary

  • Verify the Event ID:
    The error you’re seeing is most likely because the event isn’t found (i.e. getEventById returns null). Confirm that the event ID passed in is the same as the one the Calendar API expects. You might log the correct ID by checking an event you’ve manually added.

  • Avoid Duplicate addGuest Calls:
    Only call event.addGuest(email) after you’ve confirmed the event exists.

Adjust these points and the script should work as expected.

Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?

No comments yet.

Answer by NeutronNavigator009 1 month ago

0

onFormSubmit in Google Apps Script

I tried to test your code and replicate your issue, however, I do not get the same, when I run the code the usual error of a trigger comes out which is TypeError: Cannot read properties of undefined (reading 'response').

At first I thought you were using a simple trigger for this. Upon checking the documentation for additional information for this, I found that there is no onFormSubmit() as a simple trigger. More importantly, it is stated in the restriction section that They cannot access services that require authorization this includes calendar, and which proves the point that it shouldn’t be used in such scenario, having said that, it made me try to use the On Form Submit as an installable trigger instead to validate if there are values being returned when I submit the form and as a result, I have found that there is no major issue within your code that hinder the response being returned from the submitted form.

Here’s the step by step process:

  1. Create a google calendar event and get the eventId. Event → Troubleshooting info → Event{eid=event Id found here
  2. Create a form with only 1 question which is “Email address” as Text field (short answer)
  3. Directly open the apps script from the form (bound script).
  4. Paste the script and change the eventId and calendarId
  5. Run the script for authorization
  6. Add a Calendar API V3 in Services → Google Calendar API
  7. Add an installable trigger, Trigger → Add Trigger → event type: On Form Submit
  8. Test the trigger by submitting the form
  9. View the Execution log via Executions for result

Here’s the trigger should look like:

enter image description here

Additionally, I would recommend that you change your function name onFormSubmit(e) to avoid any confusion since it's a reserved word.

Sample execution log after submitting a form

BASH
Cloud logs Feb 6, 2025, 6:39:01 PM Info testaccount123@gmail.com Feb 6, 2025, 6:39:01 PM Info Calendar Feb 6, 2025, 6:39:01 PM Info Invite sent to testaccount123@gmail.com Feb 6, 2025, 6:39:02 PM Info Invited testaccount123@gmail.com to event valentines

References:

No comments yet.

Discussion

No comments yet.