Home > Back-end >  How to invoke a class method in google app script from client side?
How to invoke a class method in google app script from client side?

Time:05-23

How to invoke a class method in google app script from client side ? //client side

function myClientSideFun() { google.script.run.withSuccessHandler(onSuccess).myClass.myClassMethod()
function onSucces(msg) { console.log(msg) }
}

//server side class MyClass { myClassMethod() { return "myMsg" }
}

let myClass = new MyClass()

CodePudding user response:

Unless you export the class method in a different top level function, it is not possible to directly call class methods from the client. Classes are just syntactic sugars around existing objects. The documentation on Private functions clearly says that obj.objectMethod() isn't callable from the client.

CodePudding user response:

As a simple example of using an object method.

HTML_Test

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <input id="testParam" type="text">
    <script>
      (function () {
        google.script.run.withSuccessHandler(
          function(param) {
            document.getElementById("testParam").value = param;
          }
        ).getTestParam();
      })();
    </script>
  </body>
</html>

Code.gs

class TestObject {
  constructor(param) {
    this.param = param;
  }
  get getParam() { return this.param; }
}

var obj = new TestObject("hello");

function getTestParam() {
  return obj.getParam;
}
  • Related