// Forms · Apps Script

Jump to a section based on an answer in Google Forms.

Use Apps Script to wire Google Forms branching by attaching PageBreakItem targets to each choice — the one thing the UI editor makes tedious and scripts make repeatable.

I want to programmatically set up section branching in Google Forms so different answers route respondents to different parts of the form.

The script

copy · paste · trigger
setBranching.gs
Apps Script
// Route respondents to different sections based on a single-choice answer.
// Requires: one MultipleChoiceItem, two PageBreakItems already in the form.
function setBranching() {
  var form = FormApp.openById('YOUR_FORM_ID');
  var items = form.getItems();

  var question = items[0].asMultipleChoiceItem();
  var sectionA  = items[1].asPageBreakItem(); // shown if 'Yes'
  var sectionB  = items[2].asPageBreakItem(); // shown if 'No'

  question.setChoices([
    question.createChoice('Yes', sectionA),
    question.createChoice('No',  sectionB)
  ]);

  Logger.log('Branching applied to: ' + question.getTitle());
}

Need a variant? Gnaw writes a custom version from one sentence — fields, triggers, edge cases handled.

Walkthrough

Why the Forms UI becomes painful fast

Google Forms lets you set go-to-section navigation on individual choices through the form editor, but the moment you have more than a handful of choices or need to rebuild the form programmatically, clicking through each option becomes unreliable bookkeeping. The first time I restructured a twelve-section intake form by hand, I introduced two silent routing errors that only surfaced when a respondent emailed me asking why they were seeing irrelevant questions.

The script approach is reproducible: run it once after you've set up your section structure, and branching is consistent every time. It also makes the routing logic readable in version control rather than buried in form metadata.

How the branching model actually works

The key thing to understand is that branching is a property of the choice, not the section. You attach a destination PageBreakItem to each Choice object when you construct it via createChoice(value, pageBreakItem). The destination must be an actual PageBreakItem object retrieved from the form — not a string name, not an index number.

Only MultipleChoiceItem and ListItem (dropdown) support this two-argument form of createChoice. CheckboxItem and GridItem have no equivalent method; calling setChoices on them with PageBreakItem targets will throw an error or silently ignore the navigation. This is why scripts that generically loop over 'add navigation to all items' often silently fail on checkbox questions.

The snippet above retrieves items by index. In a real form you'll usually want to retrieve them by title instead: form.getItems() returns everything in order, and inserting a new question shifts all subsequent indices. Filtering by ItemType or matching on getTitle() is more resilient.

Targeting items by title instead of index

A more durable version filters the item list before casting. Call form.getItems(FormApp.ItemType.PAGE_BREAK) to get only page-break items, then match on item.getTitle() to find the right section. The same works for your branching question: form.getItems(FormApp.ItemType.MULTIPLE_CHOICE) narrows the list before you call asMultipleChoiceItem().

One real gotcha: if you have two sections with the same title, getTitle() matching returns the first one. Give every section a unique title in the form editor before running the script — it takes thirty seconds and saves a confusing debug session later.

After running setBranching(), open the live form editor and click through each choice to verify the go-to destination shows correctly. The editor will display the section name next to the choice; if it shows 'Continue to next section' the PageBreakItem reference was wrong.

Want a custom version?

Describe your sheet and the rule you want. Gnaw writes the Apps Script — fields, triggers, edge cases — in one shot.

FAQ

4 questions
Can I set branching on a checkbox question in Google Forms?
No. CheckboxItem and GridItem do not support per-choice navigation. Only MultipleChoiceItem and ListItem (dropdown) accept a PageBreakItem as the second argument to createChoice. If you call the two-argument version on a checkbox item, Apps Script will throw a TypeError.
What happens to a choice that has no section target — does it go to the next section?
Yes. createChoice('value') with no second argument defaults to continuing to the next section in order. You only need to pass a PageBreakItem for choices that should skip ahead or route to a non-adjacent section.
How do I send a respondent to the form's confirmation page from a choice?
Pass FormApp.PageNavigationType.SUBMIT as the second argument to createChoice instead of a PageBreakItem. For example: question.createChoice('Done', FormApp.PageNavigationType.SUBMIT). This terminates the form at that choice.
My script runs without errors but the branching is not showing in the form editor — what went wrong?
The most common cause is that items[n] is not actually a PageBreakItem at that index. Log item.getType() for each item to verify. Also confirm you are opening the form by its ID and not a copy; changes write to the form the ID points to, not necessarily the one you have open in the browser.