Multi-page site in Google Apps Script: How to invoke doGet() through jquery or javascript?

前端 未结 2 1712
无人及你
无人及你 2021-01-07 09:37

I want to build multi-page sites using Google Apps Script. To do this, I would like to exploit the ability of doGet() to process query strings. I am aware of at

相关标签:
2条回答
  • 2021-01-07 10:21

    Here's what I ended up using in the basic html page:

    <!DOCTYPE html>
    <html>
      <head>
        <title>Speedgolf Scoring</title>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script>
    
      function loadNewPage(url) {
        //window.location.href = url;
        window.open(url,"_top");
      }
    
      function getUrl(qs)
      {
        google.script.run
            .withSuccessHandler(loadNewPage)
            .getScriptURL(qs);
      }
    
    console.log('MyCode');
    </script>
      </head>
      <body>
        <H3>Please choose interface:</H3>
        <input type="button" value="All Sheets" onClick="getUrl('?mode=allsheets');" />
        <input type="button" value="One Sheet" onClick="getUrl('?mode=onesheet');" />
        <input type="button" value="Return to Main" onClick="getUrl('');" />
      </body>
    </html>
    

    And my gs code now looks like this:

    function getScriptURL(qs) {
    
      var url = ScriptApp.getService().getUrl();
      Logger.log(url + qs);
      return url + qs ;
    }
    
    
    function doGet(e) 
    {
      Logger.log('query params: ' + Utilities.jsonStringify(e));
      if(e.queryString !=='')
      {  
        switch(e.parameter.mode)
        {
          case 'onesheet':
            return HtmlService
            .createHtmlOutputFromFile('onesheet')
            .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL)
            .append('<br />onesheet')
            .setTitle("One Sheet"); 
            break;
          case 'allsheets':
             return HtmlService
             .createHtmlOutputFromFile('allsheets')
             .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL)
             .append('<br />allsheets')
             .setTitle("All Sheets");
            break;
          default:
             return HtmlService
             .createHtmlOutputFromFile('speedgolferah')
             .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL)
             .append('<br />No Query String')
             .setTitle("Main Page");
            break;
        }
      }
      else
      {
        return HtmlService
             .createHtmlOutputFromFile('speedgolferah')
             .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL)
             .append('<br />No Query String')
             .setTitle("Main Page");
      }
    }
    

    I have some other pages I threw in because I had them. One displays all of my sheets and one displays just the one I select. But yeh I'd been putting this off for a while and I was good to get in there and start messing with the multiple payge solution for the doGet again.

    But thanks for the window.open(url,"_top"); that made a lot of difference. Thanks.

    0 讨论(0)
  • 2021-01-07 10:35

    Aha! The above code will work properly if you change

    window.open(newURL,"_self");
    

    to

    window.open(newURL,"_top");
    

    I am not sure why "_top" is necessary, but in the example code above, it makes all the difference: doGet() is invoked with the proper query string, resulting in the expected page being served. Hooray! This approach to serving new web pages on the fly provides a powerful alternative to templated HTML. Enjoy!

    0 讨论(0)
提交回复
热议问题