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()
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;
}
Options 2
To build on what @Bede noted there are many ways to use server side objects.
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({name: "getParam2"});
})();
</script>
</body>
</html>
Code.gs
class TestObject {
constructor(param1,param2) {
this.param1 = param1;
this.param2 = param2;
}
getParam1() { return this.param1 }
getParam2() { return this.param2 }
}
var obj = new TestObject("hello","goodbye");
var getTestParam = param => { return obj[param.name](); }