如何使用应用程序脚本设置Google表单问题的转到部分 [英] How to set the go to sections on a Google Forms question using app script
问题描述
我有一个Google表单,其中某些字段是静态"内容,而某些字段是由Google工作表中的应用脚本填充的,该工作表每天都会运行.我几乎按我的意愿拥有了它,但是还不够.
I have a Google form where some of the fields are "static" content, and some are populated by app script from a Google sheet which will be run on a daily basis. I nearly have it how I want it, but not quite.
我有几个下拉列表,其中需要包含导航到表单不同部分的选择,例如:
I have a couple of dropdown lists that need to contain choices that navigate to different sections of the form, e.g:
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.
What's happening is that instead of appending choices to the list, I'm overwriting which means I only end up with ONE choice in my dropdown, which is the last one added, i.e. Fred in the example above.
我已经在网络上查看了很多示例,但是无法使其正常工作.我觉得我已经很近了,但还没到那儿.这是我的代码,有几行我认为是问题所在.有人可以告诉我我要去哪里了吗.
I've looked at lots of examples on the web but can't get this to work. I feel I'm very close but not quite there yet. Here's my code with a couple of lines that I believe are the problem. Can someone please tell me where I'm going wrong.
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
时指定的内容添加到该项目中.
Issue:
When setChoices is used, all choices that were previously stored in the item get removed. Only the ones that are specified when using setChoices
get added to the item.
现在,您仅指定一种选择:
Right now, you are only specifying one choice:
playersClubsFormList.setChoices([playersClubsFormList.createChoice(playersClubsArray[j], sectionMalePlayers)]);
解决方案:
您必须指定项目要具有的所有选择.因此,在这种情况下,您将必须执行以下操作:
Solution:
You have to specify all choices you want the item to have. So in this case you would have to do the following:
- 检索以前通过 getChoices存储的所有选择.这将检索一个包含该项目中所有当前选择的数组.
- 使用 Array.prototype.push()将要添加的选择添加到选择列表中.
- 使用
setChoices
时,应提供在步骤1中检索并在步骤2中修改的数组作为参数.
- Retrieve all choices that were previously stored via getChoices. This will retrieve an array with all current choices in the item.
- Use Array.prototype.push() to add the choice you want to add to the list of choices.
- When using
setChoices
, the array retrieved in step 1 and modified in step 2 should be provided as the argument.
更改此:
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)]);
参考:
- getChoices()
- setChoices(choices)
这篇关于如何使用应用程序脚本设置Google表单问题的转到部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!