首页 > 解决方案 > google.script.run 函数不会生成 Logger.log

问题描述

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

代码.gs

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

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

时钟.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>

标签: google-apps-script

解决方案


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

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

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


推荐阅读