如何使用应用程序脚本设置 Google 表单问题的转到部分

How to set the go to sections on a Google Forms question using app script

我有一个 Google 表单,其中一些字段是 "static" 内容,一些字段由来自 Google sheet 的应用程序脚本填充,这将是 运行 每天。我几乎得到了我想要的,但不完全是。

我有几个下拉列表需要包含导航到表单不同部分的选项,例如:

Phill / Beacon Hill - Go to section called "Beacon Hill
Jane  / Harbord     - Go to section called "Harbord"
Fred / Beacon Hill  - Go to section called "Beacon Hill"
etc...

发生的事情是,我没有将选项附加到列表,而是覆盖,这意味着我最终在下拉列表中只有一个选项,这是最后添加的选项,即上例中的 Fred。

我在网上查看了很多示例,但无法使其正常工作。我觉得我已经很接近了,但还没有到那一步。这是我的代码,其中有几行我认为是问题所在。谁能告诉我哪里出错了。

function populatePlayersClubsListV2() {
// ------------------------------------------------------------------------------------------------
// This gets each male player and the junior club they've been assigned to from the 
// Junior_Clubs_Training_Sessions s/sheet (which is this s/sheet). It creates a list that the player 
// chooses their name from and sets up a branch to the appropriate Club training sessions section 
// in the form depending on which club they're assigned to. .
// ------------------------------------------------------------------------------------------------  

  // Open the "Find Junior Club Training Sessions" form 
  var form = FormApp.openById("1fo33ncgJY.................iRnMsERRTen8WjTH_xrx0");

  // Identify the sheet in this spreadsheet holding the data needed to populate the drop-down. Here 
  // it's the "PlayersClubs" tab which says which club each player is assigned to.
  var ss = SpreadsheetApp.getActive();
  var playersClubsSheet = ss.getSheetByName("PlayersClubs");

  // Grab the values from the rows & columns of the sheet - use 2 to skip header row. Use getMaxRows 
  // and getMaxColumns to avoid hard-coding the number of rows and columns.
  var playersClubsSsValues = playersClubsSheet.getRange(2, 1, playersClubsSheet.getMaxRows() - 1, playersClubsSheet.getMaxColumns() - 1).getValues();

  // We need to loop thro twice - once to populate the male players, and again for the female players.
  // Males/females populate different fields and we hold the data-item-IDs of those fields in an array.
  var formFieldsArray = [
                         ["Male", 1397567108],
                         ["Female", 1441402031]
                        ];

  for(var h = 0; h < formFieldsArray.length; h++) {

    // Open the form field you want to populate - it must be a dropdown or multiple choice.
    // Right-click field, inspect and look for data-item-ID followed by a number.
    var playersClubsFormList = form.getItemById(formFieldsArray[h][1]).asListItem();

    // Define array to hold values coming from the s/sheet and used to populate form fields.
    var playersClubsArray = [];    

    var sectionMalePlayers = form.getItemById(309334479).asPageBreakItem();
    var sectionFemalePlayers = form.getItemById(856495273).asPageBreakItem();

    // Create the array of players and their clubs ignoring empty cells. Check if the s/sheet row  
    // matches male/female against formFieldsArray[h][0].
    for(var i = 0, j = 0; i < playersClubsSsValues.length; i++) {
      if(playersClubsSsValues[i][0] != "" && playersClubsSsValues[i][1] == formFieldsArray[h][0]) {
        playersClubsArray[j] = playersClubsSsValues[i][0] + " - " + playersClubsSsValues[i][2];
        if (formFieldsArray[h][0] = "Male") {
          // ** THIS IS THE LINE THAT OVERWRITES BUT I NEED IT TO APPEND *** //
          playersClubsFormList.setChoices([playersClubsFormList.createChoice(playersClubsArray[j], sectionMalePlayers)]);
        }
        else {
          // ** THIS IS THE LINE THAT OVERWRITES BUT I NEED IT TO APPEND *** //
          playersClubsFormList.setChoices([playersClubsFormList.createChoice(playersClubsArray[j], sectionFemalePlayers)]);
        }

        playersClubsFormList.setChoices([playersClubsFormList.createChoice(playersClubsArray[j], sectionMalePlayers)]);   
        j = j + 1;
      } // end if
    } // end for loop
  } // end for loop for Males/Females 
}

问题:

当使用 setChoices 时,之前存储在项目中的所有选项都将被删除。只有使用 setChoices 时指定的才会添加到项目中。

现在,您只指定了一个选项:

playersClubsFormList.setChoices([playersClubsFormList.createChoice(playersClubsArray[j], sectionMalePlayers)]);

解决方案:

您必须指定您希望项目具有的所有选项。因此,在这种情况下,您必须执行以下操作:

  • 检索之前通过 getChoices 存储的所有选项。这将检索包含项目中所有当前选项的数组。
  • 使用Array.prototype.push()将您要添加的选项添加到选项列表中。
  • 使用 setChoices 时,应提供在步骤 1 中检索并在步骤 2 中修改的数组作为参数。

代码示例:

改变这个:

playersClubsFormList.setChoices([playersClubsFormList.createChoice(playersClubsArray[j], sectionMalePlayers)]);

为此:

var choices = playersClubsFromList.getChoices();
choices.push(playersClubsFormList.createChoice(playersClubsArray[j], sectionMalePlayers));
playersClubsFormList.setChoices(choices);

注:

  • 必须对发生此问题的所有行进行相同的更改,例如:
playersClubsFormList.setChoices([playersClubsFormList.createChoice(playersClubsArray[j], sectionFemalePlayers)]);

参考: