Automating Google Forms Creation with Google Apps Script

Google Apps Script is a powerful tool that allows you to automate tasks and create custom functionalities within Google Workspace. In this blog post, we will walk through an exercise to write a script that creates a Google Form with three questions: one multiple choice, one short answer, and one paragraph question. Additionally, we will explore how you can create and customize Google Forms using Apps Script.

Prerequisites

Before we begin, ensure you have:

  • A Google account
  • Basic knowledge of JavaScript
  • Access to Google Forms

Step 1: Writing the Script

  1. Open the Apps Script Editor:
    • Go to your Google Drive, click on the + New button, select More, and then choose Google Apps Script to open the script editor.
  2. Create a New Script:
    • Replace any code in the script editor with the following code to create a Google Form with three specific questions:

function createGoogleForm() {

  // Create a new form

  const form = FormApp.create(‘Sample Form’);

  // Add a multiple choice question

  form.addMultipleChoiceItem()

      .setTitle(‘What is your favorite color?’)

      .setChoices([

        form.createChoice(‘Red’),

        form.createChoice(‘Blue’),

        form.createChoice(‘Green’),

        form.createChoice(‘Yellow’)

      ]);

  // Add a short answer question

  form.addTextItem()

      .setTitle(‘What is your name?’);

  // Add a paragraph question

  form.addParagraphTextItem()

      .setTitle(‘Please describe your experience with our product.’);

  Logger.log(‘Form created with ID: ‘ + form.getId());

}

Step 2: Running the Script

  1. Save and Run the Script:
    • Save your script by clicking the floppy disk icon or pressing Ctrl+S (Windows) or Cmd+S (Mac).
    • Click on the Run button (the play icon) to execute the createGoogleForm function.
  2. Authorize the Script:
    • The first time you run the script, you will need to authorize it. Click on Review Permissions and follow the steps to grant the necessary permissions.
  3. Check Your Google Drive:
    • After running the script, go to your Google Drive. You should see a new form named “Sample Form” with the specified questions.

Step 3: Customizing the Script

  1. Adding More Questions:
    • You can add various types of questions, such as checkboxes, drop-downs, linear scales, etc.

// Add a checkbox question

form.addCheckboxItem()

    .setTitle(‘Which of the following fruits do you like?’)

    .setChoices([

      form.createChoice(‘Apple’),

      form.createChoice(‘Banana’),

      form.createChoice(‘Cherry’),

      form.createChoice(‘Date’)

    ]);

// Add a drop-down question

form.addListItem()

    .setTitle(‘Select your country’)

    .setChoices([

      form.createChoice(‘United States’),

      form.createChoice(‘Canada’),

      form.createChoice(‘United Kingdom’),

      form.createChoice(‘Australia’)

    ]);

  1. Customizing Form Appearance:
    • Customize the form’s appearance by setting its description, confirmation message, and other properties.

form.setDescription(‘Please fill out this form to help us understand your preferences.’)

    .setConfirmationMessage(‘Thank you for your response!’)

    .setAllowResponseEdits(true)

    .setAcceptingResponses(true);

  1. Sending the Form:
    • You can automate the process of sending the form to respondents.

function sendForm() {

  const form = FormApp.openById(‘YOUR_FORM_ID’);

  const email = ‘respondent@example.com’;

  const subject = ‘Please fill out this form’;

  const message = ‘We appreciate your feedback. Please fill out this form: ‘ + form.getPublishedUrl();

  MailApp.sendEmail(email, subject, message);

}

Conclusion

With this exercise, you’ve learned how to use Google Apps Script to create and customize a Google Form. This script adds a multiple choice question, a short answer question, and a paragraph question to the form. By automating the creation and customization of Google Forms, you can save time and streamline your data collection processes. Explore further customizations to tailor your forms to your specific needs and enhance your productivity within the Google Workspace ecosystem.