How to turn off Google form responses with certain conditions

How to turn off Google form responses with certain conditions

You can limit responses in Google Forms once a certain amount of total has been reached automatically. In this blog, we will be telling you all about how to turn off Google form responses with certain conditions using the Google Apps Script.

Google Forms makes an integral part of the free suite of tools, a.k.a., Google Workspace, offered by Google. It is certainly one of the easiest methods to collect important data, and also automatically save the data on the Google Spreadsheet. 

Launched back in 2006, the Google Forms feature first started out as a Google Sheet feature in 2008. A form could be simply added to this spreadsheet. Although the feature was basic it was pretty useful at the time. Over the years, Google focused on bringing more features to Google Forms. It was finally turned into a standalone application back in 2016. From the management of forms to quick access to all important forms under one roof, the full-featured tool is beyond phenomenal. 

Here’s how you can turn off Google form responses with certain conditions using Google Apps Script. 

To turn off Google form responses after a certain number of responses, you can use the below Apps Script code:

function shutdownForm(maxResponses) {
  var form = FormApp.getActiveForm();
  form.setAcceptingResponses(false);
}

function onFormSubmit(e) {
  var form = FormApp.getActiveForm();
  var responses = form.getResponses().length;
  var maxResponses = 10; // Replace 10 with the maximum number of responses you want to allow
  
  if (responses >= maxResponses) {
    shutdownForm(maxResponses);
  }
}

To shut off the form at a specific date and time, you can use the following code:

function shutdownForm() {
  var form = FormApp.getActiveForm();
  form.setAcceptingResponses(false);
}

function scheduleFormShutdown() {
  var form = FormApp.getActiveForm();
  var shutdownDate = new Date("January 1, 2021 00:00:00"); // Replace with the date and time you want to shut off the form
  
  if (new Date().getTime() >= shutdownDate.getTime()) {
    shutdownForm();
  }
}