Create Google Form with Script

Create Google Form with Google Script

Steps

  1. Use Forms Service

    // Create a new form, then add a checkbox question, a multiple choice question,
    // a page break, then a date question and a grid of questions.
    var form = FormApp.create('New Form');
    var item = form.addParagraphTextItem();
    item.setTitle('Name')
    .setRequired(true);
    form.addTextItem()
    .setTitle('Phone Number')
    .setRequired(true);
    var item2 = form.addCheckboxItem();
    item2.setTitle('What condiments would you like on your hot dog?');
    item2.setChoices([
        item2.createChoice('Ketchup'),
        item2.createChoice('Mustard'),
        item2.createChoice('Relish')
    ]);
    form.addMultipleChoiceItem()
    .setTitle('Do you prefer cats or dogs?')
    .setChoiceValues(['Cats','Dogs'])
    .showOtherOption(true);
    form.addPageBreakItem()
    .setTitle('Getting to know you');
    form.addDateItem()
    .setTitle('When were you born?');
    form.addGridItem()
    .setTitle('Rate your interests')
    .setRows(['Cars', 'Computers', 'Celebrities'])
    .setColumns(['Boring', 'So-so', 'Interesting']);
    Logger.log('Published URL: ' + form.getPublishedUrl());
    Logger.log('Editor URL: ' + form.getEditUrl());
  2. Once Script is copied, it will ask for permission, grand it

  3. Associate a spreadsheet

  4. Add response validation (eg: email, number, regexp (phone ^([0-9]{5})\s?([0-9]{5})$, indian date format ((0[1-9])|(1[0-9])|(2[0-9])|(3[0-1]))[\/-]((0[1-9])|(1[0-2]))[\/-](\d{4}) etc) if needed
    eg: ParagraphTextValidation

A Generic Registration form

function myFunction() {
  createGenericRegistrationForm();
}
function createGenericRegistrationForm(){
  // Create a new form, then add a checkbox question, a multiple choice question,
// a page break, then a date question and a grid of questions.
var form = FormApp.create('Generic Registration Form');
var item = form.addParagraphTextItem();
item.setTitle('Name')
    .setRequired(true);
form.addMultipleChoiceItem()
    .setTitle('Gender')
    .setChoiceValues(['Male','Female','Transgender'])
    .setRequired(true);
form.addTextItem()
    .setTitle('Date of Birth')
    .setValidation(getDOBValidation())
    .setRequired(true);
form.addTextItem()
    .setTitle('Phone Number')
    .setValidation(getPhoneNumberValidation())
    .setRequired(true);
form.addTextItem()
    .setTitle('Email')
    .setValidation(getEmailValidation())
    .setRequired(true);    
var item = form.addParagraphTextItem();
item.setTitle('Address :')
    .setRequired(true);
form.addTextItem()
    .setTitle('Name of the person who invited you:');
Logger.log('Published URL: ' + form.getPublishedUrl());
Logger.log('Editor URL: ' + form.getEditUrl());
}
// email regexp [a-zA-Z0-9_\.\+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-\.]+
//phone num ^([0-9]{5})\s?([0-9]{5})$
//date ((0[1-9])|(1[0-9])|(2[0-9])|(3[0-1]))[\/-]((0[1-9])|(1[0-2]))[\/-](\d{4})
function getPhoneNumberValidation(){
    var phoneNumberValidation = FormApp.createTextValidation()
    .setHelpText('Enter a proper 10 digit phone number') 
    .requireTextContainsPattern('^([0-9]{5})\\s?([0-9]{5})$')   
    .build();
  return phoneNumberValidation;
}
function getDOBValidation(){
  var dOBValidation = FormApp.createTextValidation()
    .setHelpText('Enter date of birth in dd-mm-yyy format')
    .requireTextContainsPattern('((0[1-9])|(1[0-9])|(2[0-9])|(3[0-1]))[\\/-]((0[1-9])|(1[0-2]))[\\/-](\\d{4})')
    .build();
  return dOBValidation;
}
function getEmailValidation(){
  var emailValidation = FormApp.createTextValidation()
    .setHelpText('Enter a valid email')
    .requireTextContainsPattern('[a-zA-Z0-9_\\.\\+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-\\.]+')  
    .build();
  return emailValidation;
}