我已经在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
最佳答案
只是用一个答案来结束帖子,就像@TheMaster在上面评论的那样,建议我看看
官方应用程序脚本文档的相关部分:“客户端服务器通信”?有关更多详细信息,请参见info page。
因此,我已经像这样修复了我的代码:
// Here is(was) my problem----------------------------------------
$('#search_phrases').click(function() {
google.script.run.higlightPhrases($('#url_text').val())
});
谢谢! @大师
关于javascript - 单击Google Doc侧边栏上的按钮时,我需要执行function(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61146562/