1
votes

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.

2
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
@CrissCrossCrass edited the post with all details. Thanks for your help!Somali Batra
I assume you realize that google.script.run.doSomething should be google.script.run.doSomething().Cooper
Yes, I do. If you open my project, you will see that's how I have coded.Somali Batra
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

2 Answers

0
votes

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';
}
0
votes

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.