[英]Google Apps Script - return output from apps script function back to the html file javascript function
Ok, so I am trying to make a function in google sheets that when the user selects a cell and then runs the function (currently trying to make), a sidebar getting all the synonyms of the word should appear. 好的,所以我试图在Google表格中创建一个函数,当用户选择一个单元格然后运行该函数(当前试图创建)时,应该会出现一个侧边栏,该侧边栏将获取该单词的所有同义词。 I am using https://words.bighugelabs.com/ to get the synonyms.
我正在使用https://words.bighugelabs.com/来获取同义词。 So first I make the menu:
所以首先我做菜单:
`function onOpen(e) {
var ui = SpreadsheetApp.getUi().createMenu("Sidebar")
.addItem("Get Synonym", 'showSidebar')
.addToUi();
}`
Then this is the showSidebar function: 然后是showSidebar函数:
function showSidebar() {
var html = HtmlService.createHtmlOutputFromFile("Test")
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setWidth(150)
.setTitle("My Sidebar");
SpreadsheetApp.getUi().showSidebar(html);
}
This is the html file: 这是html文件:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script>
function doSomething() {
var synonyms = google.script.run.getSynonym();
document.getElementById("synonyms").innerHTML = synonyms;
}
</script>
</head>
<body>
<div>
<span style="color:orange;">This is a test sidebar!</span>
<button onclick="doSomething()">Click Me!</button>
<div id="synonyms"></div>
</div>
</body>
</html>
And this is the getSynonym function: 这是getSynonym函数:
function getSynonym() {
var word = SpreadsheetApp.getActiveRange().getValue();
var synonyms = [];
var response = UrlFetchApp.fetch("http://words.bighugelabs.com/api/2/{my_api_key}/" + word + "/json");
response = JSON.parse(response);
var synonyms = response.adjective.syn;
return synonyms;
}
But the variable synonyms which as an array of synonyms doesn't get returned to the doSomething function in the Html file. 但是作为同义词数组的变量同义词不会返回到Html文件中的doSomething函数。
What I want is that the sidebar should get a list of all the synonyms. 我想要的是,边栏应获取所有同义词的列表。 So basically I can't get the data from one function to another...and I want to know if this is the right way.
因此,基本上我无法从一个函数获取数据到另一个函数……而且我想知道这是否正确。
When calling server side functions using google.script.run
you need to define a success handler, which will asynchronously receive your response. 使用
google.script.run
调用服务器端函数时,您需要定义一个成功处理程序,该处理程序将异步接收您的响应。
See the examples on: https://developers.google.com/apps-script/guides/html/communication 请参阅以下示例: https : //developers.google.com/apps-script/guides/html/communication
function onSuccess(synonyms ) {
console.log(synonyms);
}
google.script.run.withSuccessHandler(onSuccess).doSomething();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.