Google Apps Script individual redirect from Script as Web App


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?"


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.


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

  1. Metatag

    <meta http-equiv="refresh" content="0; url=" />

    Redirect works but browser keeps the initial URL.

  2. JavaScript 1

    window.location.href = ""

    Redirect works but browser keeps the initial URL.

  3. JavaScript 2

    //, name, specs, replace)'', '_top')

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

  4. Link

    <a target="_top" href="">go to</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='' method='get' id='foo'></form>" +




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

var url = "";


function doGet() {



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

    if (uid == "") {

        url = '';


    else {

        url = '';



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




<!DOCTYPE html>



    <script src=""></script>



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

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

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

    <p><a target="_top" href="">go to consulity (_top)</a></p>

    <p><a target="_blank" href="">go to consulity (_blank)</a></p>




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

  //<meta http-equiv="refresh" content="0; url="/>



       console.log('script ran');

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




     function go1(){


       console.log('script ran');


     function go2(){'','_top')

       console.log('script ran');





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

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

The user id "" 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