Search code examples
google-apps-scriptgoogle-sheetstriggersgoogle-forms

Using e.values with onFormSubmit


I'm brand new at this so please excuse me if this is a silly question.

I'm using google script to carry out a change based on the submitted answer from a google form. I've got a test function I've been using to get it right in the script editor and it works perfectly. When using the onFormSubmit trigger instead though, getting the variable result from e.values, it isn't doing anything. I've defined the trigger for the project and can see that confirmed in the 'Current Project's Triggers' window so it can't be that.

This is the test function code:

function Test_form_submit() {
   var ss = SpreadsheetApp.getActiveSpreadsheet()
   var sheet = ss.getSheetByName("Group Totals")
   SpreadsheetApp.flush()
   var their_choice1 = "Group 5: Thursday 6-8pm"
   var r = find_limits("C3:E7", their_choice1)
   var count = r[0]
   var limit = r[1] 
   check_availability("545507554", count, limit, their_choice1)

}

This is my trigger function code:

function onFormSubmit(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet()
  var sheet = ss.getSheetByName("Group Totals")
  SpreadsheetApp.flush()
  var their_choice1 = e.values[3]
  var r = find_limits("C3:E7", their_choice1)
  var count = r[0]
  var limit = r[1] 
  check_availability("545507554", count, limit, their_choice1)

}

The choice of theirs I'm wanting is in column D on the spreadsheet so I believe e.values[3] is the right thing to use. I can't understand why nothing is happening.

I tried adding a line of logger.log(e.values[3]) as well but the log is blank when I look.

Any help would be much appreciated!


Solution

  • As a complement to Amit's answer, you can check all the values and parameters returned in the eventInfo coming with the onFormSubmit trigger by using a statement like this :

      Logger.log(e);
    

    This will show something like this :

    enter image description here

    (the values shown here come from an unused form that I had with "garbage" values ;-) but it is sufficient to actually see the structure.)