Form submit trigger object is empty

128 views
Skip to first unread message

Thomas Jungers

unread,
Sep 19, 2019, 8:53:14 AM9/19/19
to Google Apps Script Community
Hi,

I'm trying to write a script that sends a custom email to the person who submitted it. The content of the email depends on the responses of the user, so I'm trying to get them.
When I fetch the responses from the form, I get an error. So, I simplified my code for some simple debugging:
function onFormSubmit(e) {
 
MailApp.sendEmail({to: my_email_address, subject: 'Log', body: JSON.stringify(e)
}

When I submit the form, I do get an email (so at least the trigger works properly), but it contains this:
{"authMode":{},"response":{},"source":{},"triggerUid":"1758093"}
The response object is empty, even though I filled in the form.

When I try to debug the whole Form, with this:
function onFormSubmit(e) {
  var form = FormApp.getActiveForm()
  
MailApp.sendEmail({to: my_email_address, subject: 'Log', body: JSON.stringify(form)
}
The log email contains only an empty object:
{}

I thought it may have been due to a lack of authorisations for the forms API, so I explicitly added https://www.googleapis.com/auth/forms to the oauthScopes in my manifest. The OAuth popup did include the "form modification" notice, which I authorised, but the log emails remain the same.

Any idea why that is? How can I get the data from the submitted form?

Thanks.
Thomas

Alan Wells

unread,
Sep 19, 2019, 9:53:09 AM9/19/19
to Google Apps Script Community
I think that the log can show an empty object {} for the object even if there is something in the object.  I've seen that many times.  The Apps Script event object is not a JSON object.  Apps Script isn't written in JavaScript.  When you stringify the event object, it looks like what you are getting is just one level of the object elements.  So, stringifying it, is sort of working, but only working on one level.  Google probably doesn't want you to see what is in their objects.  So, it's not because of authorization.
If you want to get the answers from the Form submission, you might want to look at the following StackOverflow post:

Thomas Jungers

unread,
Sep 19, 2019, 9:59:24 AM9/19/19
to google-apps-sc...@googlegroups.com
Yes, that would make sense.
I slightly adapted my code based on your link and it now works.
I think I panicked a bit when I saw an empty object and I didn't think to search for other possibilities.

Thanks!
Thomas
Reply all
Reply to author
Forward
0 new messages