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();
Related
I created an apps script project for my google form, and added a trigger which has the following settigs -
and my Code.gs file has this code, which is supposed to execute when a new submission arrives -
function sendemail(e) {
GmailApp.sendEmail("sampleemail#gmail.com","Hello there","This is a sample sentence.");
}
When I tested this trigger by submitting a new form, I received the test email successfully. Now instead of a static email (sampleemail#gmail.com) being used as the recipient of the email, I want it to be the value my user entered in the Email field in the form submission which triggered this function. So I want my code to look like -
function sendemail(e) {
GmailApp.sendEmail("<email from form response>","Hello <name from the form>","This is a sample sentence.");
}
So if the trigger data is arriving in the e parameter in sendemail function, how do I get the data which was submitted? When I debugged JSON.stringify(e), I get {"authMode":"FULL","response":{},"source":{},"triggerUid":"12647267"}.
Is there any way I get the data that was submitted in the form in the function? Do I need to change any settings on my form or trigger to get the data? I've just started with Google Apps Script and am a bit confused on how should I do this... Any advice is appreciated! Thanks :)
Solution by #Cooper in the comments
Try using the onFormSubmit trigger for the linked Spreadsheet. It;s event object contains namedValues and values.
Thanks #Cooper, it works perfectly now. Cheers!
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.
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.
I have written a simple Apps Script, which does the following:
Creates a Google Form, and populates it's fields accordingly with validation.
Creates a Folder and uploads it onto Google Drive.
Creates a Spreadsheet File and uploads it into the Folder created above.
What I want to do now is handle Google Form submission using the script to send email responses. How would I go about handling Google Form submissions without having to manually attach another script every time I create a form using the above mentioned script.
Probably you want to use the onFormSubmit trigger.
Specifies a trigger that will fire when a response is submitted to the form.
var form = FormApp.openById('1234567890abcdefghijklmnopqrstuvwxyz');
ScriptApp.newTrigger('myFunction')
.forForm(form)
.onFormSubmit()
.create();
Read the official documentation here.
I used a Google Sheet form as a newsletter registration form.
In the response form i've built a Formula which builts the request.
Column A has a timestamp
Column B the e-mail adress
Column C builts the request:
Example Output
http://newsletter.request.com/automated/action.jsp?action=SubscribetoNL&pw=XXX&email=xx#xx.com
Is there a way to have a script fire (click on or use POST) the newly created request on form submit?
thanks a lot for your help!
Well, you can not fire POST request on Form submit, but you can use of trigger functionality. You can set a trigger which is executed as soon as the form is submitted submitted. As soon as form is submitted, your custom function will fire, you can do something like this in that function,
var url = UrlFetchApp.fetch('http://newsletter.request.com/automated/action.jsp?action=SubscribetoNL&pw=XXX&email=xx#xx.com');
var responseCode = url.getResponseCode();
if(responseCode == 200) {
//do your own stuff
} else {
//do your own thing
}
Currently, doPost() is only available for standalone web apps. Refer this help article about doPost() and doGet().