如何从客户端调用 google 应用程序脚本中的 class 方法?
How to invoke a class method in google app script from client side?
如何从客户端调用 google 应用程序脚本中的 class 方法?
//客户端
function myClientSideFun() {
google.script.run.withSuccessHandler(onSuccess).myClass.myClassMethod()
function onSucces(msg) { console.log(msg) }
}
//服务器端
class MyClass {
myClassMethod() { return "myMsg" }
}
let myClass = new MyClass()
除非您在不同的顶级函数中导出 class 方法,否则无法从客户端直接调用 class 方法。 类 只是现有对象的语法糖。 Private functions 上的文档明确指出 obj.objectMethod()
不可从客户端调用。
作为使用对象方法的简单示例。
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;
}
选项 2
要建立在@Bede 指出的基础上,有很多方法可以使用服务器端对象。
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](); }
非常感谢您提供的示例和答案,基于此,我似乎正在想出另一种方法如何在服务器端封装函数——实际上是使用对象文字:
const appFunctionLibrary = {
appFunctions: {
"fun1": function (arg1) {
return arg1
},
"fun2": function (arg1, arg2) {
return [arg1, arg2]
},
"fun3": function () {
return "fun without param"
}
}
}
const main = requestedFunction =>
appFunctionLibrary.appFunctions[requestedFunction.name]
(requestedFunction.arguments)
从客户端调用/1 方式:也在客户端脚本中创建一个“对象”并调用 ../:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<button type="button" onclick="clientObject.clientFun.call(clientObject)">Test</button>
<script>
function ClientObj() {
this.clientFun = function(){
let arg1 = 'hello'
let arg2 = 'from client'
google.script.run.withSuccessHandler(function onSuccess(msg) {
console.log(msg)
}).main({name:'fun2', arguments: `${arg1}, ${arg2}`}) // .main({name:'fun3', arguments: ""}) .main({name:'fun1', arguments: `${arg1}`})
}
}
clientObject = new ClientObj()
</script>
</body>
</html>
如何从客户端调用 google 应用程序脚本中的 class 方法? //客户端
function myClientSideFun() {
google.script.run.withSuccessHandler(onSuccess).myClass.myClassMethod()
function onSucces(msg) { console.log(msg) }
}
//服务器端
class MyClass {
myClassMethod() { return "myMsg" }
}
let myClass = new MyClass()
除非您在不同的顶级函数中导出 class 方法,否则无法从客户端直接调用 class 方法。 类 只是现有对象的语法糖。 Private functions 上的文档明确指出 obj.objectMethod()
不可从客户端调用。
作为使用对象方法的简单示例。
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;
}
选项 2
要建立在@Bede 指出的基础上,有很多方法可以使用服务器端对象。
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](); }
非常感谢您提供的示例和答案,基于此,我似乎正在想出另一种方法如何在服务器端封装函数——实际上是使用对象文字:
const appFunctionLibrary = {
appFunctions: {
"fun1": function (arg1) {
return arg1
},
"fun2": function (arg1, arg2) {
return [arg1, arg2]
},
"fun3": function () {
return "fun without param"
}
}
}
const main = requestedFunction =>
appFunctionLibrary.appFunctions[requestedFunction.name]
(requestedFunction.arguments)
从客户端调用/1 方式:也在客户端脚本中创建一个“对象”并调用 ../:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<button type="button" onclick="clientObject.clientFun.call(clientObject)">Test</button>
<script>
function ClientObj() {
this.clientFun = function(){
let arg1 = 'hello'
let arg2 = 'from client'
google.script.run.withSuccessHandler(function onSuccess(msg) {
console.log(msg)
}).main({name:'fun2', arguments: `${arg1}, ${arg2}`}) // .main({name:'fun3', arguments: ""}) .main({name:'fun1', arguments: `${arg1}`})
}
}
clientObject = new ClientObj()
</script>
</body>
</html>