Skip to content

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.

Code.gs
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.

Code.gs
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.Item
Code.gs
function 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.

Code.gs
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.

Code.gs
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.

FormApp.FormFormApp.ItemFormApp.ListItem
Code.gs
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.

Code.gs
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.MultipleChoiceItem
Code.gs
function 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

All snippet collections