Create Auto responding google form with 2 lines of code

One of the most requested features in google forms is auto-respond but still it is not built in with google form. But we can add this feature with just 2 lines of code! Yes, you heard it right! It’s literally 2 lines of code. Let’s do it.

Quick overview – Auto respond in google form

Every google form can be associated with a google spreadsheet to receive form submissions. Code for auto respond will be added in this spreadsheet. Every spreadsheet with google form association has a form submit trigger which sends an event object with form data.

So all we have to do is write a function for getting that form data, send email using sendEmail function and add a form submit trigger to that function.

Demo form

Here’s a demo form

Fill in the form, you will get an email with a welcome message and I will get a message with new user submission!

Try it out and start creating this auto responding form.

Getting started

  1. Create a form from here – https://docs.google.com/forms

  2. Add form fields as required. I’ve added Name and Email fields in the demo form.

  3. Link a spreadsheet to the form by clicking on the spreadsheet icon in responses tab as shown in image below.

auto respond google form

Coding part

Open the linked spreadsheet and open script editor (Tools > script editor) and paste the code given below :

function autoResponder(e){
var name = e.namedValues.Name[0], email = e.namedValues.Email[0], adminEmail = "[email protected]"
MailApp.sendEmail(email, "Welcome!", "Hello " + name + " ,\n\nThank you for registering for the event.");
}

This 2 lines of code is all you need to add auto-responding email feature to google form.

Code explanation

  1. A form submit trigger sends an event object which contains form data which can be used to get the user details and send him an email.

  2. We are passing that event object to our autoResponder function as e

  3. Next step is to get the form data. Event object has 2 properties namedValues and values. Either of them can be used to get form data.

  4. values is an array with form field values in the order as they appear in the form. namedValues is an object with field names as properties and field values as values. Read more about event objects here. We are using namedValues in this code.

  5. In the first line, we define name and email variables and set it to appropriate field values. Now send an email to the user using sendEmail function. Read more about sending emails here – Send Email from Google sheets with one line of code

Save Run this code once by clicking on the play button in the script editor > a dialog pops up for permissions > continue and allow.

Now link this function to form submit trigger. You can do this by clicking on the clock button

google form - submit trigger in google sheets

That’s it! Now Whenever someone fills your form, they get a welcome message. Customize it as per your needs.

google form - submit trigger in google sheets

Adding more functionality

You can add more functionality like sending an email to yourself with new form submission’s details. Just add one more sendEmail function with your To Address and appropriate message.

Example code looks like this :

function autoResponder(e){
var name = e.namedValues.Name[0], email = e.namedValues.Email[0], adminEmail = "[email protected]"
MailApp.sendEmail(email, "Welcome!", "Hello " + name + " ,\n\nThank you for registering for the event.");
MailApp.sendEmail(adminEmail,"New User registration","New user registered for your event.\n\nName: " + name + "\nEmail: " + email);
}

google form - submit trigger in google sheets

Wrapping up

We’ve been sending plain text emails till now, We can send HTML Emails using htmlBody parameter as mentioned in the documentation.

I’ll be writing another post on how to send full fledged HTML Emails from google sheets. Meanwhile, try it out by reading the documentation!

If you have any questions or feedback, comment below.

Get notified when there's a new post.

Need some help? Post your questions on our forum

Author: Ranjith kumar

A CA student by education, self taught coder by passion, loves to explore new technologies and believes in learn by doing.

6 Thoughts

  1. in this code:

    function autoResponder(e){
    var name = e.namedValues.Name[0], email = e.namedValues.Email[0], adminEmail = "[email protected]"
    MailApp.sendEmail(email, "Welcome!", "Hello " + name + " ,\n\nThank you for registering for the event.");
    }

    what is the use of adminEmail=”[email protected]

    ?

    1. Have a look at complete code which is at the end of the post. It is used to send another email to owner of google form.

    1. Looks like you haven’t setup trigger or you are running the code manually. This code should not be executed manually. It should be triggered by a form submission.

  2. is there anyway someone can help me? I am trying to send an email after a certain answer is entered using my form.

    So if some enters active, then an email is sent to a certain group. If someone selects kiva, then an email is sent to a different group.

    Is this posibble? Is there a script already out there that I can tweak?

Leave a Reply

Your email address will not be published. Required fields are marked *