How can I script a Google Form from a Spreadsheet to go to a specific page based on an answer?

后端 未结 1 662
野性不改
野性不改 2021-01-24 13:48

I have successfully created a Google form that populates from a spreadsheet using code adapted from here: https://www.youtube.com/watch?v=BYA4URuWw0s . Now I would like to make

相关标签:
1条回答
  • 2021-01-24 14:26

    PageBreakItem

    A layout item that marks the start of a page. Items can be accessed or created from a Form.

    PageNavigationType

    An enum representing the supported types of page navigation. Page navigation types can be accessed from FormApp.PageNavigationType.

    The page navigation occurs after the respondent completes a page that contains the option, and only if the respondent chose that option. If the respondent chose multiple options with page-navigation instructions on the same page, only the last navigation option has any effect. Page navigation also has no effect on the last page of a form.

    Choices that use page navigation cannot be combined in the same item with choices that do not use page navigation.

    // Create a form and add a new multiple-choice item and a page-break item.
    var form = FormApp.create('Form Name');
    var item = form.addMultipleChoiceItem();
    var pageBreak = form.addPageBreakItem();
    
    // Set some choices with go-to-page logic.
    var rightChoice = item.createChoice('Vanilla', FormApp.PageNavigationType.SUBMIT);
    var wrongChoice = item.createChoice('Chocolate', FormApp.PageNavigationType.RESTART);
    
    // For GO_TO_PAGE, just pass in the page break item. For CONTINUE (normally the default), pass in
    // CONTINUE explicitly because page navigation cannot be mixed with non-navigation choices.
    var iffyChoice = item.createChoice('Peanut', pageBreak);
    var otherChoice = item.createChoice('Strawberry', FormApp.PageNavigationType.CONTINUE);
    item.setChoices([rightChoice, wrongChoice, iffyChoice, otherChoice]);
    

    You can follow the sample create by Google and the implementation used in Mogsdad's answert to further understand the flow.

    I've added his code snippet:

    function createForm() {
    // Create a form and add a new multiple-choice item and a page-break item.
    var form = FormApp.create('Form Name');
    var item = form.addMultipleChoiceItem();
    item.setTitle('Do you like Food')
    var page2 = form.addPageBreakItem()
    .setTitle('Page 2')
    var item2 = form.addTextItem();
    item2.setTitle('Why do you like food?')
    var page3 = form.addPageBreakItem()
    .setTitle('Page 3')
    var item3 = form.addTextItem();
    item3.setTitle("Why don't you like food?") 
    
    item.setTitle('Question')
    .setChoices([
    item.createChoice('Yes',page2),
    item.createChoice('No',page3)
    ]);
    }
    
    0 讨论(0)
提交回复
热议问题