Search code examples
javascriptjquerygoogle-apps-scriptweb-applicationsquery-string

doGet(e) parameter undefined


I am trying to process query parameters in the Google Apps Script doGet(e) method. I've defined the following simple doGet() function that serves different html based on the parameters (or lack of parameters).

function doGet(e) {
  Logger.log('In doGet');
  Logger.log('query params: ' + e);
  var isNull = (e.queryString == null);
  var len = e.parameters.length;
  Logger.log('query string is null: ' + isNull);
  Logger.log('number of params: ' + len);
  if (e.queryString == null) { //doesn't work!
    Logger.log('Serving loginUI');
    return HtmlService
    .createHtmlOutputFromFile('loginUI')
    .setSandboxMode(HtmlService.SandboxMode.IFRAME)
    .addMetaTag('viewport', 'width=device-width, initial-scale=1')
    .setTitle("Please log in");
  }
  //if here, we know query string is NOT null
  if (e.parameter.mode == "player") {
    return HtmlService
    .createHtmlOutputFromFile('playerUI')
    .setSandboxMode(HtmlService.SandboxMode.IFRAME)
    .addMetaTag('viewport', 'width=device-width, initial-scale=1')
    .setTitle("Following Player");
  }
  if (e.parameter.mode == "hole") {
    return HtmlService
    .createHtmlOutputFromFile('playerUI')
    .setSandboxMode(HtmlService.SandboxMode.IFRAME)
    .addMetaTag('viewport', 'width=device-width, initial-scale=1')
    .setTitle("Following Hole");
  } 
  if (e.parameter.mode == "flex") {
    return HtmlService
    .createHtmlOutputFromFile('flexUI')
    .setSandboxMode(HtmlService.SandboxMode.IFRAME)
    .addMetaTag('viewport', 'width=device-width, initial-scale=1')
    .setTitle("Flex Interface");
  }
}

As you can see from my logging code, I'm trying hard to figure out what the e parameter is in the case in which doGet() is originally called without query parameters. The log output is as follows:

> [17-10-24 18:14:48:557 PDT] Starting execution [17-10-24 18:14:48:565
> PDT] Logger.log([In doGet, []]) [0 seconds] [17-10-24 18:14:48:566
> PDT] Logger.log([query params: [object Object], []]) [0 seconds]
> [17-10-24 18:14:48:566 PDT] Logger.log([query string is null: false,
> []]) [0 seconds] [17-10-24 18:14:48:567 PDT] Logger.log([number of
> params: undefined, []]) [0 seconds] [17-10-24 18:14:48:568 PDT]
> Execution succeeded [0.003 seconds total runtime]

How can I handle the case in which there are no query parameters in e.parameters. It appears that e comes in as undefined in this case. Any ideas?

As a follow-up question, my idea is to build query strings in the javascript associated with the html files that are served by doGet. In the "loginUI.html" file referred to in the code above, for example, I want to include jquery code that calls google.script.run.doGet() with different parameters, as follows:

<script>
  $("#playerUI").click(function () {
    var queryString = "?mode=player";
    google.script.run.doGet(queryString);
  });

  $("#holeUI").click(function () {
    var queryString = "?mode=hole";
    google.script.run.doGet(queryString);
  });

   $("#flexUI").click(function () {
    var queryString = "?mode=flex";
    google.script.run.doGet(queryString);
  });
</script>

My sense is that this won't work either. Please advise.


Solution

  • How can I handle the case in which there are no query parameters in e.parameters. It appears that e comes in as undefined in this case. Any ideas?

    Short answer

    function doGet(e) {
    
      Logger.log('queryString is an empty string: ' + (e.queryString === ''));
      Logger.log('parameter hasn\'t members: ' + (Object.keys(e.parameter).length === 0));
    }
    

    Explanation

    To write to the Google Apps Script editor log the content of an object use JSON.stringify(object).

    Example:

    function doGet(e) {
      Logger.log(JSON.stringify(e));
    }
    

    The above code printed to the log the following text:

    [17-10-24 21:13:28:414 CDT] {"parameter":{},"contextPath":"","contentLength":-1,"queryString":"","parameters":{}}

    As you can see, there are several named values, :

    • 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.
    • contextPath: Not used. Always empty string.
    • contentLength: Always returns -1 for doGet
    • queryString: The value of the query string portion of the URL.
    • parameters: An object similar to e.parameter, but with an array of values for each key

    Reference

    Note: The first reference says that queryString returns null when the query string is not specified but on the tests that I made, this was not true.