0% found this document useful (0 votes)
19 views

Google Apps Script for Google Forms_ Comprehensive Guide

Uploaded by

contraste visual
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Google Apps Script for Google Forms_ Comprehensive Guide

Uploaded by

contraste visual
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Google Apps Script for Google Forms: Comprehensive Guide

Google Apps Script for Google Forms: Comprehensive Guide 1


What is Google Apps Script for Forms? 2
How to Use Google Apps Script for Forms 2
Key FormApp Methods 2
Basic Examples 3
Example 1: Create a New Form 3

Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis

1
Example 2: Add a Multiple-Choice Question 3
Example 3: Handle Form Responses 4
Advanced Examples 4
Example 4: Add Conditional Logic to a Form 4
Example 5: Create a Form and Link It to a Google Sheet 4
Exercises 5
Exercise 1: Create a Form with Text and Multiple-Choice Questions 5
Solution: 5
Exercise 2: Count Total Responses 5
Solution: 5
Exercise 3: Trigger an Email on Form Submission 5
Solution: 5
Multiple-Choice Questions 6
Question 1: 6
Question 2: 6
Question 3: 6
Advanced Example: Dynamic Form Creation 7
Best Practices 7

Google Apps Script for Forms enables you to automate and customize Google Forms by
creating forms programmatically, managing responses, and integrating them with other Google
Workspace apps. This guide includes examples, detailed explanations, exercises, and
multiple-choice questions to help you master Google Forms automation.

What is Google Apps Script for Forms?

Google Apps Script provides the FormApp service to create, customize, and manage Google
Forms programmatically. You can:

● Automate form creation.


● Handle form submissions.
● Customize form appearance and functionality.

How to Use Google Apps Script for Forms

1. Open the Apps Script Editor.


2. Create a new script.
3. Save and authorize the script to manage Google Forms.

Key FormApp Methods

Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis

2
1. FormApp.create(): Creates a new form.
2. addTextItem(): Adds a text question.
3. addMultipleChoiceItem(): Adds a multiple-choice question.
4. getResponses(): Retrieves form responses.
5. onFormSubmit(): Handles form submission triggers.

Basic Examples

Example 1: Create a New Form


function createForm() {
const form = FormApp.create("Customer Feedback");
form.setTitle("Customer Feedback Form");
form.setDescription("We value your feedback. Please fill out this
form.");
}

Explanation:

● FormApp.create("Customer Feedback"): Creates a new form with the title


"Customer Feedback."
● setTitle() and setDescription(): Set the form's title and description.

Example 2: Add a Multiple-Choice Question


function addMultipleChoice() {
const form = FormApp.getActiveForm();
form.addMultipleChoiceItem()
.setTitle("How did you hear about us?")
.setChoices([
"Online Search",
"Social Media",
"Friend/Family",
"Other"
]);
}

Explanation:

● addMultipleChoiceItem(): Adds a multiple-choice question.


● setChoices(): Defines the available choices.

Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis

3
Example 3: Handle Form Responses
function logResponses() {
const form = FormApp.getActiveForm();
const responses = form.getResponses();
responses.forEach((response) => {
Logger.log(response.getItemResponses().map((item) =>
item.getResponse()).join(", "));
});
}

Explanation:

● getResponses(): Retrieves all form responses.


● getItemResponses(): Accesses individual item responses.

Advanced Examples

Example 4: Add Conditional Logic to a Form


function addConditionalLogic() {
const form = FormApp.getActiveForm();
const question = form.addMultipleChoiceItem()
.setTitle("Do you want to receive updates?")
.setChoices(["Yes", "No"]);
question.setGoToPage(FormApp.PageNavigationType.SUBMIT, "No");
}

Explanation:

● setGoToPage(): Adds conditional navigation logic.

Example 5: Create a Form and Link It to a Google Sheet


function createFormWithSheet() {
const form = FormApp.create("Survey");
form.setTitle("Survey Form");
form.setDestination(FormApp.DestinationType.SPREADSHEET,
SpreadsheetApp.create("Survey Responses").getId());
form.addTextItem().setTitle("What is your name?");
form.addMultipleChoiceItem()
.setTitle("Do you like our product?")

Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis

4
.setChoices(["Yes", "No"]);
}

Explanation:

● setDestination(type, id): Links the form to a spreadsheet for responses.

Exercises

Exercise 1: Create a Form with Text and Multiple-Choice Questions

Write a script to create a form with:

1. A text question: "What is your email?"


2. A multiple-choice question: "Rate our service (1 to 5)."

Solution:
function createFeedbackForm() {
const form = FormApp.create("Feedback Form");
form.addTextItem().setTitle("What is your email?");
form.addMultipleChoiceItem()
.setTitle("Rate our service (1 to 5)")
.setChoices(["1", "2", "3", "4", "5"]);
}

Exercise 2: Count Total Responses

Write a script to count and log the total number of responses submitted to a form.

Solution:
function countResponses() {
const form = FormApp.getActiveForm();
const responses = form.getResponses();
Logger.log(`Total responses: ${responses.length}`);
}

Exercise 3: Trigger an Email on Form Submission

Write a script to send an email whenever a form submission occurs.

Solution:
function onFormSubmit(e) {
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis

5
const responses = e.response.getItemResponses();
const email = "[email protected]";
const responseText = responses.map((item) =>
`${item.getItem().getTitle()}: ${item.getResponse()}`).join("\n");
GmailApp.sendEmail(email, "New Form Submission", responseText);
}

Explanation:

● Attach this function as a trigger for the "On Form Submit" event.

Multiple-Choice Questions

Question 1:

Which method is used to create a new Google Form?

1. FormApp.newForm()
2. FormApp.createForm()
3. FormApp.create()
4. FormApp.new()

Answer: 3. FormApp.create()

Question 2:

What does addTextItem() do in a Google Form?

1. Adds a title to the form.


2. Adds a short-answer text question.
3. Adds a paragraph question.
4. Adds a multiple-choice question.

Answer: 2. Adds a short-answer text question.

Question 3:

How can you link a Google Form to a Google Sheet?

1. setDestination(DestinationType, SpreadsheetID)
2. linkToSheet(SpreadsheetID)
3. setDestination(FormApp.DestinationType.SPREADSHEET,
SpreadsheetID)
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis

6
4. connectSheet(SpreadsheetID)

Answer: 3. setDestination(FormApp.DestinationType.SPREADSHEET,
SpreadsheetID)

Advanced Example: Dynamic Form Creation


function createDynamicForm() {
const form = FormApp.create("Dynamic Form");
form.setTitle("Event Registration");
const questions = [
{ type: "text", title: "What is your name?" },
{ type: "multipleChoice", title: "Are you attending?", choices:
["Yes", "No"] },
{ type: "text", title: "If yes, how many guests will you bring?" }
];
questions.forEach((question) => {
if (question.type === "text") {
form.addTextItem().setTitle(question.title);
} else if (question.type === "multipleChoice") {

form.addMultipleChoiceItem().setTitle(question.title).setChoices(quest
ion.choices);
}
});
}

Explanation:

● Dynamically creates a form based on an array of questions.


● Adds text and multiple-choice questions programmatically.

Best Practices

1. Test Scripts Thoroughly: Avoid errors in live forms.


2. Use Triggers for Automation: Set up triggers for actions like email notifications or
response processing.
3. Organize Responses: Link forms to Google Sheets for better response management.

Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis

7
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis

You might also like