1

I tried the simplest way of calling Google Apps Script server-side function from my html using the sample code given here https://developers.google.com/apps-script/guides/html/reference/run. It works like a charm.

However, when I try to do the same thing in my actual project which has all the server code in a library, it doesn't work. I keep getting the error

"Uncaught TypeError: google.script.run.doSomething is not a function"

Here is a sample project that I created to recreate the issue

The Gdoc Here look for "Test Menu" and click on "open sidebar" to invoke the functionality. Access the bound script to see the code and the usage of Library.

Library code

Any help with this would be much appreciated.

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • it is really hard to help you without any assist of you, could you share the project which is not working? can you share the relevant piece of code ? (for example for me it's unclear what you mean by "in my project / all the code in a library" are you talking about a local project or something?) – CrissCrossCrass Jan 23 '20 at 06:06
  • @CrissCrossCrass edited the post with all details. Thanks for your help! – Somali Batra Jan 23 '20 at 07:07
  • I assume you realize that `google.script.run.doSomething` should be `google.script.run.doSomething()`. – Cooper Jan 23 '20 at 07:14
  • Yes, I do. If you open my project, you will see that's how I have coded. – Somali Batra Jan 23 '20 at 09:05
  • 1
    When using the `google.script.run` API, only public methods of your server-side script are exposed. If you want to reference library methods, then I believe you need a public wrapper for them: `function useLib() { return myLibName.doSomething(); }` – tehhowch Jan 23 '20 at 13:54
  • Does this answer your question? [Call Library function from html with google.script.run](https://stackoverflow.com/questions/48928932/call-library-function-from-html-with-google-script-run) – tehhowch Jan 23 '20 at 14:04
  • See also https://stackoverflow.com/questions/31352328/server-error-google-script-run-fails-when-using-a-shared-library – tehhowch Jan 23 '20 at 14:04

2 Answers2

0

Example:

html:

<script>
function doSomething() {
  google.script.run
  .withSuccessHandler(function(msg){
    window.alert(msg);//Thank's I got it.
  })
  .doSomething();
} 
</script>

<body>
  <input type="button" value="Do Something" onClick="doSomething();" />
</body>

gs:

function doSomething() {
  return 'Thanks I got it';
}
Cooper
  • 59,616
  • 6
  • 23
  • 54
  • If I copy paste this stuff directly in the script bound to the doc, then all works fine. My problem is when I put this code in the library and then invoke it from my script bound to the doc then it doesn't. I see, the issue is something to do with me using the library, but I don't know what it is and how can I resolve it... When I say library, I mean this https://developers.google.com/apps-script/guides/libraries – Somali Batra Jan 23 '20 at 09:08
  • 1
    Are you adding the library name before the function? – Cooper Jan 23 '20 at 09:22
  • can you please precisely tell which function are you talking about? From the bound script all library functions that I call are prefixed with the library name. – Somali Batra Jan 23 '20 at 16:26
  • I've never tried to access library code using google.script.run. – Cooper Jan 23 '20 at 19:35
0

You are trying to call DocumentApp.getUi() from the library.

As you can see here

A script can only interact with the UI for the current instance of an open document, and only if the script is bound to the document.

Your library is not bound to your document. This is why your code cannot work.

You can only move those parts of your code into a library that do no use getUi() or any Not-shared resources (e.g. triggers). The documentation specifies which resources are shared and which ones are not.

ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • I don't agree. I have used DocumentApp.getUi() at many places in my other project and it works just fine – Somali Batra Jan 23 '20 at 16:31
  • You mean you have DocumentApp.getUi() implemented in a library and could access it from a script into which you incorporated this library? I tried to do this before posting my answer and found that it was not possible - at least for the creation of sidebars. – ziganotschka Jan 23 '20 at 16:34