Suppose I have a Google Forms that has only one item: a short answer item. As soon as I submit the form I would like a script so that it changes the response. For example, whatever the user enters to the question, I would like a script to change it to be "foo". The form responses Google Sheet should also show "foo" as the response.
I know it involves something like ScriptApp.newTrigger("edit_response").forForm(form).onFormSubmit().create();
where form = FormApp.getActiveForm()
and edit_response
is a function that takes the event e
. But I can't seem to find a method that can modify form responses...
A Form response inside of a Google Form can NOT be edited with Apps Script. A response saved to a spreadsheet can be changed, but an existing response in a Form can not be changed. A Form response can be deleted with code, but not edited with any Apps Script methods.
You can manually set up an On Form Submit trigger. There is no need to create a trigger from code unless you are doing something like creating new Forms with code, and distributing them automatically. Or having people install an Add-on to the Form.
Form responses are always written into the Form. You can also set a spreadsheet as the response destination. In that case, the Form answers will be saved to both the Form and a spreadsheet. You can not intercept the data before it's written into the Form. But you can change it immediately after it's written.
The function that serves as the On Form Submit trigger can receive a JavaScript object with Form data. The variable name used for the event object is often the letter "e", because the letter "e" is the first character in the word "Event." But the variable name can can be anything.
function myOnFormSubmitFunc(e) {};
An On Form Submit trigger can be either in the Form, the Sheet, or both. The On Form Submit object for the Sheet is different than for the Form. For example, with the Sheet On Form Submit object, you can directly get an array of values.
var arrayOfValues = e.values;
Apps Script Documentation - Sheets Form Submit events
There is a FormResponse class:
Apps Script Documentation - FormResponse
But it can not edit an existing Form response in the Form.
来源:https://stackoverflow.com/questions/36637753/edit-form-responses-as-soon-as-it-is-submitted