【问题标题】:google.script.run function won't generate Logger.log [duplicate]google.script.run 函数不会生成 Logger.log [重复]
【发布时间】:2021-12-09 20:10:28
【问题描述】:

我正在尝试复制一个简单的 Web 应用教程视频,但无法让 google.script.run.function 在我的 code.gs 中创建日志。这是我的代码:

代码.gs

function doGet(){
  return HtmlService.createHtmlOutputFromFile("clock");
}

function userClicked(){
  Logger.log("Log clicked");
  console.log("Console click");
}

clock.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <h1>test</h1>
    <div id="replace">___</div>
    <button id="submit">Submit</button>

    <script>
      document.getElementById("submit").addEventListener("click",doSubmit);

      function doSubmit(){
        google.script.run.userClicked();
        document.getElementById("replace").innerHTML = "clicked";
      }
    </script>
  </body>
</html>

当我在编辑器中运行 userClicked 时,执行日志显示正常。此外,当在 web 应用程序中单击按钮时,“单击”文本显示得很好。此外,doGet 和 userClicked 都出现在我的 Executions 中。问题是从 webapp 运行时日志不会显示在我的执行日志中。我找到了几个类似的线程,但它似乎永远不会得到解决。

更新:我也尝试添加 withSuccessHandler 并且结果是相同的 - 函数都在执行中运行良好,但没有日志显示在执行日志中(如果我在旧版本中测试,“日志”也是如此) .新的 html 是这样的:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <h1>test</h1>
    <div id="replace">___</div>
    <button id="submit">Submit</button>

    <script>
      document.getElementById("submit").addEventListener("click",doSubmit);

      function onSuccess(){
        document.getElementById("replace").innerHTML = "success";
      }

      function doSubmit(){
        google.script.run.withSuccessHandler(onSuccess).userClicked();        
      }
    </script>
  </body>
</html>

【问题讨论】:

  • 你需要使用 SuccessHandler
  • 2-3 分钟后查看executions(不是code editor
  • @TheMaster doGet 和 userClicked 几乎立即出现在 Executions 中。问题是“Log clicked”文本没有显示在编辑器日志中,我不知道为什么。
  • @Cooper 我刚刚更新以添加 withSuccessHandler;结果相同

标签: google-apps-script


【解决方案1】:

如果您创建一个函数并使用您的服务器函数调用该函数,那么可行的是:

function logHi(){
  console.log("hi")
}

// And then use it
function userClicked(){
  logHi()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 2015-12-14
    • 2017-11-23
    • 1970-01-01
    • 2016-10-18
    • 2019-08-29
    • 1970-01-01
    相关资源
    最近更新 更多