DEV Community

Vryntel
Vryntel

Posted on

Add Responses Counter To Your Google Form

In this article I'll show you how to add a responses counter text, directly in the form, by using a simple script.

We will see two different type of counter, an incremental counter that will show how many people submitted our form, and a decremental counter, useful for example when you need to organize a limited event.

Incremental Counter:
Incremental Counter

Decremental Counter:
Decremental Counter

To make this, we will use the Google Apps Script Platform. 

Create a new form or open one you already have. Add a new title and description element like this:

Add Title and Description

After this, click on the three dots in the right of the page and click on Script Editor.
Script Editor

Now in the Editor page just copy this code for the incremental counter:



function increaseCounter() {
  // Get the form to which this script is bound.
  var form = FormApp.getActiveForm();

  //Get responses number
  var responses = form.getResponses().length;

  //The position of the question to change, starting from 0
  var questionIndex = 0;

  //Get the question and update the counter
  var question = form.getItems()[questionIndex];
  question.setTitle("This form has been submitted " + responses + " times");

  //To change the title or the description of the form
  //form.setDescription("Counter: " + responses);
  //form.setTitle("Counter: " + responses);
}


Enter fullscreen mode Exit fullscreen mode

And this for the decremental counter:



function decreaseCounter() {
  // Get the form to which this script is bound.
  var form = FormApp.getActiveForm();

  //Get responses number
  var responses = form.getResponses().length;

  var startCounter = 200;
  var currentCounter = startCounter - responses;

  //The position of the question to change, starting from 0
  var questionIndex = 0;

  //Get the question and update the counter
  var question = form.getItems()[questionIndex];
  question.setTitle("Only " + currentCounter + " tickets remaining");

  //To change the title or the description of the form
  //form.setDescription("Counter: " + currentCounter);
  //form.setTitle("Counter: " + currentCounter);
}


Enter fullscreen mode Exit fullscreen mode

You can also update the Form Title and Description, or any other form questions you want, all you need is the position index in the form (just count the questions from the top and starting from 0).

Save the project.

Save project

Now we need only to update the counter every time a user submit a new response. In other words we need to add a new event trigger that will run on every form submit action. To add a new trigger click on triggers on the left bar.

Add Trigger

Click on Add trigger button, on the bottom right, select the function to run and set the event type to "On form submit".
Click on save.

Trigger details

Now the counter is ready :)

Top comments (0)