本文介绍了服务器错误,使用共享库时google.script.run失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个Google Apps脚本项目,都是电子表格类型.

I have two Google Apps Script projects, both of the Spreadsheet type.

让我们呼叫一个服务器和另一个 Client .

Let's call one Server and the other Client.

我想公开一些服务器中的功能,以便可以从 Client 中调用它们.

I want to expose some functions from Server so I can call them from Client.

图书馆对此似乎很完美.

Libraries seem perfect for this.

不幸的是,当我使用资源->库...菜单选项将服务器库添加到客户端时,东西中断了.

Unfortunately when I add the Server library to Client using the Resources --> Libraries... Menu option, stuff breaks.

注意,即使 Server 库已添加到 Client ,我实际上从未使用过任何 Server 库功能. (实际上,服务器代码是完全空白.)

Note, even though the Server library is added to the Client, I never actually use any Server library functions. (In fact Server Code.gs is totally blank.)

//nothing, totally blank, these are test projects created find out what is wrong

客户 Code.gs

Client Code.gs

//This is our entry point
//It instantiates the Sidebar from Sidebar.html
function test()
{
  SpreadsheetApp.getUi().showSidebar(HtmlService.createHtmlOutputFromFile('Sidebar')
  .setSandboxMode(HtmlService.SandboxMode.IFRAME));
}


function testReturn()
{
  Logger.log("doTest() called");
  return 1;
}

客户端 Sidebar.html

Client Sidebar.html

<script>
function yay(d)
{
  alert('yay: ' + d);
}

function boo(d)
{
  alert('boo: ' + d);
}

google.script.run.withFailureHandler(boo).withSuccessHandler(yay).testReturn();
</script>

应该发生什么:

  1. 我们致电 Client .test().

  1. We call Client.test().

这会从Sidebar.html

This opens a Sidebar from Sidebar.html

侧边栏运行:

google.script.run.withFailureHandler(boo).withSuccessHandler(yay).testReturn();

  • 这应从客户端 Code.gs

  • This should call testReturn() from Client Code.gs

    testReturn()完成(或未完成)时...

    And when testReturn() finishes (or doesn't)...

    应调用yay(d)boo(d).

    非常简单...

    因此,当我们连接了 NO 库时,将调用yay(d)! (在这种情况下,使用d = 1)

    So when we have NO library attached, yay(d) is called! (With d = 1 in this case)

    但是,当我们 DO 附加了一个库时,会使用调用boo(d)

    But when we DO have a library attached, boo(d) is called with

    此外,我相信testReturn()永远不会运行,因为它不会显示在日志中.

    Also, I believe testReturn() is never run because it does not show up in the logs.

    我做错了什么?这是Google方面的错误吗?

    What I am doing wrong? Is this a bug on Google's side?

    我公开了测试项目:

    • 服务器 (服务器键,可轻松将其添加为库:Mb7vYHsQuTR9f4-cMPhFPYBfykRDizSBp)

    • Server (Server key for easily adding it as a library: Mb7vYHsQuTR9f4-cMPhFPYBfykRDizSBp)

    客户端

    总而言之,我不明白为什么简单地添加共享库会破坏google.script.run ....功能.特别是因为库是空白的.

    To summarize, I do not understand why simply adding a shared library breaks the google.script.run.... functionality. Especially because the library is blank.

    推荐答案

    tl; dr 是包含电子表格的脚本.对您的库使用独立脚本,就可以了.

    tl;dr Your "library" is a spreadsheet-contained script. Use a stand-alone script for your library, and you'll be fine.

    我创建了一个脚本&包含示例代码的库,并且效果很好.我抓取了您的共享文件的副本,它们都是电子表格,并重现了此错误:

    I created a script & library with your example code, and it worked perfectly. I grabbed copies of your shared files, which were both spreadsheets, and reproduced this error:

    We're sorry, a server error occurred. Please wait a bit and try again.
    

    我尝试了几个著名的公共图书馆, SheetConverter BetterLog .如果我从项目中删除了您的库,一切都很好.

    I tried a couple of well-known public libraries, SheetConverter and BetterLog. If I removed your library from the project, all was well.

    观察:问题不是不是存在 a ,而是 您的.

    Observation: The problem is not the presence of a library, it's the presence of your library.

    与我的手抄本和两个公共图书馆相比,您的图书馆有什么特别之处?您的包含电子表格.其他三个是独立的.

    What's special about your library, compared to my hand-rolled copy of it and the two public libraries? Yours is spreadsheet-contained. The three others are stand-alone.

    结论:不要那样做.对于库,请使用独立脚本.

    Conclusion: Don't do that. For libraries, use stand-alone scripts.

    我在当前的任何文档中都没有发现这一点.碰巧的是,我所有的库都是独立的脚本,因此我以前从未遇到过此限制,也无法说这是新内容还是旧文档或示例表达了独立要求.

    I haven't found this spelled out in any current documentation. As it happens, all my libraries are stand-alone scripts, so I've never run into this limitation before, and can't say whether it's something new or if older documentation or examples expressed the stand-alone requirement.

    关于您的其他问题/问题...

    About your other questions / problems...

    WRT中的或不"部分...服务器功能throw发生异常时,将调用failureHandler.您观察到的错误消息就是这样的异常之一.如果您有意要调用failureHandler,则服务器代码应为throw.

    WRT the "or doesn't" part... a failureHandler is invoked when the server function throws an exception. The error message you've observed is one such exception. If you purposely want to invoke a failureHandler, your server code should throw.

    触发脚本无法始终访问编辑器的日志查看器,因此您应考虑使用BetterLog库将日志写入电子表格.它甚至适用于没有附加调试器的触发功能和Web应用程序. (只是说,因为缺少日志并不能可靠地暗示从客户端代码调用的功能未运行.)

    The editor's log viewer is not always accessible to triggered scripts, so you should consider using the BetterLog library to write logs to your spreadsheet. It works even for trigger functions and web apps that have NO attached debugger. (Just saying, because the lack of a log cannot reliably imply that a function called from client code did not run.)

    这篇关于服务器错误,使用共享库时google.script.run失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 09-05 11:33