如何使PageNavigationType通过循环工作

时间:2019-01-24 12:38:45

标签: forms google-apps-script

我正在尝试从Google表格中的某个范围收集信息,并使用Google脚本创建表单。根据表单内的用户输入,应将用户引导至表单内的特定页面。页面是通过循环创建的,我需要帮助才能在生成的多项选择中的createChoice项上放置正确的navigationType。

在Sheet范围上,例如Sheet1范围A1:D10,第一行中的数据(A1:D1)包含多选标题。 A2:D10行是选项。如果选项等于第一行中的范围/值,那么如果用户选择此选项,则他应该能够进入表单中的该页面。唯一的规则是,每列中的每个选项都应指向下一列,而不是之前,因此,B列中的选项不应指向A列,而只能指向C或D列。

您可能会找到Google表格here

下面的代码已经创建了此功能,但是对于所有页面,它们都卡在setGoToPages上。对我没有做对的事情有什么投入吗?

function TheForm() {
  var SS = SpreadsheetApp.getActiveSpreadsheet();
  var Sheet = SS.getSheetByName('Sheet1');
  var data = Sheet.getRange('A1:D10').getValues();

  //  Creating the form
  var form = FormApp.create('Input')
  .setDescription('Some description')
  .setConfirmationMessage('Have a fruity day:)')
  .setAllowResponseEdits(true)
  .setAcceptingResponses(true);

  //  adding the Fruits column in first page
  var itemPage1 = form.addListItem()
                  .setTitle('Fruits')
                  .setRequired(true);

  var PageNamesValues = [];
  for (var x=data[0].length-1; x>0; x--) {
      var choices = [];
      var choicesPage1 = []
      if (data[0][x] !== '') {
        // Create all other pages
        // My problem is how to set the page names so that I can refer to later!      
        var PageName = form.addPageBreakItem()
        .setTitle(data[0][x]);
        PageNamesValues.push(PageName.getTitle());
        Logger.log(PageName.getTitle());
        Logger.log(PageNamesValues);

        PageName.setGoToPage(FormApp.PageNavigationType.SUBMIT);

        var item = form.addMultipleChoiceItem()
        .setTitle(data[0][x])
        .showOtherOption(false)
        .setRequired(true);

        Logger.log('PageName is "'+PageName+'"');
        for (var z=1; z<data.length; z++) {  
          if (data[z][x] !== '') {
            Logger.log('data[z][x] = '+data[z][x]+' and asking to CONTINUE');
            choices.push(item.createChoice(data[z][x]));
            for (var zz=0; zz<PageNamesValues.length; zz++) {
              // if a choice equals a Page name then go to that Page 
              if (data[z][x] == PageNamesValues[zz]){
                // I have problems converting the PageNames[x] variable to the correct PageNavigationType
                Logger.log('We found option "'+data[z][x]+'" matching Page Name "'+PageNamesValues[zz]+'"');
                item.createChoice(data[z][x], PageName);
              }
            }
          }
        }
        Logger.log('Set all choices to: '+choices);
        item.setChoices(choices);
      }
    } 
        for (var z=1; z<data.length; z++) {  
          if (data[z][x] !== '') {
            Logger.log('data[z][x] = '+data[z][x]+' and asking to CONTINUE');
            choicesPage1.push(itemPage1.createChoice(data[z][x]));
            for (var zz=0; zz<PageNamesValues.length; zz++) {
              // if a choice equals a Page name then go to that Page 
              if (data[z][x] == PageNamesValues[zz]){
                // I have problems converting the PageNames[x] variable to the correct PageNavigationType
                Logger.log('We found option "'+data[z][x]+'" matching Page Name "'+PageNamesValues[zz]+'"');
                itemPage1.createChoice(data[z][x], PageName);
            }
          }
        }
        Logger.log('Set all choicesPage1 to: '+choicesPage1);
        itemPage1.setChoices(choicesPage1);
  }
}

以上代码的日志为:

  

冬天

     

[冬天]

     

PageName是“ PageBreakItem”

     

