Google 文档脚本中的边栏按钮 onClick 未触发
Sidebar button onClick in Google docs script doesn't fire
我正在尝试通过单击按钮来机械化调用文档脚本例程(请参阅 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') ;
}
如果我从脚本调试器调用它们,一切正常,但如果我打开文档,边栏创建正常,但当我点击按钮时没有任何反应。
检查显示:
userCodeAppPanel:1 Uncaught ReferenceError: CallFromSidebarButton is not defined
at HTMLButtonElement.onclick (userCodeAppPanel:1)
文档已共享 here。您可能需要登录并同意一堆内容。
要从客户端代码调用服务器端函数,您必须使用 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') ;
资源
- https://developers.google.com/apps-script/guides/web
- https://developers.google.com/apps-script/guides/dialogs
- https://developers.google.com/apps-script/guides/html/communication
相关
我正在尝试通过单击按钮来机械化调用文档脚本例程(请参阅 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') ;
}
如果我从脚本调试器调用它们,一切正常,但如果我打开文档,边栏创建正常,但当我点击按钮时没有任何反应。
检查显示:
userCodeAppPanel:1 Uncaught ReferenceError: CallFromSidebarButton is not defined
at HTMLButtonElement.onclick (userCodeAppPanel:1)
文档已共享 here。您可能需要登录并同意一堆内容。
要从客户端代码调用服务器端函数,您必须使用 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') ;
资源
- https://developers.google.com/apps-script/guides/web
- https://developers.google.com/apps-script/guides/dialogs
- https://developers.google.com/apps-script/guides/html/communication
相关