EpiX
EpiX

Reputation: 1401

Server error, google.script.run fails when using a shared library

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

Let's call one Server and the other 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.

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.)

Server Code.gs

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

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;
}

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>

What is supposed to happen:

  1. We call Client.test().

    This opens a Sidebar from Sidebar.html

    The Sidebar runs:

    google.script.run.withFailureHandler(boo).withSuccessHandler(yay).testReturn();
    
  2. This should call testReturn() from Client Code.gs

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

  3. Either yay(d) or boo(d) should be called.

Very simple...

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

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

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

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

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

I made the test projects public:

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

Upvotes: 3

Views: 971

Answers (3)

user27213
user27213

Reputation: 11

I would make the following two changes.

  1. Make sure your library is shared with anyone with a Google account.
  2. In the code where you use the library, use a version with a non-zero number. By default, the library version is set to 0. Change it to some non-zero number (hopefully your most recent deployment of the library). If you do not see any version numbers for your library, then you will first need to deploy it.

Upvotes: 1

Jan
Jan

Reputation: 31

5 years later I'm landing here while having similar problem - google.script.run fails with server error 500. My setting is similar - spreadsheet bound GAS and a reference to a library in the same project. Unlike in your case, my library is stand-alone. Also, my library dependent code in the project is independent of the google.script.run related code. If I chuck out the lib & its dependent code from the project then the google.script.run bits work fine.

Solution - in my case at least ... it turns out that the server error only occurs if the library is pointed to its head rather than (any) versioned deployment ([https://developers.google.com/apps-script/concepts/deployments][1]). This applies even when the libs head matches completely with its latest version code-wise. So it's not even the lib code, but the deployment. Likely irrelevant, but just in case - the library references BigQuery API service. I did not experiment with this any further.

Hope this might help to folks landing here with similar problem.

Upvotes: 3

Mogsdad
Mogsdad

Reputation: 45710

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.

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

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

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

    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.

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

    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.)

Upvotes: 4

Related Questions