Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

Dynamically edit multiple choice options in live Google Form using Apps Script

I'm a high school teacher in L.A. trying to create a course registration system using Apps Script. I need the Google Form I'm using for this registration to:

Question 1) Update the choices available in subsequent multiple choice questions on new pages based on a student's current response choices.

Question 2) Eliminate choices from the form when a multiple choice option has reached it's "cap".

Question 1 Example) A student registers for “tie-tying” in workshop 1, and gets taken to a new page. The Script edits the available choices on that new page based on the student’s first choice, and removes “tie-tying” from the list of possible choices on that new page, so “etiquette” is their only remaining option.

Question 2 Example) Students can either register for “tie-tying” or “etiquette”, both responses are initially available in the Google Form. 30 students take the survey, all 30 register for the “tie-tying” workshop. The Apps Script references the response spreadsheet, realizes the “tie-tying” workshop is full, then removes it from the Google Form's list of possible choices. Student 31 goes to register, and their only option is “etiquette”.

If my question has already been asked and answered (believe me, I did search!) I'd appreciate the redirection.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I believe we can achieve your second objective without too much difficulty and modify the form, based on the current state of response.

The approach is to

  1. Create the form and associate it with a response spreadsheet
  2. In that response spreadsheet, create a script with a function (updateForm for instance)
  3. Bind that function with the onFormSubmit event, see Using Container-Specific Installable Triggers.
  4. Analyse the response in the updateForm function and modify your form using the Form Service

For instance

function updateForm(e) {
  if (e.values[1] == 'Yes') {
    Logger.log('Yes');
    var existingForm = FormApp.openById('1jYHXD0TBYoKoRUI1mhY4j....yLWGE2vAm_Ux7Twk61c');
    Logger.log(existingForm);
    var item = existingForm.addMultipleChoiceItem();
     item.setTitle('Do you prefer cats or dogs?')
     .setChoices([
         item.createChoice('Cats'),
         item.createChoice('Dogs')
      ])
     .showOtherOption(true);
  }
}

When it comes to achieving the goal in your first question, its more delicate, as the form will not submit mid way. What is possible is to go to different pages based on different responses to a Multiple Choice question, your use case may fit this method, although its not very dynamic.

Further its possible to use html Service to create completely dynamic experience.

Let me know if you need further information.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...