Creating A Google Form Using Google Apps Script

I recently learned that you can create a Google Form by calling a function in Google Sheets. I am going to demonstrate how this is possible using Google Apps Scripts.

function createTestForm() {
  var form = FormApp.create('This is a test form');

  // First question - What percent?
  var percentQuestion = form.addMultipleChoiceItem();
  percentQuestion.setTitle('What percent?');
  percentQuestion.setChoices([
    percentQuestion.createChoice('25%'),
    percentQuestion.createChoice('50%'),
    percentQuestion.createChoice('75%'),
    percentQuestion.createChoice('100%')
  ]);

  // Second question - What's your email?
  var emailQuestion = form.addTextItem();
  emailQuestion.setTitle("What's your email?");
  emailQuestion.setValidation(FormApp.createTextValidation()
    .requireTextIsEmail()
    .build());

  // Third question - Enter a number
  var numberQuestion = form.addTextItem();
  numberQuestion.setTitle('Enter a number');
  numberQuestion.setValidation(FormApp.createTextValidation()
    .requireNumber()
    .build());

  Logger.log('Form URL: ' + form.getPublishedUrl());
  Logger.log('Editor URL: ' + form.getEditUrl());
}

The code above does the following

  • Creates a form and calls it “This is a test form”
  • Creates the first question with 4 multiple choice options
  • Creates the second question that is free response
  • Creates the third question where the response is validated as a number

The above should give a taste for what is possible. This process is also useful if you want to share the Google Sheets file that logs the responses of the Form / if the Google Sheets file has code that runs when the form is submitted.

Sharing

Related Articles

  • All Post
  • Articles
  • Blog Post
  • General Business Automation
  • Portfolio
  • Stock Market & Finance