0

I have a script that is running when a form is submitted. The script does not work when I use a function with the e parameter such as the code below. I get a message returned: ReferenceError: 'e' is not defined. Any idea why?

function submitFormFunc(e) {
  var items = e.response.getResponses();
  var responses={};
  for(var i = 0; i< items.length; i++) {
   responses[items[i].getItem().getTitle()]=items[i].getResponse();
  }

  var responseTable = [];
  var responseIndex = ["Timestamp","ID","question 1","question 2"];
  responseTable.push(e.response.getTimestamp().toString());
  responseTable.push(e.response.getId());
  responseTable.push(responses["question 1"]);
  responseTable.push(responses["question 2"]);
  responseTable.push(FormApp.getActiveForm().getResponse(e.response.getId()).getEditResponseUrl());
  SpreadsheetApp.openById("your spreadsheetId").appendRow(responseTable);
}
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
user2188317
  • 71
  • 1
  • 8
  • 1
    How does `submitFormFunc` get called? Are you binding it to the submit event handler for the form? If so, how? Also, what browser are you using? – Anthony Grist Apr 02 '14 at 14:59
  • Yes, I have defined a trigger for this script on spreadsheet form submit (script editor=> tools=> triggers for this project) . A simple script works fine, but the trigger is not working for the script above. – user2188317 Apr 02 '14 at 16:46
  • Message has changed (message is in Dutch, translated) submitFormFunc TypeError: Kan methode getResponses van undefined niet aanroepen. (regel 2, bestand 'Code'). Translate: "submitFormFunc TypeError: Cannot call method getResponses from undefined. (line 2, file 'Code')" – user2188317 Apr 02 '14 at 18:13
  • Tried to use another method, which works correctly: var question = e.values[2]. Apparently the script does not accept the line – user2188317 Apr 02 '14 at 19:35
  • Tried to use another method, which works correctly: var question = e.values[2]. Apparently the script does not recognize the line var items = e.response.getResponses(); – user2188317 Apr 02 '14 at 20:19
  • To anyone else led astray by the `[javascript]` tag - this question is specific to the Google Apps Script environment, and its Forms service - the normal concerns about HTML form submit events mentioned by Anthony are irrelevant. – Mogsdad Nov 14 '14 at 19:44

1 Answers1

1

You are receiving that error because the event object, e, isn't defined when you've called submitFormFunc(e). This is typical when you are debugging, and have invoked a parameterized function from the editor/debugger.

For your testing, simulate the event using the technique from How can I test a trigger function in GAS?

Community
  • 1
  • 1
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • The nice thing about good answers is that you can reuse them indefinitely :-) , in french we say "cent fois sur le métier remettez votre ouvrage"..., which means in short that repetition should not be discouraging. – Serge insas Nov 14 '14 at 20:20