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

后端 未结 3 1368
旧时难觅i
旧时难觅i 2020-11-28 12:54

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:

Ques

相关标签:
3条回答
  • 2020-11-28 13:33

    The other answer to this question will keep adding a multichoice select each time for the form is submitted. Using similar approach of:

    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

    I've used the following code to modify a list select which could be easiliy modified for a multiple choice.

    function updateForm(){
      var form = FormApp.openById('YOUR_FORM_ID'); // Base form  
      // need to read what dates are available and which are taken
      var doc = SpreadsheetApp.getActiveSpreadsheet();
      var dates = doc.getRange("dates!A1:A10").getValues(); //available options
      var taken_dates = doc.getRange("responses!F2:F51").getValues(); //just getting first 50 responses 
      // joining the taken dates into one string instead of an array to compare easier
      var taken_dates_string = taken_dates.join("|");
    
      var choice = [];
      // loop through our available dates
      for (d in dates){
        // test if date still available
        if (dates[d][0] != "" && taken_dates_string.indexOf(dates[d][0]) === -1){ 
          choice.push(dates[d][0]); // if so we add to temp array
        }
      }
      var formItems = form.getItems(FormApp.ItemType.LIST); // our form list items
      // assumption that first select list is the one you want to change
      // and we just rewrite all the options to ones that are free
      formItems[0].asListItem().setChoiceValues(choice); 
    }
    
    0 讨论(0)
  • 2020-11-28 13:35

    You are not able to create this type of dynamic form using the Google Forms Service, because there is no interaction between the service and scripts during form entry, except upon Form Submission. In the case of a multi-page form, a script has no way to know that a student has completed one page and gone on to another.

    You could achieve this using the HtmlService or UiService, though. In either case, you'd rely on the client-side form interacting through server-side scripts to get updated lists of course options, then modifying the next 'page'. It will be complex.

    0 讨论(0)
  • 2020-11-28 13:50

    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.

    0 讨论(0)
提交回复
热议问题