javascript - 单击Google Doc侧边栏上的按钮时,我需要执行function()

标签 javascript html google-apps-script google-docs sidebar

我已经在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/

相关文章:

javascript - DateTime错误21-undefined-2014

php - 当 readyState 为 4 时,为什么我在脚本中调用的 Ajax 函数会连续运行两次?

javascript - FullCalendar 正在加载 > 未捕获的类型错误 : Cannot read property 'push' of undefined

twitter - 使用 Google Apps 脚本将推文发布到我的供稿

javascript - 范围高度不正确-Google Apps脚本

javascript - 无法在我的表单按钮上实现Confirm()函数

javascript - 一个函数的多个 setIntervals (Javascript)

javascript - Rails——ERB 忽略了 JS/jQuery

javascript - 在 React 组件中使用的字符串变量中插入 html

javascript - 如何触发附加到我的页面的内联 Javascript? (ASP.NET 网络表单)