如何从客户端中调用Google App脚本中的类方法?

发布于 2025-01-31 15:20:20 字数 318 浏览 5 评论 0原文

如何从客户端中调用Google App脚本中的类方法? //客户端

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()

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

第几種人 2025-02-07 15:20:20

除非将类方法导出不同的顶级功能,否则不可能直接从客户端调用类方法。类只是现有物体周围的句法糖。 私人功能清楚地说, obj.objectMethod()无法从客户端起诉。

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.

空心空情空意 2025-02-07 15:20:20

非常感谢您的示例和答案,基于我看来我正在弄清楚如何将函数封装在服务器端 - 实际上是用对象字面的:从客户

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>

Many thanks for the example and for the answer, based on that it seems I am figuring out another way how could I encapsulate functions on server side - actually with an object literal:

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)

Calling from client side /1 way: also create an "object" in client script and call ../:

<!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>
暗藏城府 2025-02-07 15:20:20

作为使用对象方法的简单示例。

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](); }

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](); }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文