user4935235
user4935235

Reputation:

Modify form navigation via script

I want to use a script to change the behavior of an existing Google Form so that after completing page 1, future respondents will go to another page (say, page 3).

I know this involves using .setGoToPage(), but I don't how to set the parameters to make this modification.

Take into account that the pages in the form are already created, so I just need to indicate that page 1 should go to page 3. To clarify, my problem is that I need to find "page 1" without its name being known so that, afterwards, I can apply the ".setGoToPage(pageThree);" command to page 3 (whose name is also not known).

The real goal here is actually to set page 1 to go back to itself so that no one can see the options of page two, essentially blocking the form from being used. I just worded it this way to be able to undo it later on. It must be a script because it will be added to another script that does other things. I cannot go into the form and do this manually.

 function myFunction() {
 var form = FormApp.openById("ID GOES HERE");
 var item = form.getItems(itemType);
 // here is where I need the rest of the script 

 .setGoToPage("PAGE THREE GOES HERE");
}    

Upvotes: 1

Views: 2674

Answers (3)

codepants
codepants

Reputation: 55

Grab page 2 and setGoToPage to FormApp.PageNavigationType.RESTART.

Page 2's pageNavigationType is where page 1 goes after you complete it without choosing an option that redirects you. To go to page 1, use FormApp.PageNavigationType.RESTART. To go to any other page, use that page as a pageBreakItem.

I realize this topic is nearly three years old but, as there is a better answer, I thought I'd reply for anyone who stumbles across it.

Upvotes: 0

user4935235
user4935235

Reputation:

Excellent Mogsdad. I could not use the second option you gave because the goal is to make sure the users cannot see the rest of the form, because the form essentially is an exam, so just turning off responses would be insufficient.

BUT, you indirectly solved the problem because what I did was put an empty extra page in between page one and two (making page two really page three) and used your script but slightly modified and it has achieved what I was aiming for.

function myFunction() {
 var form = FormApp.openById('FORM ID HERE');
 var pageBreaks = form.getItems(FormApp.ItemType.PAGE_BREAK);
 pageBreaks[0].asPageBreakItem().setGoToPage(pageBreaks[0].asPageBreakItem());
 pageBreaks[1].asPageBreakItem().setGoToPage(pageBreaks[0].asPageBreakItem());
}

Upvotes: 1

Mogsdad
Mogsdad

Reputation: 45710

Good news, bad news. First the good news.

You were on the right track; you can get all the page break items via Form.getItems(FormApp.ItemType.PAGE_BREAK), which will result in an ordered array of sections. The "first page" (section 1) ends with page break #0, the second with page break #1, and so on.

So modifying the form to go to section 3 after section 1 is easy:

function myFunction() {
  var formId = '--form-id--';

  var form = FormApp.openById(formId);
  var pageBreaks = form.getItems(FormApp.ItemType.PAGE_BREAK);
  pageBreaks[0].asPageBreakItem().setGoToPage(pageBreaks[1].asPageBreakItem());
}

Don't forget the .asPageBreakItem() calls, since the results from .getItems() are generic Items, but you need a PageBreakItem specifically, in order to use .setGoToPage().

Now the Bad News... Sections end with page breaks, so there is none to set as a destination for the first section.

However, since you want to effectively disable the form, you could just stop accepting responses.

function myFunction() {
  var formId = '--form-id--';

  var form = FormApp.openById(formId);
  form.setAcceptingResponses(false);
}

Upvotes: 1

Related Questions