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

拟墨画扇 提交于 2019-12-20 05:16:12

问题


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 the form go to a specific question based on the answer of a previous question without losing the function of updating from the spreadsheet. I have tried to alter code from here: How to assign Go_To_Page in Form Service on Multiple Choice? , but have yet to have any luck. Below is what I have as of now. I am very new to coding and any help will be greatly appreciated. Thanks in advance!!!

//insert your form ID
var FORM_ID = '1iA8jX720Eqi_GKwiA1RJiWwzt3vJ8XOOAqh-SjO9mXM';
//insert your sheet name with a list
var EMP_SHEET_NAME = 'empROSTER';
//insert your range of lis
//insert list id (before run getItemsOfForm function and look at t as A1Notation
var range1 = 'B2:B';
//insert list id (before run getItemsOfForm function and look at log CTRL+Enter)
var ITEM_ID = '1097376824';

  /* 
  add date question? - 
    form.addDateItem()
        .setTitle('Date of Work Performed')
  */

//insert your sheet name with a list
var JOB_SHEET_NAME = 'jobROSTER';
//insert your range of list as A1Notation
var range2 = 'C2:C';
//insert list id (before run getItemsOfForm function and look at log CTRL+Enter)
var ITEM2_ID = '773657499';

//insert your sheet name with a list
var AT_SHEET_NAME = '300';
//insert your range of list as A1Notation
var range3 = 'B2:B';
//insert list id (before run getItemsOfForm function and look at log CTRL+Enter)
var ITEM3_ID = '1884670833';

//insert your sheet name with a list
var DT_SHEET_NAME = '1000';
//insert your range of list as A1Notation
var range4 = 'B2:B';
//insert list id (before run getItemsOfForm function and look at log CTRL+Enter)
var ITEM4_ID = '379969286';

//insert your sheet name with a list
var CT_SHEET_NAME = '2000';
//insert your range of list as A1Notation
var range5 = 'B2:B';
//insert list id (before run getItemsOfForm function and look at log CTRL+Enter)
var ITEM5_ID = '128282987';

//insert your sheet name with a list
var HOURS_SHEET_NAME = 'hourROSTER';
//insert your range of list as A1Notation
var range6 = 'B2:B';
//insert list id (before run getItemsOfForm function and look at log CTRL+Enter)
var ITEM6_ID = '1385334889';


  //Section in question
  function createGoTo(){
    var af = FormApp.openById(FORM_ID);
    var pAdmin = af.getItemById(AT_SHEET_NAME);  //use findPageIds to get the page id of a pre-created page
    var pDesign = af.getItemById(DT_SHEET_NAME);
    var pCon = af.getItemById(CT_SHEET_NAME);
    var item = ITEM2_ID
    //create choices as an array
    var choices = [];
    choices.push(item.createChoice('Administration Job', pAdmin));
    choices.push(item.createChoice('Design Job', pDesign));
    choices.push(item.createChoice('Construction Job', pCon));

    // assignes the choices for the question
    item.setChoices(choices);
  }

  function findPageIds(){
    var af = FormApp.getActiveForm();
    var pages = af.getItems(FormApp.ItemType.PAGE_BREAK);

    for (i in pages){
      var pName = pages[i].getTitle();
      var pId = pages[i].getId(); 
      Logger.log(pName+":"+pId); 
    }  
  }
  //End of section in question


function updateEmpName() {
var values_ = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(EMP_SHEET_NAME).getRange(range1).getValues();
values_[0][0] = values_[0][0].toString();
for(var i = 1; i < values_.length; i++){
 // the if-block from https://productforums.google.com/d/msg/docs/v8ejYFpoGa8/HVqg6auIAg0J
if(values_[i][0] != "") {
  values_[0].push(values_[i][0].toString())
  }
 }
 var form = FormApp.openById(FORM_ID);
 var list = form.getItemById(ITEM_ID);
 list.asListItem().setChoiceValues(values_[0]);
}

function updateJobName() {
var values2_ = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(JOB_SHEET_NAME).getRange(range2).getValues();
 values2_[0][0] = values2_[0][0].toString();
     for(var i = 1; i < values2_.length; i++){
    // the if-block from https://productforums.google.com/d/msg/docs/v8ejYFpoGa8/HVqg6auIAg0J
  if(values2_[i][0] != "") {
      values2_[0].push(values2_[i][0].toString())
 }
 }
  var form = FormApp.openById(FORM_ID);
 var list = form.getItemById(ITEM2_ID);
 list.asListItem().setChoiceValues(values2_[0]);
}

function updateAdminTasks() {
 var values3_ = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(AT_SHEET_NAME).getRange(range3).getValues();
 values3_[0][0] = values3_[0][0].toString();
 for(var i = 1; i < values3_.length; i++){
  // the if-block from https://productforums.google.com/d/msg/docs/v8ejYFpoGa8/HVqg6auIAg0J
  if(values3_[i][0] != "") {
   values3_[0].push(values3_[i][0].toString())
  }
 }
 var form = FormApp.openById(FORM_ID);
  var list = form.getItemById(ITEM3_ID);
  list.asListItem().setChoiceValues(values3_[0]);
}

function updateDesignTasks() {
 var values4_ = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(DT_SHEET_NAME).getRange(range4).getValues();
 values4_[0][0] = values4_[0][0].toString();
  for(var i = 1; i < values4_.length; i++){
   // the if-block from https://productforums.google.com/d/msg/docs/v8ejYFpoGa8/HVqg6auIAg0J
   if(values4_[i][0] != "") {
    values4_[0].push(values4_[i][0].toString())
  }
 }
 var form = FormApp.openById(FORM_ID);
  var list = form.getItemById(ITEM4_ID);
  list.asListItem().setChoiceValues(values4_[0]);
}

function updateConstructionTasks() {
 var values5_ = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(CT_SHEET_NAME).getRange(range5).getValues();
 values5_[0][0] = values5_[0][0].toString();
 for(var i = 1; i < values5_.length; i++){
   // the if-block from https://productforums.google.com/d/msg/docs/v8ejYFpoGa8/HVqg6auIAg0J
   if(values5_[i][0] != "") {
     values5_[0].push(values5_[i][0].toString())
   }
  }
 var form = FormApp.openById(FORM_ID);
 var list = form.getItemById(ITEM5_ID);
  list.asListItem().setChoiceValues(values5_[0]);
}

function updateHours() {
 var values6_ = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(HOURS_SHEET_NAME).getRange(range6).getValues();
 values6_[0][0] = values6_[0][0].toString();
for(var i = 1; i < values6_.length; i++){
  // the if-block from https://productforums.google.com/d/msg/docs/v8ejYFpoGa8/HVqg6auIAg0J
 if(values6_[i][0] != "") {
   values6_[0].push(values6_[i][0].toString())
  }
 }
 var form = FormApp.openById(FORM_ID);
 var list = form.getItemById(ITEM6_ID);
 list.asListItem().setChoiceValues(values6_[0]);
}

function getItemsOfForm(){
 var form = FormApp.openById(FORM_ID);
 var items = form.getItems(FormApp.ItemType.LIST);
 for(var i in items){
  Logger.log('id: ' + items[i].getId() + ' title: ' + items[i].getTitle());
 }
}

回答1:


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)
]);
}


来源:https://stackoverflow.com/questions/38316334/how-can-i-script-a-google-form-from-a-spreadsheet-to-go-to-a-specific-page-based

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!