Snippets · 8 functions
Google Forms snippets
Eight short functions for common Google Forms tasks: opening a form, listing questions, reading the latest response, updating dropdown choices, and toggling whether the form accepts responses.
How to use these
Set up before you run
Copy a snippet into a script file, read its setup and notes, change the placeholder values, and save. For functions meant to run manually, choose the function name in the toolbar and press Run. Results appear in the Execution log unless the description says otherwise.
1
Get the active form and log its title
Reads the title of the Google Form this script project is bound to.
function logActiveFormTitle() {
const form = FormApp.getActiveForm();
Logger.log('Form title: ' + form.getTitle());
} 2
Open a Google Form by ID
Opens a Google Form from a standalone script or Google Sheet using the ID in its edit URL.
function logFormById() {
const form = FormApp.openById('REPLACE_WITH_FORM_ID');
Logger.log('Opened form: ' + form.getTitle());
} Good to know. Copy the ID between /forms/d/ and /edit in the form editor URL, not the responder link.
3
List all question titles and types
Reads every item on the active form and logs its title and item type.
FormApp.FormFormApp.Itemfunction logFormQuestions() {
const form = FormApp.getActiveForm();
form.getItems().forEach((item) => {
Logger.log(item.getTitle() + ' (' + item.getType() + ')');
});
} 4
Count total form submissions
Reads all stored submissions on the active form and logs the total count.
function logResponseCount() {
const form = FormApp.getActiveForm();
const responses = form.getResponses();
Logger.log('Total responses: ' + responses.length);
} 5
Read the most recent submission
Logs every question title and answer from the newest response on the active form.
function logLatestFormResponse() {
const form = FormApp.getActiveForm();
const responses = form.getResponses();
if (responses.length === 0) {
Logger.log('No responses have been submitted yet.');
return;
}
const latest = responses[responses.length - 1];
latest.getItemResponses().forEach((itemResponse) => {
Logger.log(itemResponse.getItem().getTitle() + ': ' + itemResponse.getResponse());
});
} 6
Update choices on a dropdown question
Finds a dropdown question by title and replaces its list of selectable options.
function setDepartmentDropdownChoices() {
const form = FormApp.getActiveForm();
const targetTitle = 'Department';
const item = form.getItems(FormApp.ItemType.LIST).find((candidate) => candidate.getTitle() === targetTitle);
if (!item) throw new Error('No dropdown question titled ' + targetTitle + '.');
item.asListItem().setChoiceValues(['Operations', 'Finance', 'Support']);
Logger.log('Updated dropdown choices for ' + targetTitle + '.');
} 7
Close a form to new responses
Turns off response collection on the active form and sets the message visitors see.
function closeFormWithMessage() {
const form = FormApp.getActiveForm();
form.setAcceptingResponses(false);
form.setCustomClosedFormMessage('Registration is now closed.');
Logger.log('Form closed to new responses.');
} 8
Add a multiple-choice question
Appends a required multiple-choice question with three options to the active form.
FormApp.FormFormApp.MultipleChoiceItemfunction addSessionChoiceQuestion() {
const form = FormApp.getActiveForm();
form
.addMultipleChoiceItem()
.setTitle('Preferred session')
.setChoiceValues(['Morning', 'Afternoon', 'Evening'])
.setRequired(true);
Logger.log('Added multiple-choice question.');
} Keep going
See these in a complete task
- Task guide Respond to a Google Form submission
Get an email every time someone submits your Google Form, listing each question and its answer. The email goes only to you, the owner of the script.
- Task guide Send a confirmation to the form respondent
Send each person who submits your Google Form a short thank-you email that repeats their answers. The form must collect verified email addresses.
- Task guide Populate a Google Form dropdown from Sheets
Read a list of choices from column A of a Google Sheet and push them into a dropdown question on a Google Form, removing blank cells and duplicates first.
- Basics Google Apps Script triggers explained
Google Apps Script triggers run functions automatically when a file opens, a cell changes, a form is submitted, or a scheduled time arrives. Use a simple onEdit or onOpen for work in the bound file. Choose an installable trigger for a schedule, form submission, or work that needs authorization.
- Basics Apps Script permissions and authorization
Before a script can read your sheet or send your mail, you give it permission to access a list of OAuth scopes. This page walks through each authorization screen, explains @OnlyCurrentDoc, and shows how to revoke access.