【问题标题】:Google Appscript WebApp Refresh谷歌应用脚​​本网络应用刷新
【发布时间】:2020-03-13 22:45:25
【问题描述】:

有人知道使用 Javascript 为已部署的 Google Appscript Web 应用程序触发窗口刷新的有效方法吗?

我已经搜索并测试了 window.location.href = window.location.hrefwindow.reload(true) 等的变体。

似乎没有任何效果。

我发现的唯一解决方法是创建一个隐藏的网址链接并使用 .click() 命令导航到同一页面(即刷新),但最近,这引起了问题,尤其是因为我现在必须跟踪不同的链接地址。

有没有人有更好的选择?

HTML:

<html>

<head>
  <title>Finance Mileage Entry</title>
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
  <base target="_top">
  <?!= include("page-css");?>
  <a id="refreshLink" href="<?= ScriptApp.getService().getUrl() ?>?v=home" style="display:none"></a>

JS:

function refresh(){
    var link = document.getElementById("refreshLink")
    link.click();
};

【问题讨论】:

    标签: javascript html google-apps-script web-applications


    【解决方案1】:

    为了刷新Web App,你可以使用Window.open(url, windowName),像这样:

    window.open("https://script.google.com/macros/s/{your-webapp-id}/exec", "_top");
    

    更新:

    您也可以通过ScriptApp.getService().getUrl() 检索脚本 URL,而不是自己编写。为此,您必须从服务器端检索此信息(您不能从客户端访问像 ScriptApp 这样的类)。您必须使用google.script.run 从客户端执行服务器端功能:

    function retrieveUrl() {
      google.script.run.withSuccessHandler(refresh).getUrl();
    }
    

    这样,retrieveUrl 将从您的服务器代码 (.gs) 中执行一个名为 getUrl 的函数,它可能是这样的:

    function getUrl() {
      return ScriptApp.getService().getUrl();
    }
    

    最后,当getUrl成功返回时,success handler将执行一个名为refresh的客户端函数(其返回值——脚本URL——将作为参数传递):

    function refresh(scriptUrl) {
      window.open(scriptUrl, "_top");
    }
    

    参考:

    【讨论】:

    • 谢谢,但这与我使用的类似,并且仅适用于 dev 或 exec 版本,因为我无法动态更改链接。没有更简单的刷新选项吗?
    • @haby22 不确定这是否是您想要的,但我根据您的评论更新了我的答案。
    猜你喜欢
    • 1970-01-01
    • 2018-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多