Google Apps Script individual redirect from Script as Web App

Abstract

This article regards the question "Is it possible (let's say depending on the user name calling the script) to redirect to another URL in a Google script as web app view?"

Objective

The objective is already contained in the question:

Depending on the username, a script as web app view should redirect to another page.

This implementation is needed in the Hotline Planner. Only one link for the My Personal View should be implemented on the Google Site, while three separate systems exists. The My Persional View should redirect the user to the correct system according the user id that is calling the script. A definition (dictionary) from user to system has to be provided.

Techniques

To redirect to different page, the follwing techniques can be used:

  1. Metatag

    <meta http-equiv="refresh" content="0; url=https://example.com/" />

    Redirect works but browser keeps the initial URL.

  2. JavaScript 1

    window.location.href = "https://example.com"

    Redirect works but browser keeps the initial URL.

  3. JavaScript 2

    // window.open(URL, name, specs, replace)

    window.open('https://consulity.de', '_top')

    Redirect works perfectly and the new URL is displayed in the browser's address bar correctly.

  4. Link

    <a target="_top" href="https://consulity.de">go to consulity.de</a>

    According to this article, links have to be used with target="_top" or target="_blank".

  5. Workaround

    After some research, the following solution for GAS was found on stackoverflow

    function doGet() {

        return HtmlService.createHtmlOutput(

        "<form action='https://www.google.com' method='get' id='foo'></form>" +

        "<script>document.getElementById('foo').submit();</script>");

    }

Result

If HTMLService is used with SandboxMode NATIVE or EMULATED, none of the above mentioned techniques work at all.

The results above are described for SandboxMode IFRAME.

Source Code

Code.gs

var url = "";

 

function doGet() {

 

 

    var uid = Session.getActiveUser().getEmail();

    if (uid == "dn@consulity.com") {

        url = 'https://consulity.de';

    }

    else {

        url = 'https://google.de';

    }

 

    return HtmlService.createTemplateFromFile('Index').evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);

 

}

Index.html

<!DOCTYPE html>

<html>

  <head>

    <script src="https://consulity.de/scripts/jquery-1.9.1.min.js"></script>

  </head>

  <body>

    <input id="url" type="hidden" value="<?!= url ?>"/>

    <p><button onclick="go1()">redirect via location href</button></p>

    <p><button onclick="go2()">redirect via window.open</button></p>

    <p><a target="_top" href="https://consulity.de">go to consulity (_top)</a></p>

    <p><a target="_blank" href="https://consulity.de">go to consulity (_blank)</a></p>

  </body>

 

  <script>

  //<!-- redirect via meta tag after 0 seconds-->

  //<meta http-equiv="refresh" content="0; url=https://consulity.de/"/>

 

     $(function(){

       console.log('script ran');

       var url=$('#url').val();

       console.log(url);

       window.open(url,'_top')

     });

    

     function go1(){

       window.location.href='https://consulity.de';

       console.log('script ran');

     }

     function go2(){

       window.open('https://consulity.de','_top')

       console.log('script ran');

     }

  </script>

</html>

Conclusion

The window.open command works perfect when called with "_top" or "_blank" (target).

Here is the solution and the source (access restricted).

The user id "dn@consulity.com" will be redirected to consulity, while all other users will be redirected to google.

Please note:

It is important to use

return HtmlService.createTemplateFromFile('Index').evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);

and not to use

return HtmlService.createHtmlOutputFromFile('Index').setSandboxMode(HtmlService.SandboxMode.IFRAME);

because in the latter case, server variables <?!=?> cannot be evaluated in the HTML context.


Note: The script needs to retrieve the user ID from the session and therefore needs the following rights/scope:




Dieter Neumann