user3288051
user3288051

Reputation: 614

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

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

enter image description here

Whenever the form is loaded (opened), I need to set a location value (loc) to this existing textbox and show it to the user.

function populateMemberIds(loc){
    var form = FormApp.openById(formUrl);
    var questions = form.getItems();
    for (var i=0; i<questions.length; i++){
       if(questions[i].getTitle()=="Which location was this performed at?"){
       var textItem = questions[i].asTextItem();
       //I get stuck here
    }

}

I already setup the openForm trigger which allows to run the populateMemberIds function to be run on each form load. Again, what I need is to change the value of the "Your answer" section of the text item with the location value (loc).

I would appreciate any help.

Upvotes: 0

Views: 6561

Answers (2)

Andres Duarte
Andres Duarte

Reputation: 3340

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

Upvotes: 2

Wicket
Wicket

Reputation: 38150

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

Upvotes: 1

Related Questions