Catch response from coded google form [duplicate] - google-apps-script

I'm trying to run a script that is connected to one form (A) when a different form (B) is submitted. I have a series of different forms that are similar and needs to run the same script, so instead of copying the code to each script I wonder if this is possible.
I successfully made an installable trigger function in A, pointing at B. I have granted permission when prompted.
When i post B, the script in A fires, and I get this in the log:
Exception: No response with ID 2_ABaOnudSFDkNQOL2Xn4fNOmT95GrTotEW8LSjxfI5qf6qceDN5hD5CHKqNT5D4G_DdONWq0 exists for this form.
at onFormAnswerSubmit(Kod:40:20)
The line (Kod:40:20) that halts is the line that fetches the posted data:
var items = e.response.getItemResponses();
Is this supposed to work or is it impossible to pass posted data from one form to a script in a different form?
Or is there something more I can do in the trigger? Like forcing a pass on of e. or something?
Here is the trigger I created:
/**
* Creates a trigger for when a different form is posted that runs a function in this script (run once)
*/
function createOtherFormTrigger() {
var formID = FormApp.openById("XXXXX"); // form B
ScriptApp.newTrigger('onFormAnswerSubmit') // a function in this script, in form A
.forForm(formID)
.onFormSubmit()
.create();
}

You need to change the logic a bit:
The function fired on trigger should be contained in the same script like the trigger.
Sample code to create a formSubmit trigger for form B:
//run this once
function createOtherFormTrigger() {
var formB = FormApp.openById("XXX");
ScriptApp.newTrigger('onFormAnswerSubmit') // a function in this script, in form B
.forForm(formB) //the active form is Form B
.onFormSubmit()
.create();
}
function onFormAnswerSubmit(e){
var formB = e.source;
var items = e.response.getItemResponses(); //items of the latest form response of form B
var formA = FormApp.openById("XXXX"); // Form A
...
// do what you need to do with form A
}
The code above will create trigger that fires when a form response for form B is being submitted. Thereby it is not important either the script is bound to form A, form B or is standalone. Actually, it might make mosst sense to implement those functions in a standalone script, to make sure that they don't interfer with other form submit triggers you might have in a form-bound script.

I had the same issue. Anywhere where I put e.response I get the same error. It looks that the only workaround is to use a stand alone script instead of a bounded script.

I just understand the problem.
So if you want to catch the response from the form submission, you need to bound your script to the form. To do that, go to your form edit view, then click the three dots, then open script editor.
If you done that previously, try do it again and make sure your script is there. In my case, the first time I open the script editor from the Google Form, it doesn't bound the script. So when I did it the second time, it opens a new script editor with blank template. After I copied my script to the new one, it works perfectly.

Related

Installable trigger to fire script in a different form?

