Skip to content

Instantly share code, notes, and snippets.

@JakubAndrysek
Last active April 16, 2024 21:23
Show Gist options
  • Save JakubAndrysek/fbcdf78f7bc91d905d22350e7cbcdb31 to your computer and use it in GitHub Desktop.
Save JakubAndrysek/fbcdf78f7bc91d905d22350e7cbcdb31 to your computer and use it in GitHub Desktop.
This Google Apps Script code will automatically close a Google Form when it reaches a maximum number of responses. It will also send an email notification to the form owner when the limit is reached.
/**
* Google Apps Script code to close a Google Form when it reaches a maximum number of responses.
* Author: Jakub Andrýsek
* Website: https://kubaandrysek.cz
* Email: email@kubaandrysek.cz
* GitHub: https://github.com/JakubAndrysek
* License: MIT
* File: https://gist.github.com/JakubAndrysek/fbcdf78f7bc91d905d22350e7cbcdb31
*/
// Number of maximum responses you want to allow before closing the form
const maxResponses = 42;
const ownerEmail = "send-info-to@email.com"; // Email address for notifications
const full = " (full)"
// Triggered automatically when a form response is submitted.
function onFormSubmit(e) {
// Get the Google Form object
var form = FormApp.getActiveForm();
var formId = form.getId();
// Get the actual number of responses the form has received so far
var numResponses = form.getResponses().length;
Logger.log(`${numResponses}/${maxResponses}`)
// Check if the limit has been reached
if (numResponses >= maxResponses) {
Logger.log("This form has reached the maximum number of responses. Sorry, you can't sign up anymore.")
// Display a message on the form
form.setCustomClosedFormMessage('This form has reached the maximum number of responses. Sorry, you can\'t sign up anymore.');
// Append "(obsazeno)" to the title if it's not already there
if (!form.getTitle().endsWith(full)) {
form.setTitle(form.getTitle() + full);
}
// Also rename the file on Google Drive if needed
var file = DriveApp.getFileById(formId);
if (!file.getName().endsWith(full)) {
file.setName(form.getTitle());
}
Logger.log("Rename to: " + form.getTitle());
// Close the form to prevent more responses
form.setAcceptingResponses(false);
// Send an email notification to the form owner
sendEmailToOwner(form);
Logger.log("Email sent to the form owner.");
}
}
// Sends an email to the form owner when the maximum limit is reached
function sendEmailToOwner(form) {
const title = DriveApp.getFileById(form.getId()).getName();
var subject = `Form "${title}" has reached the maximum number of responses`;
var body = `Your Google Form "${title}" has reached the maximum number of responses (${maxResponses}). Further sign-ups are not allowed.`;
// Send the email
GmailApp.sendEmail(ownerEmail, subject, body);
}

Google Form limit responses - Apps Script

This Google Apps Script code will automatically close a Google Form when it reaches a maximum number of responses. It will also send an email notification to the form owner when the limit is reached.

How to use

  1. Open your Google Form.
  2. Click on the three dots in the upper right corner.
  3. Click on "Script editor".
  4. Copy and paste the code from the code.gs file into the script editor.
  5. Save the script.
  6. Set the maxResponses variable to the number of responses you want to allow before closing the form.
  7. Set the ownerEmail variable to the email address where you want to receive notifications.
  8. Save the script again.
  9. Set up a trigger for the onFormSubmit function to run when a form response is submitted.
  10. Test the form by submitting responses until the limit is reached.
  11. Have great success! (you have successfully limited the number of responses on your Google Form wihthou any additional paid service :-).

Related links (found during publishing Gist)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment