user11935585
user11935585

Reputation:

Unable to read parameters from GAS web app

I am organising an event where each attendee has a unique barcoded ticket. The barcode is a QR code linking to a GAS web app with url parameters for lookup in a private spreadsheet (name, ticket code and attendance date). Lets call the parameters a, b and c

The problem I have is that my doGet(e) function is not finding the parameters in the url and so all spreadsheet checks are failing, even if the data is in the sheet. The relevant code I have is as follows:

function doGet(e) {
  var ss = SpreadsheetApp.openById("id");
  var sheet = ss.getSheets()[1];

  var name = e.parameter.a[0];
  var ticketID = e.parameter.b[0];
  var attendanceDate = e.parameter.c[0];

  // other code

  var attendeeNames = sheet.getRange("A:A1").getValues();

  for (var i = 0; i < attendeeNames.length; i++) {
    if (attendeeNames[i] == name) {
      // ticket ID and date checks go here but name is failing
    } 
  } 
} 

Upvotes: 0

Views: 255

Answers (1)

Rafa Guillermo
Rafa Guillermo

Reputation: 15377

Answer:

e.parameter should be replaced with e.parameters.

Explanation:

There are two ways of obtaining parameters from event objects in Apps Script Web Apps - e.parameter and e.parameters. As per the documentation:

e.parameter:

An object of key/value pairs that correspond to the request parameters. Only the first value is returned for parameters that have multiple values.

{"name": "alice", "n": "1"}

e.parameters:

An object similar to e.parameter, but with an array of values for each key

{"name": ["alice"], "n": ["1", "2"]}

Only e.parameters uses arrays for the values of the key-value pairs and so when you're calling the values with

var name = e.parameter.a[0];
var ticketID = e.parameter.b[0];
var attendanceDate = e.parameter.c[0];

You will be returning null.

Fixes:

There are a couple of other issues that will need to be fixed with your code. In order to get the range of a column, you need to use .getRange('A1:A'), and not .getRange('A:A1;).

Replace:

var name = e.parameter.a[0];
var ticketID = e.parameter.b[0];
var attendanceDate = e.parameter.c[0];

with:

var name = e.parameters.a[0];
var ticketID = e.parameters.b[0];
var attendanceDate = e.parameters.c[0];

Or, if you only have one parameter for a, b and c, you can just use:

var name = e.parameter.a;
var ticketID = e.parameter.b;
var attendanceDate = e.parameter.c;

Also, replace:

var attendeeNames = sheet.getRange("A:A1").getValues();

with:

var attendeeNames = sheet.getRange("A1:A").getValues();

References:

Upvotes: 1

Related Questions