I'm trying to run a script that is connected to one form (A) when a different form (B) is submitted. I have a series of different forms that are similar and needs to run the same script, so instead of copying the code to each script I wonder if this is possible.
I successfully made an installable trigger function in A, pointing at B. I have granted permission when prompted.
When i post B, the script in A fires, and I get this in the log:
Exception: No response with ID 2_ABaOnudSFDkNQOL2Xn4fNOmT95GrTotEW8LSjxfI5qf6qceDN5hD5CHKqNT5D4G_DdONWq0 exists for this form.
at onFormAnswerSubmit(Kod:40:20)
The line (Kod:40:20) that halts is the line that fetches the posted data:
var items = e.response.getItemResponses();
Is this supposed to work or is it impossible to pass posted data from one form to a script in a different form?
Or is there something more I can do in the trigger? Like forcing a pass on of e. or something?
Here is the trigger I created:
/**
* Creates a trigger for when a different form is posted that runs a function in this script (run once)
*/
function createOtherFormTrigger() {
var formID = FormApp.openById("XXXXX"); // form B
ScriptApp.newTrigger('onFormAnswerSubmit') // a function in this script, in form A
.forForm(formID)
.onFormSubmit()
.create();
}
You need to change the logic a bit:
The function fired on trigger should be contained in the same script like the trigger.
Sample code to create a formSubmit trigger for form B:
//run this once
function createOtherFormTrigger() {
var formB = FormApp.openById("XXX");
ScriptApp.newTrigger('onFormAnswerSubmit') // a function in this script, in form B
.forForm(formB) //the active form is Form B
.onFormSubmit()
.create();
}
function onFormAnswerSubmit(e){
var formB = e.source;
var items = e.response.getItemResponses(); //items of the latest form response of form B
var formA = FormApp.openById("XXXX"); // Form A
...
// do what you need to do with form A
}
The code above will create trigger that fires when a form response for form B is being submitted. Thereby it is not important either the script is bound to form A, form B or is standalone. Actually, it might make mosst sense to implement those functions in a standalone script, to make sure that they don't interfer with other form submit triggers you might have in a form-bound script.
I had the same issue. Anywhere where I put e.response I get the same error. It looks that the only workaround is to use a stand alone script instead of a bounded script.
I just understand the problem.
So if you want to catch the response from the form submission, you need to bound your script to the form. To do that, go to your form edit view, then click the three dots, then open script editor.
If you done that previously, try do it again and make sure your script is there. In my case, the first time I open the script editor from the Google Form, it doesn't bound the script. So when I did it the second time, it opens a new script editor with blank template. After I copied my script to the new one, it works perfectly.

How can I make it so once a user submits on a google forum it executes a function?

I have a google forum that links to a spreadsheet and my code is linked to the spreadsheet. I want to make it so once a user submits on the forum and the spreadsheet receives a change it executes a function. What would be the simplest and most efficient way of doing so?
Creating an event trigger in the Apps Script file of your Google Form or Google Sheet is the way to do it. With this, each time a Google Form submission is made your function will run.
Here is an example when creating it in your Google Form:
Open your Google Form
Open the Script Editor
Create the following function
function onFormSubmit(e){
var response = e.response;
var responseId = response.getId();
var form = FormApp.getActiveForm();
var formResponse = form.getResponse(responseId);
// enter the rest of your code here that you would like to run each time a form submission is made
}
Then, within your Script Editor file, click Edit > Current project's triggers
In the bottom right corner, click Add trigger
Select the onFormSubmit function, change the event type to 'On form submit', and leave everything else as they default
Next form submission that is made, your function will run.
Hope this helps.

Wrong source and response in form triggers

I set a simple workflow with a Google Apps Script. I created the script project from a form A.
Here is the workflow:
users submit a response for the form A
the script creates a new response for the form B, submit it, and sends me the edition link by email
I submit the response for the form B the script created for me
the script sends me a recap of both form by email
To do so I have programmatically set up a trigger on the form B:
function addOnNewFormBSubmitTrigger() {
ScriptApp.newTrigger('onNewFormBSubmit')
.forForm(FORM_B_ID)
.onFormSubmit()
.create();
}
The function onNewFormBSubmit looks like that:
function onNewFormBSubmit(formSubmitEvent) {
_sendAnalysisEmail(formSubmitEvent.source, formSubmitEvent.response);
}
The function is triggered but the the formSubmitEvent.source value is the form A and formSubmitEvent.response is the latest response for this form. I would expect the source to be the form B, as I attached the trigger on it.
EDIT:
Even passing a form instance instead of its ID does not work as I expect it
function addOnNewAlaysisSubmitTrigger() {
var form = FormApp.openById(FORM_B_ID);
ScriptApp.newTrigger('onNewFormBSubmit')
.forForm(form)
.onFormSubmit()
.create();
}
Am I missing something?
Thank you #tehhowch, the link you gave me in the comments made me think that it may work if the script is not created from the Form. Thus I made a POC creating the script from a Spreadsheet and it works.
This is the script I made: Google Apps Script
And these are the logs generated by the two functions: function call logs from Stackdriver
As you can see I get two different responses submitted from the two forms.
Now the problem is that the event is not a submit form event as defined in the documentation: https://developers.google.com/apps-script/guides/triggers/events#form-submit_4 but this is another story.
If you want to try, here are:
the spreadsheet from where I created the forms and the script (logs are pushed in the "logs" sheet): https://docs.google.com/spreadsheets/d/17pFDXlYpOjXgaN5ZsBYimrBZHl6WPcmUvMrAjAnc9ys/edit?usp=sharing
the live form A (Google sign in required): https://docs.google.com/forms/d/e/1FAIpQLSff7ISYkqLtPwtzTWw2Iwxmm4dIax7z9ye9EeLY9SxDz3vJvw/viewform?usp=sf_link
the live form B (Google sign in required too): https://docs.google.com/forms/d/e/1FAIpQLSc1HrswTr6prGxBBwy2a4Nqs1pyF7CJTjk2HafZyXsTtdnaXQ/viewform?usp=sf_link
the script : https://docs.google.com/spreadsheets/d/17pFDXlYpOjXgaN5ZsBYimrBZHl6WPcmUvMrAjAnc9ys/edit?usp=sharing
Have you tried to use Form as parameter instead of just his Id.
Something like that:
var form = FormApp.openById('FORM_B_ID');
ScriptApp.newTrigger('onNewFormBSubmit')
.forForm(form)
.onFormSubmit()
.create();

