How to dynamically change an existing text item value on Google Forms using Google Apps Script

后端 未结 2 909
感动是毒
感动是毒 2020-12-22 09:55

I have an existing Google Form in which there is a TextItem with a title \"Which location was this performed at?\".

Whenever the form is loaded (opened), I

相关标签:
2条回答
  • 2020-12-22 10:25

    The onOpen Google Apps Script triggers (simple and installable) for Google Forms are executed only when the form is opened in the form editor, not when the form is opened by using the view / edit response links.

    There are two ways to "prefill" a Google Forms response:

    1. Use the prefilled response URL
    2. Create a response programmatically, then use the editResponseUrl

    Related

    • Is it possible to 'prefill' a google form using data from a google spreadsheet?
    • How to generate a pre-filled form URL for Google Form
    0 讨论(0)
  • 2020-12-22 10:41

    You can't modify a form response filled by a user, you can either create a form response programmatically or edit a response after being submitted. The onOpen form trigger runs when someone opens the form to edit it rather than answer it [1]:

    This event does not occur when a user opens a form to respond, but rather when an editor opens the form to modify it.

    Moreover, triggers functions comes with an event parameter already defined [1] so you can't set your own function parameter(s) as you're doing with your loc parameter.

    EDIT

    You can programmatically create and submit a form response [2], from which you can also get a URL with a prefilled form for the user to finish [3].

    function populateMemberIds(loc){
      var form = FormApp.openById("[FORM-ID]");
      var questions = form.getItems();
      var response = form.createResponse(); 
    
      for (var i=0; i<questions.length; i++){
        if(questions[i].getTitle()=="title form"){//Which location was this performed at?"){
          var textItem = questions[i].asTextItem();
          var itemResponse = textItem.createResponse(loc) ;
          response.withItemResponse(itemResponse);
        }
      }
      //Submit programmatically the form response
      response.submit();
      //URL with prefilled form response
      Logger.log(response.toPrefilledUrl()); 
    }
    
    function test () {
      populateMemberIds("US");
    }
    

    [1] https://developers.google.com/apps-script/guides/triggers/events#google_forms_events

    [2] https://developers.google.com/apps-script/reference/forms/form-response

    [3] https://developers.google.com/apps-script/reference/forms/form-response#toprefilledurl

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