Creating form with a script : Page Break and go to

2019-01-19 22:39发布

问题:

Before I ask my question, I want to point out that I'm french, so I may have a bad english. If my problem is not understood, I'll try to explain it in better ways.

I am currently working on a google script to create a form. In this form I want to ask respondent several multiple choice question. In order to get good and specific results, I want to adapt the questions I ask to the profile of the respondent.

To make it clear this is the shape of my form : I start by asking the respondent a first question with 5 possible answer. Upon the answer, the respondent will have to be directed to a new page. On this new page, there is a new multiple choice question with 2 possible answers. Again, upon the answer, the respondent has to be directed to a new page and so on... At the end, when I get all the details I want, I ask precise questions to the respondent. The final page is a page containing a lot of multiple choice question, which all respondent will have to answer, no depending of their profile.

I have tried to write it on google script this way :

var page1 = form.addPageBreakItem()
    .setTitle('First page');
var item = form.addMultipleChoiceItem();
 item.setTitle('Question')
     .setChoices([
         item.createChoice('Yes',page2),
         item.createChoice('No',page3)
       ]);
var page2 = form.addPageBreakItem()
    .setTitle('Second page');
var page3 = form.addPageBreakItem()
    .setTitle('Third page');

But as page2 and page3 are defined after page1, the script does not understand it, and cannot jump to page3 or page2 upon the answer. I have tried to defined all my page first, but then I don't know how to add my multiple answer question into a specific page.

Does anybody can give me a hint ? Thanks for reading anyway, and don't hesitate to ask for more explanations if it is not clear

回答1:

You only need to change the order of your operations; you do not need to keep the operations on item together. Here's an example, using your code:

function createForm() {
  var title = 'Multipage Form Test';
  var description = 'Stackoverflow question 17083500';

  var form = FormApp.create(title)
      .setDescription(description)
      .setConfirmationMessage('Thanks for responding!');

  var page1 = form.addPageBreakItem()
      .setTitle('First page');
  var item = form.addMultipleChoiceItem();
  var page2 = form.addPageBreakItem()
      .setTitle('Second page');
  var page3 = form.addPageBreakItem()
      .setTitle('Third page');

  item.setTitle('Question')
      .setChoices([
         item.createChoice('Yes',page2),
         item.createChoice('No',page3)
       ]);

  Logger.log(form.getEditUrl());
  Logger.log(form.getPublishedUrl());
}

Copy the "edit URL" from the Logs, and check - you'll find that the form has the behavior you are looking for: