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

enter image description here

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 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> 

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.

37.4k 9 9 gold badges 75 75 silver badges 181 181 bronze badges asked Dec 10, 2019 at 15:46 user3288051 user3288051 604 1 1 gold badge 15 15 silver badges 31 31 bronze badges

2 Answers 2

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> //Submit programmatically the form response response.submit(); //URL with prefilled form response Logger.log(response.toPrefilledUrl()); > function test ()