data [z] [x] =滑雪并询问继续

     

data [z] [x] =单板滑雪并询问继续

     

data [z] [x] =下雪并询问继续

     

data [z] [x] =下雨并要求继续

     

data [z] [x] =雪人并要求继续

     

将所有选项设置为:Choice,Choice,Choice,Choice,Choice

     

香蕉

     

[冬天,香蕉]

     

PageName是“ PageBreakItem”

     

data [z] [x] =黄色并要求继续

     

data [z] [x] =夏天并询问继续

     

数据[z] [x] =维生素C并要求继续

     

data [z] [x] =不含脂肪并要求继续

     

将所有选项设置为:Choice,Choice,Choice,Choice

     

苹果

     

[冬天,香蕉,苹果]

     

PageName是“ PageBreakItem”

     

数据[z] [x] =维生素C并要求继续

     

data [z] [x] =树并要求继续

     

data [z] [x] =红色并要求继续

     

data [z] [x] =绿色并要求继续

     

data [z] [x] =冬季并要求继续

     

我们找到与页面名称“ Winter”匹配的选项“ Winter”

     

将所有选项设置为:Choice,Choice,Choice,Choice,Choice

     

data [z] [x] =苹果并要求继续

     

我们找到与页面名称“ Apple”匹配的选项“ Apple”

     

将所有choicesPage1设置为:Choice

     

data [z] [x] =香蕉并要求继续

     

我们找到了与页面名称“香蕉”匹配的选项“香蕉”

     

将所有choicesPage1设置为:Choice,Choice

     

data [z] [x] =橙色并要求继续

     

将所有choicesPage1设置为:Choice,Choice,Choice

     

data [z] [x] =杏并询问继续

     

将所有choicesPage1设置为:Choice,Choice,Choice,Choice

     

data [z] [x] =黑加仑并要求继续

     

将所有choicesPage1设置为:Choice,Choice,Choice,Choice,Choice

     

data [z] [x] =蓝莓并要求继续

     

将所有choicesPage1设置为:Choice,Choice,Choice,Choice,Choice,Choice

     

data [z] [x] =樱桃并询问继续

     

将所有choicesPage1设置为:Choice,Choice,Choice,Choice,Choice,Choice,Choice

     

将所有choicesPage1设置为:Choice,Choice,Choice,Choice,Choice,Choice,Choice

     

将所有choicesPage1设置为:Choice,Choice,Choice,Choice,Choice,Choice,Choice

1 个答案:

答案 0 :(得分:0)

文档中提到的

Page Navigation不能具有混合导航类型,即,您不能定义某些选项来导航到特定页面,而将其余部分留空。即使您明确将它们设置为在代码中更早地提交,您仍为找不到匹配项的每个选择再次设置了它。像这样:

for (var z=1; z<data.length; z++) {  
        if (data[z][x] !== '') {
          Logger.log('data[z][x] = '+data[z][x]);        
          var notFound = 1                              //whether page with name was found?
          for (var zz=0; zz<PageNames.length; zz++) {
            if (data[z][x] == PageNamesValues[zz]){     //When the page is found, since both arrays have the same index
              choices.push(item.createChoice(data[z][x], PageNames[zz]));  //you use same index to find set navigation
              notFound = 0
              Logger.log('We found option "'+data[z][x]+'" matching Page Name "'+PageNamesValues[zz]+'"');
              Logger.log('Choice navigation '+ choices[choices.length -1].getGotoPage().getTitle())
            } 
          }
          if (notFound == 1){
            // You cannot have certain choice navigate to a page and remaining not,
            // you will have to explicitly define that they submit if those choices are selected
           choices.push(item.createChoice(data[z][x],FormApp.PageNavigationType.SUBMIT));
          }
        }
      }

第二,将pageBreakItem视为每个部分开头出现的表单中的另一个元素。它们将表单的流程从一个部分引导到另一部分,可以将其设置为使用功能setGoToPage()

