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.
Related
I wrote a script for a Google Sheet that emails a user some specific info relevant to them alone. It works fine with the onOpen trigger, but I don't want them to have to open the sheet each time.
It would be easier to submit a form request and then receive the email but I'm not sure how to do that. I'm sure this is very basic so apologies for that, I'm new to this.
Basically I would like to form to ask if they want to receive an email with the info, and if they say yes, it triggers the script.
There are two ways to do this. The first is to:
Link the form responses to the spreadsheet
Add the installable trigger on form submit to the spreadsheet on the function you want to run.
Get the necessary information from the event object to see who submitted the form.
The second way is to:
Place a script on form.
Put an onFormSubmit() trigger on the form
Have the form open the spreadsheet - SpreadsheetApp.openById()
Copy paste the spreadsheet code into the form, making adjustments, so it will run (will mostly be how to get the user data)
If you would like this to only occur if the respondent asks, you can place a yes/no question on the form and check the answer. You can also have a questionless form that only collects emails, if you just want them to submit it to trigger an email.
there might be useful to do a template with a form with two buttons, 'Yes' or 'No'. If they hit 'Yes' you can hit an API in your platform to run script.
Maybe you can use a redis or some database to save the preferences of this user.
Hope it helps.
I am trying to automate some extra data generated to a form response using Google App Script; (in particular, a unique identifier).
I can trigger a script on a form response, but I have multiple forms connected to this sheet document, so I need to make sure that the correct form is triggering the response, and then I would like to write the values to the correct sheet of the document.
How do I check which form is being triggered, and how can I find the sheet that collects it's responses?
I assume you've already set up the function on the sheet to capture the "on submit" event. You can get the [sheet] object from the e (event) argument.
//make sure to set up the form submit trigger from
//your script menu: Edit -> Current Project's Triggers
function onSubmit(e) {
var range = e.range
var sheet = range.getSheet();
//then do something with [sheet] object
}
I'm pretty sure you can only connect one form to a sheet, so knowing which sheet got written to will tell you which form was submitted.
Check out more about event triggers in the documentation.
I'm writing a script within (bound to) a Google Form. The idea here is to have the respondent enter their email address in the form (that's the only field in the form) and then make a copy of a spreadsheet (based on a template) and email it to them.
The script is triggered by the submission of the form. At the moment I'm wondering what the easiest way would be to grab that email address out of their response. I have gotten as far as figuring out that it's available within FormApp.getActiveForm(), but I can't seem to find a "getActiveResponse()" method. How do I grab that email address they just entered, without scrolling through every response? If there was a getActiveResponse, then I would just grab the contents of Item 1.
The Form submit event has the FormResponse property. This property has the method getRespondentEmail()
If the script is triggered by the submit event, you should be able to fetch the email address from there.
I am using a form to collect booking details and have the script attached to the form (so that the form submit trigger will trigger when the response is edited, which the spreadsheet bound version does not allow) which sends an email, schedules calendar events, creates confirmation documents etc on form submit. Using if statements I have provided the user the ability to avoid 'click the box to not send the customer an email on submit' functionality so that the form responses can be edited without triggers the automated functions, but that relies on the user remembering to click those boxes.
A better solution would be to be able to have the script, as its final function, change the item response, so that when the form is edited in future, the automatic functions are already disabled.
I do not want to create a new form response (although it would be possible to tweak the pre-populated url to create a duplicate with the appropriate changes)
FormResponse.withItemResponse(response) specifies that it doesn't work on stored responses
It is so easy to view form responses, and with the native edit response url functionality I figured editing a single item response would be doable, but so far it has eluded me.
Is there a way to do this?
Thanks
At this time Form responses could only be created programmatically, they can't be edited programmatically. Consider to submit a feature request through Google Apps Script Issues
I need clarification from someone.
I understand that multiple forms can send their responses to a single spreadsheet in the new Google Sheets.
So I created two Google forms and had them both send their responses to a new Google spreadsheet. I then created two scripts in the spreadsheet (not in the forms). Each script is set to trigger when a form is submitted. Here are the two simple scripts.
function form2() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form Responses 1");
sheet.getRange(sheet.getLastRow(),3).setValue("This is for test 2");
}
function form1() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form Responses 2");
sheet.getRange(sheet.getLastRow(),3).setValue("This is for test 1");
}
Somehow, Google is running the correct script for the appropriate form! How can that be? That is, form1() is automatically triggered when someone submits form1 and form2() is automatically triggered when someone submits form2.
So my question is, "How does Google know which script to run when a form is submitted?".
Thanks. I want to send all my form data to one spreadsheet if possible, but I don't want to set something up that will be unreliable.
-jay
Thanks wchiquito for your answer. I think that I figured this out with your help. Here is what I think is going on. If someone can confirm or disprove my conclusions it would be a big help to me.
There are two ways of creating a form in Google Apps.
Create a Form Directly.
Create a spreadsheet first and then create the form by selecting "Form" and then "Create Form" from within the menu bar of the spreadsheet.
But no matter how you create your form, you will end up having two files. One will be for the form and one will be for the spreadsheet which receives the data from the form. If you put the script in the form and in a spreadsheet, both will run when the form is submitted. The difference is that any script in the form will be limited in permissions. That is, any script in the form will not be able to access the spreadsheet where-as the script in the spreadsheet will have all of the permissions that you have (as long as the trigger is set under your account).
Now...
The new Sheets in Google Drive now has the ability to receive responses from more than one form while the old sheets in Google Drive only had the ability to receive one form responses. This eventually lead to my confusion and the answer from wchiquito is correct. It appears that the receiving spreadsheet will run as many onsubmit scripts as you like, but whether it is one or more than one, they will all run on all form data being received by the spreadsheet. That is, when the receiving spreadsheet receives form data from any form, ALL onsubmit scripts within the spreadsheet will be executed.
Both scripts should be executed, perhaps by using literal strings, the change is not visible.
Try running with the following changes:
function form2(e) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form Responses 1');
sheet.getRange(sheet.getLastRow(), 3).setValue(e.values);
}
function form1(e) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form Responses 2');
sheet.getRange(sheet.getLastRow(), 3).setValue(e.values);
}