Dynamic selection options in Google Apps Script for forms

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 23:25:09

问题


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?


回答1:


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:

Google Apps Script Forms Service

addPageBreakItem

createChoice




回答2:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!