【问题标题】:Sidebar button onClick in Google docs script doesn't fireGoogle 文档脚本中的侧边栏按钮 onClick 不会触发
【发布时间】:2021-06-05 06:22:45
【问题描述】:

我正在尝试通过单击按钮来机械化调用 docs 脚本例程(请参阅 this question)。似乎唯一的方法是为文档创建一个餐具柜。

这是我的脚本:

function CreateSideBar ()
{
Logger.log ('CreateSideBar Entry')
var HTML = HtmlService.createHtmlOutput ('<button onClick="CallFromSidebarButton () ;">Do It!</button>').setTitle ('My Sidebar') ;
DocumentApp.getUi ().showSidebar (HTML) ;   
Logger.log ('CreateSideBar Exit')
}

function onOpen() 
{
CreateSideBar ()  
}


function CallFromSidebarButton ()
{
var ui = DocumentApp.getUi () ; 
ui.alert ('Call from sidebar OK') ;  
}

如果我从脚本调试器中调用它们,一切正常,但如果我打开文档,侧边栏会创建 OK,但是当我单击按钮时没有任何反应。

检验显示:

userCodeAppPanel:1 Uncaught ReferenceError: CallFromSidebarButton is not defined
    at HTMLButtonElement.onclick (userCodeAppPanel:1)

该文档已共享 here。您可能需要登录并同意一堆东西。

【问题讨论】:

    标签: google-apps-script web-applications google-docs


    【解决方案1】:

    要从客户端代码调用服务器端函数,您必须使用 google.script.run 即替换

    var HTML = HtmlService.createHtmlOutput ('<button onClick="CallFromSidebarButton () ;">Do It!</button>').setTitle ('My Sidebar') ;
    

    通过

    var HTML = HtmlService.createHtmlOutput ('<button onClick="google.script.run.CallFromSidebarButton () ;">Do It!</button>').setTitle ('My Sidebar') ;
    

    资源

    相关

    【讨论】:

    • 工作愉快 - 非常感谢您的回答和资源。我对在调试器中执行时 不起作用 的原始代码如何能够工作感到困惑 - 这也不是客户端吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多