Call Library function from html with google.script.run

It seems that google.script.run can’t look inside Objects or self-executing anonymous functions. In my case, putting any code inside an object or IIFE resulted in “is not a function” type of error being thrown in the console.

You can work around this by declaring a single function that will call nested methods inside libraries and objects.

.GS file

 function callLibraryFunction(func, args){

    var arr = func.split(".");
    
    var libName = arr[0];
    var libFunc = arr[1];
    
    args = args || [];
       
   return this[libName][libFunc].apply(this, args);

}

In JavaScript, every object behaves like an associative array of key-value pairs, including the global object that ‘this’ would be pointing to in this scenario. Although both ‘libName’ and ‘libFunc’ are of String type, we can still reference them inside the global object by using the above syntax. apply() simply calls the function on ‘this’, making the result available in global scope.

Here’s how you call the library function from the client:

 google.script.run.callLibraryFunction("Library.libraryFunction", [5, 3]);

I don’t claim this solution as my own – this is something I saw at Bruce McPherson’s website a while back. You could come up with other ad-hoc solutions that may be more appropriate for your case, but I think this one is the most universal.

Leave a Comment