I have this form:
If you see List Items - Student Name and Grades are getting populated from a Google Sheet.
What I want to do now is upon selection of List Item Grade, show only those students that belong to that particular grade in the "Select Student" selection...
How can it be done, because form has only two triggers: onOpen and onEdit?
As far as I know, this type of client side event does not exist in Google Forms. The scripting mechanisms of Google Apps Script are designed for creating forms and other documents, but do not reach very far for client-side functionality.
What I suggest is that you split the form into multiple pages and use the "Go to page based on answer" feature to achieve your desired functionality.
Use form.addPageBreakItem()
to create pages and use the item.createChoice(value, PageBreakItem)
to navigate to the correct page.
References:
Edit: Thanks to the additional information in follow up comments, the question has been clarified, and boils down to this: "As a user makes selections in a Google Form, is it possible to customize the options in other questions on that form?"
A: No. See Dynamically edit multiple choice options in live Google Form using Apps Script.
Original Answer - this explains how one could change the options presented in a form when the source of those options (a spreadsheet) was edited.
If your data is in a Google Spreadhsheet, an onEdit
or onChange
trigger in the source spreadsheet would be able to respond to changes in the name and grade lists.
The onChange
trigger is "installable", and is able to perform actions that a simple onEdit
can't, such as modify a form, so it would be the better choice in this case.
In the trigger function, use FormsApp.openById()
or FormsApp.openByUrl()
, then update the previous item choices with item.setChoices()
, reading the values from the spreadsheet just as you did when creating the form.
For bonus marks... instead of blindly overwriting the choices, you could read the existing choices and update them only if changed.
Caveat - on a sheet that changes frequently, or with long lists, this trigger will be computationally expensive. You may find that you run into Google's processing limits.
来源:https://stackoverflow.com/questions/20489864/dynamic-selection-options-in-google-apps-script-for-forms