jwesonga
jwesonga

Reputation: 4383

Google Apps Script Form: e.namedValues logs nothing

I have a very basic Google Apps form with two questions, Resource (radio button with two options) and Name (Text Field). I'd like to pick the form values upon submission so I've set up a trigger (on form submit) to call a function:

function onFormSubmit(e) {
  Logger.log(e.namedValues["Resource"]);
}

Any idea what I'm doing wrong?

Upvotes: 3

Views: 13460

Answers (4)

Patricio Bahamondes
Patricio Bahamondes

Reputation: 21

That's because you connect the Form to the script, in order to be able to use e.namedValues or e.values you must connect your script with the sheet where the results are stored. And don't forget to activate the trigger "from your sheet"/"on form submit"/"use your function".

Upvotes: 2

Syed Waqas Bukhary
Syed Waqas Bukhary

Reputation: 5340

This is how we get responses for all previously submitted forms including the last one:

var form = FormApp.openById('1234567890abcdefghijklmnopqrstuvwxyz');
//OR get the form by URL
//var form = FormApp.openByUrl( 'https://docs.google.com/forms/d/Uksgii-z-EpjdsIKZE/edit' );
 var formResponses = form.getResponses();
 for (var i = 0; i < formResponses.length; i++) {
   var formResponse = formResponses[i];
   var itemResponses = formResponse.getItemResponses();
   for (var j = 0; j < itemResponses.length; j++) {
     var itemResponse = itemResponses[j];
     Logger.log('Response #%s to the question "%s" was "%s"',
         (i + 1).toString(),
         itemResponse.getItem().getTitle(),
         itemResponse.getResponse());
   }
 }

This is how we get responses for only the last submitted form:

//change the id or URL with your form's URL
  var form = FormApp.openById('1234567890abcdefghijklmnopqrstuvwxyz');
//OR get the form by URL
//var form = FormApp.openByUrl( 'https://docs.google.com/forms/d/Uksgii-z-EpjdsIKZE/edit' );

  var formResponses = form.getResponses();

  //getting the last form
  var formResponsesLength = formResponses.length - 1;
  var formResponse = formResponses[formResponsesLength];


   var itemResponses = formResponse.getItemResponses();
  //iterate over each field in the last form
   for (var j = 0; j < itemResponses.length; j++) {
   var itemResponse = itemResponses[j];

     if (itemResponse.getItem().getTitle() == 'Field One') {//your field name (heading) in google form
     var FieldOne = itemResponse.getResponse();
     }

     if (itemResponse.getItem().getTitle() == 'Field Two') {//your field name (heading) in google form
     var FieldTwo = itemResponse.getResponse();
     }
   }
Logger.log(FieldOne);
Logger.log(FieldTwo); 

Upvotes: 1

jwesonga
jwesonga

Reputation: 4383

I figured the way to achieve this, here's the code:

function onFormSubmit(e) {
  var frm = FormApp.getActiveForm().getItems();
  var rsc = e.response.getResponseForItem(frm[0]).getResponse();
  var reasonSubmitted = e.response.getResponseForItem(frm[1]).getResponse();
  var dateRequested = e.response.getResponseForItem(frm[2]).getResponse();

   var submitter = Session.getActiveUser().getEmail();

  //Logger.log(e.response);
  Logger.log("resource: " + rsc
  + " reason: " + "resource: " + reasonSubmitted);
  }

This will give you access to the individual responses for each question

Upvotes: 6

Cameron Roberts
Cameron Roberts

Reputation: 7377

Because the script is contained by the form itself (as mentioned in comments on this answer), e.namedValues is not present, and you must use the Form event methods instead.

EG var responses = e.response.getItemResponses();

See Form Events documentation here:

https://developers.google.com/apps-script/guides/triggers/events#google_forms_events

If you are working within the destination spreadsheet where answers are stored, (where e.namedResponses will be present) the Logs displayed are related to the session viewing the script.

Since the OnFormSubmit() trigger is fired by an external event, you won't reliably see the results of Logger.log() calls in your open copy of the Script Editor.

So, the trigger is likely working, but you aren't seeing the log results due to this limitation.

Upvotes: 0

Related Questions