【问题标题】:GoogleSheets google.script.run always going to FailureHandlerGoogleSheets google.script.run 总是去 FailureHandler
【发布时间】:2021-03-23 17:13:36
【问题描述】:

我正在使用 GoogleSheets HTMLService。我从我的 Html 页面的脚本中调用 google.script.run。但它总是去FailureHandler。它有什么问题?请看下面的代码。当我运行它时,它总是显示警报失败。此外,记录器没有显示任何错误。它也没有在 hello() 函数中显示控制台日志“Inside Hello”。我们是否还需要做一些浏览器设置(我使用的是 chrome - 允许使用 javascript)。

[更新] 将 Logger.log 替换为 console.log 后,我将其视为传输错误。

modeDialog.gs

function openDialog() {
  var html = HtmlService.createHtmlOutputFromFile("test");
  html.setWidth(90).setHeight(1);
  var ui = SpreadsheetApp.getUi().showModalDialog(html, "Opening ..." );
}

function hello() {
  console.log("Inside Hello");
  return "hello";
}


test.html

<!DOCTYPE html>

<html>  
  <head>
  <base target="_top">
  <script>
  function onSuccess(str) {
  window.alert("executed");
  }
  
  function onFailure(error) {
  window.alert("failed");
  Logger.log(error);
  }
  
  google.script.run.withSuccessHandler(onSuccess).withFailureHandler(onFailure).hello();
  
  </script>
  </head>
  <body>
    <div id="failureContent"></div>
    Hello world
  </body>  
</html>


【问题讨论】:

  • Logger 在客户端不可用。请改用console。在开发工具> 浏览器控制台中查看错误日志
  • 在您的情况下,我认为google.script.run 可能会出现类似Failed to load resource: the server responded with a status of 500 的错误。在我的环境中,我也使用google.script.run 确认了这种情况。在这种情况下,当我创建新的电子表格并将脚本复制并粘贴到脚本编辑器并再次运行时,问题就解决了。那么在你的环境中,当你这样做的时候,你会得到什么结果呢?在当前阶段,我无法在问题跟踪器中找到此问题。所以我不确定这是否是您问题的直接解决方案。对此我深表歉意。
  • 尝试将其放入window.onload
  • @shanti 我不确定传输错误是否重要。是什么让您认为 hello 没有运行?你检查过服务器日志吗?查看> 处决?
  • 好的,所以我尝试了 FireFox,它会转到成功处理程序并显示警报“已执行”。 chrome 出了点问题……

标签: google-apps-script google-sheets


【解决方案1】:

我是这样运行的:

GS:

function openDialog() {
  SpreadsheetApp.getUi().showModalDialog(HtmlService.createHtmlOutputFromFile("ah3"), "Opening ..." );
}

function hello() {
  return "hello";
}

HTML:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    Hello world
    <script>
    window.onload=function(){
      google.script.run
      .withSuccessHandler(function(str){window.alert("executed");})
      .withFailureHandler(function(error){window.alert("failed");})
      .hello();
    }  
    console.log('MyCode');
    </script>
  </body>
</html>

我只是喜欢使用 onReadyState 函数或 onload 来运行大多数 javascript,以便 html 已经加载。并不是说在这个微不足道的例子中它有很大的不同。此外,我倾向于将脚本放在正文而不是头部。

【讨论】:

  • 我复制了你的答案并运行它,它仍然失败:(并再次在开发控制台中传输错误和 2451951077-waffle_js_prod_core.js:471 GET chrome-extension://invalid/ net:: ERR_FAILED
  • 我总是遇到这样的传输错误。老实说,我不知道问题出在哪里,最后我不在乎,只要我的脚本运行并且它们运行,所以我不再担心它们。
  • 好的,但它甚至没有执行我的 hello() 函数...
  • 显然我无法确认
  • 你把'ah3'改回'test'了吗。 'ah3' 是我的 html 文件的名称
【解决方案2】:

正如我之前提到的,它在 Chrome 中早期工作并且目前在 FireFox 中工作,我在通过转到 Chrome > 设置 > 高级 > 重置和清理 > 将设置恢复为原始设置将我的 chrome 设置更改为默认值后再次对其进行了测试默认值。

之后一切正常。因此,将其设置为答案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-21
    • 2011-09-19
    • 2019-10-13
    • 2020-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多