问题
I have a question about how to set a list of choices in a Google Forms ListItem
which specifies not only the the choice value but also the goToPage()
pageBreak
destination according to the chosen value.
I am sorry for the length of the question: I prefer to give some background to make the issue clear. Thank you so much in advance!
The context is that of a greenhouse in which different actions have to be performed for each plant at different stages of the plant growth, such as sowing, transplanting, harvesting.
The plant name and their stage of growth are stored in a sheet, called plantProgress, which has this form:
|plantNumber | nextAction |
| ------------- ------------- |
|salad001 | harvest |
|salad002 | transplant |
|salad003 | sow |
The plantNumber
is updated when a new one is created (I have a pageBreakItem
for this) while the nextAction
value is updated in the sheet once the previous action has been completed.
When the user opens the form, he is presented with a ListItem
of the plantNumber
. Each of the nextAction
corresponds to a different page (i.e. pageBreakItem
)
Note that I already created in the Google Forms GUI both the initial page with the ListItem
and the subsequent pageBreakItems
with the values of nextAction, so I access to the ListItem
by its ID, e.g.
plantList = form.getItemById("999999999").asListItem()
I know how to dynamically create the list of choices values if a new plant is added: I create an array from the plantNumber
column (e.g. plantChoices
) and pass it to plantList
:
plantList.setChoiceValues(plantChoices)
What I would need to do is to also pass the destination pageBreakItem
(e.g. harvest) for each choice.
I tried to understand how to do this out by looking at the documentation for the class Choice but I couldn’t figure it out.
Ideally I would like to do something like:
plantList = form.getItemById("999999999").asListItem()
plantList.setChoices([
item.createChoice('salad001', FormApp.PageNavigationType.goToPage("harvest"))
item.createChoice('salad002', FormApp.PageNavigationType.goToPage("transplant"))
item.createChoice('salad003', FormApp.PageNavigationType.goToPage("sow"))
])
回答1:
I managed to solve the issue! I just needed to get the id of each defined pageBreakItem
(sow, transplant, harvest) and pass it to plantList.createChoice
as a second argument. Here's an example:
plantList = form.getItemById("999999999").asListItem()
sowPage = form.getItemById("111111111").asPageBreakItem();
transplantPage = form.getItemById("222222222").asPageBreakItem();
harvestPage = form.getItemById("333333333").asPageBreakItem();
plantList.setChoices([
plantList.createChoice('salad001', harvestPage),
plantList.createChoice('salad002', transplantPage),
plantList.createChoice('salad003', sowPage)
])
来源:https://stackoverflow.com/questions/53096914/google-script-forms-listitem-how-to-set-pagebreakitems-for-each-choice