Limit Responses in Google Forms Using Google Apps Script

July 14, 2021
Limit Responses in Google Forms Using Google Apps Script
Table of Contents
Table of Contents

In this blog, I will show you how to limit responses in Google Forms using Google Apps Script. We are going to use the following fictitious example:

  • We are having a Cloud computing workshop.
  • But the hall that we have booked has a max capacity of 100 people.
  • People can register for the workshop through a Google Form.
  • The organizing Team wants to make sure that there are only 100 responses in the Google Form.

So let’s get started.

If you need to refresh your knowledge on using Google Forms, click on the link given below.

Step1: Sample Google Form

The form that I will be using is a Simple Registration form. (If you prefer working with click here).

The form contains the following details:

  1. Name of the Attendee
  2. Email Address of the Attendee
  3. Phone number of the Attendee
This is the Google Form that the Attendee will fill

Step2: Write the Automation Script

While you are in the Form, launch the Apps Script Editor.

To do that:

(1) Click on 3 dots next to the Send button on the top right.

(2) Next click on the Script Editor option.

This brings up the Script Editor as shown below:

We have reached the Script Editor. Let’s Code.

function Limit_Responses() {
    
    let  max_responses = 8;
    let  form = FormApp.openById("17XDAjSnwB8gU0CIpiDLRv3Q7-Ozfro-n0pRDQOAEjuI");
    let  responses = form.getResponses();
  let len = responses.length;
  //Logger.log(len);
  if (len === max_responses){

    form.setAcceptingResponses(false);

  }

}
function Limit_Responses() {
    let  max_responses = 8;
    let  form = FormApp.openById("17XDAjSnwB8gU0CIpiDLRv3Q7-Ozfro-n0pRDQOAEjuI");
    let  responses = form.getResponses();

Here we are first declaring the maximum number of responses that you want in your Google Form. Next, we are going to get the form by ID. Then we are going to get the responses that are in the Google Form.

let len = responses.length;
//Logger.log(len);
if (len === max_responses){

    form.setAcceptingResponses(false);
     }

}

Here we get the length of the responses present in the Google Form.

Step3: Add a Trigger

In order to count the responses, we have to add a trigger, so that as soon a new response comes into the form, the count of the responses will get incremented by 1.

To add the trigger follow these steps:

  1. Click on the Triggers button.

2. To add the trigger click on Add Trigger.

3. Make the following changes in the trigger that you have made.

Here you have to select the Limit_Responses function. For the event type you have to select On form Submit.

After making the changes click on Save.

Now as soon as the number of responses in the Google Form matches the max responses, the Google Form will get locked and no longer accept responses. It will look like this.

Here you can see that the form has shown a message that says that the form is no longer accepting responses. If you want custom messages for the form you can go back to the form and make the changes.

After some time maybe you want to open the Google Form again, to do that follow these steps :

  1. Click on responses

2. Here you can see that currently, the Google Form is not accepting responses.

Open the Form back for Responses

To open the form back for responses click on the button that says Not Accepting responses.

On clicking the Google Form will be open for responses.

If you prefer watching the video, click on the link below.

This is all for this blog. I hope you have understood how to Limit Responses in Google Forms Using Google Apps Script. You can send your feedback to aryanirani123@gmail.com.

A previous version appeared at this link: https://aryanirani123.medium.com/limit-responses-in-google-forms-using-google-apps-script-64ea2d07342f



Great! Next, complete checkout for full access to Form Publisher Blog
Welcome back! You've successfully signed in
You've successfully subscribed to Form Publisher Blog
Success! Your account is fully activated, you now have access to all content
Success! Your billing info has been updated
Your billing was not updated