Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Behavior of ScriptApp.getService().getUrl() seems to have changed

Summary

I have been using ScriptApp.getService().getUrl() to get the current instance (dev|prod) in my Google Apps Script project successfully for some time now. As of yesterday, the returned value seems to depend completely on whether the URL ends with /dev or not. Has anyone else seen this issue or have a workaround?

Previous Behavior

getUrl would always return https://script.google.com/a/dustinluck.com/macros/s/{deployment-id}/exec. The deployment id would be exactly what I'd see in the browser's URL bar.

Current Behavior

getUrl always returns the dev URL if the address in the browser's URL bar ends with /dev or the prod URL if it doesn't. This is not a problem when first displaying the web form since the URL correctly ends with /dev, however, it seems that when the form is submitted and a call is made to Google Apps Script code using google.script.run, the URL contains /callback and therefore getEnv evaluates to prod.

Sample Code to Reproduce

The app is published to execute as me and allows anonymous access, however, I see the same behavior when published to execute as the user accessing the web app and/or limiting access to the web app.

code.gs

function doGet(e) {
  var pageHtmlTemplate = HtmlService.createTemplateFromFile("default");
  pageHtmlTemplate.env = getEnv();
  return pageHtmlTemplate.evaluate();
}

function getEnv() {
  var devId = 'AKfycbwou0odFWX6II6YaeSaSOaXF4faYrzJ5XygssntdnI';
  var prodId = 'AKfycbxO20AYwEDPvdRsXu_K9pOb-E_iWRV12Wuv3TCApF53vDeuQpU';
  var currUrl = ScriptApp.getService().getUrl();
  var env = "unknown";
  if (currUrl.indexOf(devId) != -1) {
    env = "dev";
  } else if (currUrl.indexOf(prodId) != -1) {
    env = "prod";
  }
  
  return env + " (" + currUrl + ")";
}

default.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  </head>
  <body>
    <h1>Env on load</h1>
    <?= env ?>
    
    <h1>Env on callback</h1>
    <div id="test">
    </div>
  </body>
  <script>
      $(document).ready(function() {
          google.script.run.withSuccessHandler(success).getEnv();
      });

      function success(env) {
          $("#test").html(env);
      }
  </script>
</html>

Test URLs

Both of these have the prod script id, but depending on whether it ends with /dev or /exec, the results of getUrl change.

  • https://script.google.com/a/dustinluck.com/macros/s/AKfycbxO20AYwEDPvdRsXu_K9pOb-E_iWRV12Wuv3TCApF53vDeuQpU/dev
  • https://script.google.com/a/dustinluck.com/macros/s/AKfycbxO20AYwEDPvdRsXu_K9pOb-E_iWRV12Wuv3TCApF53vDeuQpU/exec
like image 821
Dustin Luck Avatar asked Nov 13 '19 19:11

Dustin Luck


1 Answers

Posted by the OP as an "update" to the question

Update

(Apr 17, 2020) After republishing this script, the previous (correct) behavior returned. I can only guess that some recent updates from Google fixed this issue.

like image 160
Rubén Avatar answered Nov 15 '22 08:11

Rubén