问题描述
我在 html 中有很多带有 data-action="someAction"
属性的锚标记。
我想从 1 个 JavaScript 点击事件中调用这些操作,如下所示:
但是这个测试抛出错误
Uncaught TypeError: google.script.run.withSuccessHandler(...).withFailureHandler(...).withUserObject(...).action is not a function
更新:
我已经检查过 console.log(action)
是 string
类型,
有什么方法可以使它成为 Google App Script 的方法,以便我下面的脚本可以工作?
我的脚本:
$('.icon-action').on("click",function() {
let action = $(this).attr("data-action");
google.script.run
.withSuccessHandler(
function(returnSuccess,element) {
// return success
})
.withFailureHandler(
function(msg,element) {
// return error
})
.withUserObject(this)
.action();
});
解决方法
在 Oleg Valter 的帮助和建议下,我解决了这个问题。 对于未来的用户,我在这里发布我的工作解决方案。 请参考此评论 - call google app script user defined method on JavaScript event
$('.icon-action').on("click",function() {
let action = $(this).attr("data-action");
google.script.run
.withSuccessHandler(
function(returnSuccess,element) {
// return success
})
.withFailureHandler(
function(msg,element) {
// return error
})
.withUserObject(this)[action]();
});