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.