单击GoogleDoc侧栏上的按钮时,我需要执行一个function()

问题描述 投票:0回答:1

我已经在Google文档上创建了一个非常简单的侧边栏作为附加组件。但是,由于我在这件事上没有自己想要的先进,所以我陷入了困境。此侧边栏只有一个文本框和一个按钮,用户可以在其中输入字符串文本,然后只需单击该按钮即可“执行功能”(这是我需要帮助的地方)。

我具有功能,并且有效!但是,我不知道如何通过侧边栏的按钮来构建脚本来执行功能。

我对此事进行了研究,发现没有做任何事情,也没有给出任何错误消息。只是行不通。如果你们知道我在哪里可以找到可以实现我的目标的以前的问题,请告诉我!如果没有,请告诉我该怎么做。

这是html文件代码:

<!DOCTYPE html>
<!-- Use this CSS stylesheet to ensure that add-ons styling
matches the default Google Docs styles -->
<link href="https://ssl.gstatic.com/docs/script/css/add-ons.css"
rel="stylesheet">

<!-- The sidebar will have a input box and the search button -->
<div class="sidebar">

<!-- The search box -->
<div class="block form-group">
<input type="text" id="url_text" placeholder="Enter DB spreadsheet url" />
<button class="blue" id="search_phrases">Search Phrases</button>
</div>

<!-- Load the jQuery library from the Google CDN -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>

<script>
// Attach click handlers after the Sidebar has loaded in Google Docs
$(function() {

// Here is my problem----------------------------------------
$('#search_phrases').click(function() {
higlightPhrases($('#url_text').val())
});

// If the user presses the Enter key in the search box, perform a search
$('#url_text').keyup(function(e) {
if (e.keyCode === 13) {
$('#search_phrases').click();
}
});

});
</script>

而且,我的功能如下(在脚本文件中):

 function higlightPhrases(db_url) {
     // For comfortable reasons, I've reduced the function script into a message.
     DocumentApp.getUi().alert(db_url);
 }

感谢您的帮助!AJ

javascript html google-apps-script google-docs sidebar
1个回答
1
投票

仅用一个答案结束帖子,正如@TheMaster在上面评论的那样,建议我查看

官方应用程序脚本文档的相关部分:“客户端服务器通信”?有关更多详细信息,请参见info page

因此,我已经像这样固定了我的代码:

// Here is(was) my problem----------------------------------------
$('#search_phrases').click(function() {
google.script.run.higlightPhrases($('#url_text').val())
});

谢谢! @TheMaster

© www.soinside.com 2019 - 2024. All rights reserved.