Add Responses Counter To Your Google Form
Tags: googlecloud, tutorial, javascript, productivity
Published: 2022-01-16 18:17:42

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zej3upqir6821zz14cgs.JPG)


*Decremental Counter:*
![Decremental Counter](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w75b2r00xv7iztm2m2qy.JPG)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qcag5rjpwwpvhdyh715k.JPG)

After this, click on the three dots in the right of the page and click on Script Editor.
![Script Editor](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8l3dl87x5kbyazwy2e83.JPG)

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);
}
```
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);
}
```


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mcaq9yg4srhfaqmru1bs.JPG)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gooqyogd0bv5y5f7ecm4.JPG)



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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7o20p6uuahv1fr1t38ug.JPG)

Now the counter is ready :)