ScriptApp.getService().getUrl()的行为似乎已更改 [英] Behavior of ScriptApp.getService().getUrl() seems to have changed
问题描述
摘要
一段时间以来,我一直在使用ScriptApp.getService().getUrl()
成功获取Google Apps脚本项目中的当前实例(dev | prod).从昨天开始,返回值似乎完全取决于URL是否以/dev
结尾.还有其他人看到此问题或有解决方法吗?
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?
以前的行为
getUrl
将始终返回https://script.google.com/a/dustinluck.com/macros/s/{deployment-id}/exec
.部署ID正是我在浏览器的URL栏中看到的.
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.
当前行为
getUrl
始终返回dev URL,如果不是,则返回prod URL.由于URL正确以/dev
结尾,因此在第一次显示Web表单时这不是问题,但是,似乎在提交表单并使用google.script.run
调用Google Apps脚本代码时,URL包含/callback
,因此getEnv
的计算结果为prod
.
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
.
要复制的示例代码
该应用程序被发布为以我的身份执行并允许匿名访问,但是,与用户访问该Web应用程序和/或限制对该Web应用程序的访问一样,发布该应用程序时,我会看到相同的行为.
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>
测试网址
这两个都有prod脚本ID,但是getUrl
的结果会改变,具体取决于它是以/dev
还是/exec
结尾.
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
- 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
推荐答案
OP将其发布为问题
更新
(2020年4月17日)重新发布此脚本后,返回了先前的(正确)行为.我只能猜测Google最近的一些更新解决了这个问题.
(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.
这篇关于ScriptApp.getService().getUrl()的行为似乎已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!