将流程引导到另一部分。
// Set to navigate from the first page to the last pageBreakItem    
      if (x==0) {
         // Incorrect Line: PageNames[data[0].length-1].setGoToPage(PageNames[x])
         PageNames[PageNames.length-1].setGoToPage(PageNames[x])
      }

但是同时使用choices(pageBreakItem)setGoToPage(pageBreakItem)函数中的参数时,它可以同时充当转发地址:

choices.push(item.createChoice(data[z][x], PageNames[zz]));

程序决定何时转发或何时充当地址的方式取决于是否通过"normal linear progression through the form"到达了页面导航 。或通过函数显式地定向到它。 希望清除如何使用分页符项来指导页面导航

这是最终代码:

function fruitForm() {
  var SS = SpreadsheetApp.getActiveSpreadsheet();
  var Sheet = SS.getSheetByName('Sheet1');
  var data = Sheet.getDataRange().getValues();
  var form = FormApp.openById('FormID')
  .setDescription('Some description')
  .setConfirmationMessage('Have a fruity day:)')
  .setAllowResponseEdits(true)
  .setAcceptingResponses(true);
  Logger.log(form.getId())
  // Reset Multiple choice naviagtion , to enable forms to delete it. 
  var mulChoiceItems = form.getItems(FormApp.ItemType.MULTIPLE_CHOICE)
  for (var i=0; i<mulChoiceItems.length; i++) {
    //Input dummy values to remove page navigation
    var item = mulChoiceItems[i].asMultipleChoiceItem().setChoiceValues(["a","b"])
  } 

  //Delete Previous Form values
  var items = form.getItems();
  for (var i=0; i<items.length; i++) {
      form.deleteItem(0);
  }

  //  asking user for name on the first page
  var text = form.addTextItem().setTitle('What is your name?').isRequired()
  var PageNames = [];
  var PageNamesValues = [];


  var temp =0;
  for (var x=data[0].length-1; x>=0; x--) {
    var choices = [];
    if (data[0][x] !== '') {
      temp = temp+1;     
      //Store pageBreakItem and their titles in the arrays
      //Note the arrays are filled in the same order for both pageBreakItem and their names
     //Thus preseving the relative index positions
      PageNames[x] = form.addPageBreakItem()
      .setTitle(data[0][x]);
      PageNamesValues[x] = PageNames[x].getTitle();
      Logger.log(PageNames);
      Logger.log(PageNamesValues);
      // I set all pages to SUBMIT except of the temp=1 (1st page)
      if (temp !== 1){
        PageNames[x].setGoToPage(FormApp.PageNavigationType.SUBMIT);
      }
      // Set to navigate from the first page to the last pageBreakItem    
      if (x==0) {
         // Incorrect Line: PageNames[data[0].length-1].setGoToPage(PageNames[x])
         PageNames[PageNames.length-1].setGoToPage(PageNames[x])
      }

      var item = form.addMultipleChoiceItem()
      .setTitle(data[0][x])
      .showOtherOption(false)
      .setRequired(true);

      Logger.log(PageNames[x]);
      for (var z=1; z<data.length; z++) {  
        if (data[z][x] !== '') {
          Logger.log('data[z][x] = '+data[z][x]);        
          var notFound = 1                              //whether page with name was found?
          for (var zz=0; zz<PageNames.length; zz++) {
            if (data[z][x] == PageNamesValues[zz]){     //When the page is found, since both arrays have the same index
              choices.push(item.createChoice(data[z][x], PageNames[zz]));  //you use same index to find set navigation
              notFound = 0
              Logger.log('We found option "'+data[z][x]+'" matching Page Name "'+PageNamesValues[zz]+'"');
              Logger.log('Choice navigation '+ choices[choices.length -1].getGotoPage().getTitle())
            } 
          }
          if (notFound == 1){
            // Not you cannot have certain choice navigate to a page and remaining not,
            // you have to explicit define that they submit if those choices are selected
           choices.push(item.createChoice(data[z][x],FormApp.PageNavigationType.SUBMIT));
          }
        }
      }
      item.setChoices(choices);
    }
  }
}