Add button to trigger script along top bar in google sheets

I have written the script below to hid all rows that have a specific box checked. I set a trigger using the clock and every 10 minutes but instead seems to run every time I check a box and it screws up the view every time I check it. I would like to change it to a manual trigger that is a button along the top bar that someone manually clicks. Can anyone help me edit it?
function onEdit(e) {
var s = SpreadsheetApp.getActive().getSheetByName('2 Week Snapshot');
s.showRows(1, s.getMaxRows());
s.getRange('C:C')
.getValues()
.forEach( function (r, i) {
if (r[0] == 1)
s.hideRows(i + 1);
});
}
As mentioned by I'-'I, the name given to your function causes your script to run when an edit is made to the sheet. From the Apps Script documentation on Simple Triggers:
To use a simple trigger, simply create a function that uses one of these reserved function names:
- onOpen(e) runs when a user opens a spreadsheet, document, presentation, or form that the user has permission to edit.
- onEdit(e) runs when a user changes a value in a spreadsheet.
- onInstall(e) runs when a user installs an add-on.
- doGet(e) runs when a user visits a web app or a program sends an HTTP GET request to a web app.
- doPost(e) runs when a program sends an HTTP POST request to a web app.
The e parameter in the function names above is an event object that is passed to the function. The object contains information about the context that caused the trigger to fire, but using it is optional.
Renaming your function to avoid binding this simple trigger will prevent edits of your sheet from triggering your function.
Documentation to look at:
Guide to Menus
Guide to Triggers

Google App Scripts - Create Google form using CreateForm() function and run MarkForm() function on form submit

Aim
I am currently using Google App Scripts to create a form that one submitted sends an email to the respondent with a detailed summary of their results.
Methods so Far
To achieve this, I have written two functions: one function to create the form and a second function to mark and email the summary of the results to the respondent.
function CreateForm(){
//Working code to create form here
}
function MarkForm(){
//Working code to create and email detailed summary of results
}
In order to make this code work correctly, I have to:
Create the form using the CreateForm() function
Open the new form and paste the MarkForm() function into the script editor.
Set a trigger OnFormSubmit to run the function MarkForm()
Question
Is it possible to do the three steps above using one function?
You can use the "On Form Submit" installable trigger to run a function when a form response comes in. Reading the Google Triggers guide in the Apps Script Documentation will help.
Do the following:
Choose Edit > Current project's triggers. You see a panel with the message No triggers set up. Click here to add one now.
Click the link.
Under Run, select the function you want executed by the trigger. (MarkForm())
Under Events, select From Spreadsheet.
From the next drop-down list, select On form submit.
Click Save.
Now, when a submission comes in, the form response will be marked automatically. This trigger is an installable trigger, which means it needs to be set up through the menu rather than calling a function like onEdit().