3 March 2014

How to schedule and limit your google forms

How to schedule and limit your google forms
Google Forms impose no limits. Any poll or survey created inside Google Forms has no expiration date and it can collect unlimited number of responses until the form owner decides to manually close* the form. But you can schedule these from and close it by trigger. Suppose anyone wants to set their form before 5 days and think after 5 days it will open for general user. For this situation you need to apply trigger on script editor by using some lines of Google Script.


Setting Expiration date and time of form:
Google script editor give flexibilty to set your expiration date and limiting your forms as following.
1.Go to google drive and click on existing or create new form
2.Inside editor click on Tools->Script editor.
3.Click on Form
4.Set DATE_OPEN   =  "2014-01-04 06:00". On this time your form will be visible.
5.Set DATE_CLOSE =  "2014-03-2 11:59". After this dste snd time your form will be invisible.
6.Set MAX_RESPONE   =  "200". This will set your maximum number of response.
7.(CNTL+S) save your script.

 Google Script for Setting expiration and Limiting response

DATE_OPEN   =  "2014-01-04 06:00"
DATE_CLOSE =  "2014-03-2 11:59"
MAX_RESPONE   =  "200"


/* Initialize the form, setup time based triggers */
function Initialize() {
 
  deleteTriggers_();
 
  if (DATE_OPEN !== "") {
    closeForm();
    ScriptApp.newTrigger("openForm")
    .timeBased()
    .at(parseDate_(DATE_OPEN))
    .create();
  }
 
  if (DATE_CLOSE!== "") {
    ScriptApp.newTrigger("closeForm")
    .timeBased()
    .at(parseDate_(FORM_CLOSE_DATE))
    .create();
  }
 
  if (MAX_RESPONE !== "") {
    ScriptApp.newTrigger("checkLimit")
    .forForm(FormApp.getActiveForm())
    .onFormSubmit()
    .create();
  }
 
}

/* Delete all existing Script Triggers */
function deleteTriggers_() { 
  var triggers = ScriptApp.getScriptTriggers(); 
  for (var i in triggers) {
    ScriptApp.deleteTrigger(triggers[i]);
  }
}

/* Send a mail to the form owner when the form status changes */
function informUser_(subject) {
  var formURL = FormApp.getActiveForm().getPublishedUrl();
  MailApp.sendEmail(Session.getActiveUser().getEmail(), subject, formURL); 
}

/* Allow Google Form to Accept Responses */
function openForm() {
  var form = FormApp.getActiveForm();
  form.setAcceptingResponses(true);
  informUser_("Your Google Form is now accepting responses");
}

/* Close the Google Form, Stop Accepting Reponses */
function closeForm() { 
  var form = FormApp.getActiveForm();
  form.setAcceptingResponses(false);
  deleteTriggers_();
  informUser_("Your Google Form is no longer accepting responses");
}

/* If Total # of Form Responses >= Limit, Close Form */
function checkLimit() {
  if (FormApp.getActiveForm().getResponses().length >= MAX_RESPONE ) {
    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));

}  

                                                           Thank you for Visiting blog.only4student.in