google-apps-scriptweb-applicationsclient-server

How to use parameters that go to a script in html content?


I am new to using Google script, but not completely new to programming. I have looked at some examples and they generate HTML output like this:

function doGet(e) {
  return HtmlService.createHtmloutputFromFile('form.html') 
}

I would like to pass a parameter to my script to use that in the output. I have gotten as far as that I can use this in the function:

var room= e.parameter.room 

So when I execute my script with url?room=test I do get the value for that parameter.

But how can I use that variable room that I create in the HTML output and other code?

I have been looking at createTemplateFromFile but I am not getting anywhere.

Hope someone can point me in the right direction of what constructs and command to look at.


Solution

  • You can directly get query parameters client side1:

    form.html

    <script>
    google.script.url.getLocation(function(location) {
      alert(location.parameter.room); //alerts "test" on loading  "url?room=test"
    });
    </script>
    

    Alternatively, You can use scriplets to load html2:

    code.gs

    function doGet(e) {
      var temp = HtmlService.createTemplateFromFile('form');
      temp.room = e.parameter.room;
      return temp.evaluate();
    }
    

    form.html:

    <script>
        alert('<?=room?>'); //Printing scriplets
    </script>