javascript - 单击 GoogleDoc 侧边栏上的按钮时,我需要执行 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);
 }

感谢您的帮助! 阿杰

最佳答案

只是为了用答案来结束帖子,正如 @TheMaster 上面评论的那样,建议我查看

the relevant section of the official apps script documentation: "Client server communication"? See info page for more details.

所以,我像这样修复了我的代码:

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

谢谢! @大师

关于javascript - 单击 GoogleDoc 侧边栏上的按钮时,我需要执行 function(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61146562/

相关文章:

html - FullCalendar - React - 加载功能问题

google-apps-script - 如何仅向最后一次提交的 Google 表单发送电子邮件?

javascript - 为什么这个条件在没有 else 的 return 语句中起作用?

javascript - ReactJS:使用枚举进行条件渲染

html - 在 HTML 中,do::after 和::before 伪元素只适用于可渲染的 "non-replaced"元素吗?

javascript - 如何从Angularjs中的不同文件调用服务

javascript - 如何从循环范围内计数

google-apps-script - Apps 脚本 .getActivePage() 仅返回 "home"页面

javascript - 除法 hh :mm:ss by an integer in Javascript

javascript - Eclipse Javascript 插件扩展