26

I'm trying to get started with a very simple Google Form containing just a couple of questions (a multiple choice with just 2 options and a short text). After creating it, I opened the script editor and typed in

function onSubmit(e) {
  Logger.log("onSubmit(%s)", JSON.stringify(e));
}

and configured onSubmit as the handler for "form submit" trigger using the "Current project's triggers" from the "Edit" menu.

Filling in the form and submitting it now does result in the handler being called, but I only see this in the log:

[17-04-15 18:56:23:584 CEST] onSubmit({"response":{},"source":{},"authMode":{},"triggerUid":1870249629})

i.e. the response field is empty. I've also tried using FormApp.getActiveForm().getResponses(), but it returns an array of several empty objects too (OTOH, FormApp.getActiveForm().getTitle() does return the title I gave the form).

I suspect I need to give the script some extra permissions to access the form data, but I have no idea how to do it, nor even if this is really the problem.

Does anybody know why am I not getting the form values and what should I do to get them? Thanks in advance!

Rubén
  • 34,714
  • 9
  • 70
  • 166
VZ.
  • 21,740
  • 3
  • 39
  • 42

1 Answers1

49

There are 2 patterns for retrieving submitted values. For both patterns, the function for retrieving the values from form submission has to be installed as a trigger. The detail information of Installable Triggers is https://developers.google.com/apps-script/guides/triggers/installable.

1. Script is opened on spreadsheet.

In this case, by installing a trigger, you can retrieve the submitted values by your script. The detail information of Event Objects is https://developers.google.com/apps-script/guides/triggers/events#form-submit.

Script :

function onSubmit(e){
  Logger.log("%s", JSON.stringify(e));
}

Result :

{
  "values": [
    "date and time",
    "test"
  ],
  "namedValues": {
    "fromtestform": [
      "test"
    ],
    "timeStamp": [
      "date and time"
    ]
  },
  "range": {
    "columnStart": 1,
    "rowStart": 2,
    "rowEnd": 2,
    "columnEnd": 2
  },
  "source": {},
  "authMode": {},
  "triggerUid": #####
}

2. Script is opened on form.

In this case, the submitted values can be retrieved by following script. The detail information is https://developers.google.com/apps-script/reference/forms/form-response.

Script :

function onSubmit(e){
  Logger.log("authMode=%s, source.getId()=%s", e.authMode, e.source.getId());
  var items = e.response.getItemResponses();
  for (i in items){
    Logger.log("getItem().getTitle()=%s, getResponse()=%s", items[i].getItem().getTitle(), items[i].getResponse());
  }
}

Result :

authMode=FULL, source.getId()=### form ID ###
getItem().getTitle()=## item's title ##, getResponse()=test

If I misunderstand your question, I'm sorry.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • 3
    You're absolutely right, this (the 2nd one, I don't use spreadsheets, just a standalone form) does work. Apparently it's just that `JSON.stringify()` doesn't work with GS objects! – VZ. Apr 16 '17 at 17:43
  • Thank you, too. I'm happy I could help you. – Tanaike Apr 16 '17 at 21:52
  • 13
    There is more value in this answer than in all of google's docs on the subject. – Alexander Toptygin Apr 19 '17 at 15:04
  • @Alexander Toptygin Thank you. – Tanaike Apr 19 '17 at 21:46
  • 3
    I'd like to note that its important that the function is called `onSubmit()`. the editor lets you type in anything, and I've read a tutorial where the function was called something else, but then the event received doesn't include the `response` object. – Guss Aug 16 '17 at 18:32
  • @Tanaike neatly explained. thank you. I am facing a problem with items[i].getScore(). it is returning null. any suggestions? – sreenath sirimala Nov 16 '17 at 12:05
  • @sreenath sirimala Thank you for your comment. But I'm sorry. I don't know about your situation from your comment. Can you post your problem as a new question? – Tanaike Nov 16 '17 at 22:53
  • @Tanaike I thought i could post it, but there are possible duplicates exist. – sreenath sirimala Nov 20 '17 at 08:52
  • @andrew_jackson I'm glad this answer was useful for you. Thank you, too. – Tanaike Jul 25 '18 at 21:50
  • 2
    @Tanaike google should put this in their docs. Spent so much time. – change Feb 06 '19 at 07:43
  • Clearly explained the problem, never know that the Google App Script stands alone at from / spreadsheet. Thank you! – PeterChen Jun 18 '21 at 16:34
  • 1
    Thanks so much for this, it kind of makes sense finally. If I have the script ont he sheet, then I do the event onFormSubmit on the sheet not the form ! – carbontracking Nov 10 '21 at 18:20