自动重定向到页面

Rob*_*icz 12 google-sites google-apps-script

在按钮单击处理程序中,我正在创建一个新的网页,如下所示:

var page = SitesApp.getPageByUrl(url).createPageFromTemplate(title, name, template);
Run Code Online (Sandbox Code Playgroud)

我想将用户自动重定向到该页面.

我无法找到太多信息,可以这样做吗?

Dav*_*pez 11

Corey G的答案对我有用,但问题是我重定向的网页在GAS的响应中被嵌入到iframe中,因此Web浏览器中的真实URL是脚本的.

这实际上对我有用:

function doGet() {
  return HtmlService.createHtmlOutput(
    "<script>window.top.location.href='https://example.com';</script>"
  );
}
Run Code Online (Sandbox Code Playgroud)

这个类似的Q/A帮助我解决了这个问题.

希望能帮助到你.

  • 这给了我一个错误。“尝试从 URL 为“https://{id}-script.googleusercontent.com/userCodeAppPanel”的框架启动原点为“https://script.google.com”的框架导航是不安全的。尝试导航顶部的框架-level 窗口使用“allow-top-navigation-by-user-activation”标志进行沙箱处理,但没有用户激活(也称为手势)。请参阅 https://www.chromestatus.com/feature/5629582019395584。” (2认同)

Cor*_*y G 10

这不能在UiApp中完成,但它在HtmlService中是可行的:

function doGet() {
  return HtmlService.createHtmlOutput(
    "<form action='http://www.google.com' method='get' id='foo'></form>" + 
    "<script>document.getElementById('foo').submit();</script>");
}
Run Code Online (Sandbox Code Playgroud)

这应该更容易; 请在问题跟踪器中提交功能请求,我们将看到如何使这更加愉快.

(编辑:要清楚,没有办法从UiApp回调中执行此操作;您的整个应用程序必须使用HtmlService才能实现此功能.)

  • 这不再适用了.我已经打开了[问题](http://code.google.com/p/google-apps-script-issues/issues/detail?id=1995).*Star*它跟踪更新.@CoreyG请看看. (2认同)