Search code examples
google-apps-scriptweb-applications

Navigate to other pages


Can someone explain to me how to go about navigating to a new page in Google Apps Script is done ? My initial thought is to hide or delete all child elements within the app and then rebuild it accordingly. Is this the right approach ?

I'm using UiApp.


Solution

  • Srik already arrived one very common approach for page navigation/organization.

    Here is another way using a query string attribute to fork off to a different UI path. This has the added benefit of getting the activity "spinner".

    You can see it in action at this link below with the code powering it underneath it. Hope this provides another perspective.

    https://script.google.com/macros/s/AKfycbx68wR5HmCbil_LY8LlMd2m16_xNEdEtXq7-YfgqsMPqeoe-E3L/exec

    function doGet(e) {
      var app = UiApp.createApplication();
      var page = e.parameter.page
      
      //add a default page
      if(!page){
        page = '1';  
      }
      
      if(page === '1'){
        var label = app.createLabel('You are in page 1.')
        
        var anchor = app.createAnchor("Next page",ScriptApp.getService().getUrl()+"?page=2");
        anchor.setTarget('_self');
        
        app.add(label);
        app.add(anchor);
      }
      else if(page === '2'){
        var label = app.createLabel('You are in page 2, now.')
        
        var anchor = app.createAnchor("Go back",ScriptApp.getService().getUrl()+"?page=1");
        anchor.setTarget('_self');
        
        app.add(label);
        app.add(anchor);
      }
      
      return app;
    }