Dame Lyngdoh
Dame Lyngdoh

Reputation: 352

Event Object not working in google app script for onSubmit trigger in google form

I created a google form and added a trigger which triggers whenever the form submit event is triggered. I need to use the event object for this event and when I add any line of code which tries to access this event then, an error occurs.

function onSubmit(e) {
  var s = e.values[0];
  Logger.log(s);
}

I get this error message when the function is triggered:

Execution failed: TypeError: Cannot read property "0" from undefined. (line 2, file "Code")

My form has one text input field (basically its just a form where I'm testing and trying out things with Google App Script), so I'm trying to access the data in this field when the form is submitted.

Upvotes: 3

Views: 2489

Answers (1)

piscator
piscator

Reputation: 8739

You can use the ActiveForm object instead of the event object.

function onSubmit() {

  var responses = FormApp.getActiveForm().getResponses();
  var length = responses.length;
  var lastResponse = responses[length-1];
  var formValues = lastResponse.getItemResponses();

  Logger.log(formValues[0].getResponse());
}

This code does basically what you need (after you set up the trigger like you did).

Better explanation can be found here: google script get the current response onSubmit

Upvotes: 4

Related Questions