Google Apps Forms - google-apps-script

I am trying to develop a form. I have created it and am getting responses in a worksheet. However, after amendment I am getting blank columns where changes were made.
I tried deleting the Response sheet but it appears not to make a difference.
Secondly is there a way of running a script when the Form Submit button is clicked?

1:
Reconnect the form to the response sheet or create a new response sheet.
You are not supposed to mess with the response sheet data or format.
2:
see trigger onFormSubmit

Related

Email after Form Submit, Problem with Calculations

I've currently got a trigger on form submit to run my code which pulls in the submitted data and emails a summary to the user. I need to perform calculations on the submitted data and provide those results in the summary email. I've been able to hard code this and I have a working script but I want to do the calculations on one of the sheets instead. I've got another sheet setup to pull in the form data and perform the calculations but it seems the problem is that when the form data is submitted, none of the other sheets are updated so if I try to pull calculations from the other sheets it will not include the latest entry.
I've tried getting data from other sheets but I don't think the other sheet calculations are refreshing to include the newly submitted data, or the code is executing before the sheets can refresh, possibly order of operations limitation. The desired goal would be to have a user submit the form, then have all sheets using that data pull it in and complete calculations, then send an email using the newly calculated results. Ideally pulling data from multiple sheets.
I'm thinking the "on form submit" trigger is not appropriate here, instead maybe I need to have an event occur on the sheet or sheets I'm using to calculate that triggers an email. Or does anyone know if there's a way to force all sheets to update and fields to calculate before continuing to execute the code?
Any ideas? Thanks!
I'm not sure if I got 100% what you're looking for but you need to know that if it's your function who is opening a Spreadsheet and writing data on it, you can't add any trigger.
Trigger can only run following a "human" action (onOpen, onEdit..)
Pl. try importrange in the target sheet/sheets to pull data from the form sheet.
You have to maintain "completed" in a seperate sheet.
If you see a new entry, do the calculation, send mail and update the completed field.

How can I trigger a Sheets script from a Google Form Submission?

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.

Google Form Response Submit and Google Sheet Update

I have a script I am working on and need some help.
First how my script is supposed to work.
Script: A user fills out google form and hits submit. Upon submit my script runs and reads from the google sheet a range of data cells that I manipulate in sheets. The data cells then get formatted into a string and prompted in the Confirmation Message of the form.
Now for the problem.
Problem:
When I submit my responses to the form everything from sheets works fine. When I run my code manually the code works fine. When I submit my form responses, the event history shows the trigger and says it completes, but the end result(the confirmation message updating) doesn't take place.
Not Possible.
FormSubmit trigger and subsequent script execution is a asynchronous operation. The Form doesn't wait for the script to complete(and change the confirmation message). You can send a email for confirmation.

Edit Form Responses as soon as it is submitted

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.

How does google know which onsubmit trigger to execute when a form is submitted if you have multiple forms sending responses to a single spreadsheet

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);
}