Search code examples
google-apps-scriptweb-applications

Google App Script how to load different pages using HTML Service?


How do I load different pages in doGet with HTML Service programmatically? If it is impossible, why GAS editor allows creating different HTML pages?


Solution

  • If your loading different pages based on parameters passed in the URL you can test for the parameter using

    function doGet(e) {
       if (e.parameter.messageID) {   // Simply test if the parm messageID exists
    

    You can always do something like this too

    switch (v) {
       case "A":   var t = HtmlService.createTemplateFromFile("A");  break;
       case "B":   var t = HtmlService.createTemplateFromFile("B");  break;
    }
    return t.evaluate();
    

    And my final option is you can put conditional logic into the template to get different HTML

     // GAS file
     var t = HtmlService.createTemplateFromFile("A");
     var v = "A";   
     t.v = v;  // pass the variable v to the template 
     return t.evaluate();
    
     // A.html template file
     <? if (v == "A") { ?>
        <b>a bold A</b>
     <? } else { ?>
        <b>not a A but bold anyway </b>
     <? } ?>