Skip to content

Instantly share code, notes, and snippets.

@creotiv
Last active June 21, 2020 13:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save creotiv/9351de10e6fed38b8dff to your computer and use it in GitHub Desktop.
Save creotiv/9351de10e6fed38b8dff to your computer and use it in GitHub Desktop.
Setting limits for Google Form
/* Time zone used from your Google Callendar */
FORM_OPEN_DATE = "2014-12-20 08:00";
FORM_CLOSE_DATE = "2014-12-25 23:30";
RESPONSE_COUNT = "100";
/* Init the from and set triggers */
function Initialize() {
deleteTriggers_();
if ((FORM_CLOSE_DATE !== "") &&
((new Date()).getTime() >= parseDate_(FORM_CLOSE_DATE).getTime())) {
closeForm();
}
if ((FORM_OPEN_DATE !== "") &&
((new Date()).getTime() < parseDate_(FORM_OPEN_DATE).getTime())) {
closeForm();
ScriptApp.newTrigger("openForm")
.timeBased()
.at(parseDate_(FORM_OPEN_DATE))
.create();
}
if (FORM_CLOSE_DATE !== "") {
ScriptApp.newTrigger("closeForm")
.timeBased()
.at(parseDate_(FORM_CLOSE_DATE))
.create();
}
if (RESPONSE_COUNT !== "") {
ScriptApp.newTrigger("checkLimit")
.forForm(FormApp.getActiveForm())
.onFormSubmit()
.create();
}
}
/* Remove all existing triggers */
function deleteTriggers_() {
var triggers = ScriptApp.getProjectTriggers();
for (var i in triggers) {
ScriptApp.deleteTrigger(triggers[i]);
}
}
/* Send email notification when from status changed */
function informUser_(subject) {
var formURL = FormApp.getActiveForm().getPublishedUrl();
MailApp.sendEmail(Session.getActiveUser().getEmail(), subject, formURL);
}
/* Set form to accept responses */
function openForm() {
var form = FormApp.getActiveForm();
form.setAcceptingResponses(true);
informUser_("You Google form is active.");
}
/* Closing the form */
function closeForm() {
var form = FormApp.getActiveForm();
form.setAcceptingResponses(false);
deleteTriggers_();
informUser_("You Google Form is closed.");
}
/* If total form responses >= then set limit, then we close the form */
function checkLimit() {
if (FormApp.getActiveForm().getResponses().length >= RESPONSE_COUNT ) {
closeForm();
}
}
/* Parse the Date for creating Time-Based Triggers */
function parseDate_(d) {
return new Date(d.substr(0,4), d.substr(5,2)-1,
d.substr(8,2), d.substr(11,2), d.substr(14,2));
}
/* Written by Amit Agarwal amit@labnol.org */
@raja04
Copy link

raja04 commented May 22, 2018

Hey Amit,

I hope your doing good.

Your code is awesome !!! but for me it is getting error.Please find the screeshot.

i have created google form for employee attedance tracking.. but the form should not open after 10AM. Can you please provide the code for this requirement..

Can you please share your contact details..

Thanks in advance !!!!

@raja04
Copy link

raja04 commented May 22, 2018

image

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