【发布时间】:2021-05-20 09:49:40
【问题描述】:
我正在创建一个谷歌应用程序脚本,并试图将 json 数据从 code.gs 放到 html 文件下拉列表中。我真的不知道这是如何通过对 JS 和谷歌自己的东西没有那么丰富的经验来完成的。代码如下:
function PopulateDropDownList() {
var url = 'url for the api call'
var res = UrlFetchApp.fetch(url, {'api stuff here, it works i have tested it'}})
var json = res.getContentText()
var txt = JSON.parse(json)
people = txt.data.map(function (a) {
return [a.firstName, a.lastName];
})
Logger.log(people)
return people
}
问题是,如何将“人员”列表添加到 html 下拉选择中?我知道它与脚本标签有关,但我真的不知道该怎么做。我也尝试过 goolge.script.run.myFunction 但我不知道如何从那里取得进展
编辑:
HTML 文件
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<select id="droplist">
</select>
<script>
(function () {
google.script.run.withSuccessHandler(
function(people) {
var select = document.getElementById("droplist");
for( var dude of people) {
var option = document.createElement("option");
// option.text = dude.firstName + " " + dude.lastName;
option.text = dude[0] + " " + dude[1];
select.add(option);
}
}
).get_people();
}());
</script>
</body>
</html>
code.gs:
function getPeople() {
/*json stuff here*/
var json = res.getContentText()
var txt = JSON.parse(json)
var people = txt.data.map(function (a) {
return [a.firstName, a.lastName]
})
Logger.log(people)
return people
}
function showDialog() {
var html = HtmlService.createTemplateFromFile("Page").evaluate();
// return html;
SpreadsheetApp.getUi().showModalDialog(html, "List");
}
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('choose person')
.addItem('search', 'showDialog')
.addToUi();
}
【问题讨论】:
-
对不起,我的回答延迟了,我周末不在。
-
在
Page.html中,您调用的是函数).get_people();,但在Code.gs中,函数的名称为getPeople()。尝试修复它。 -
我真的希望你能看到我现在的脸有多难。它现在完美运行。非常感谢你的帮助,你太棒了!
-
其实这对我来说很多都是新事物。我也很高兴知道它是如何工作的。
标签: javascript html json google-apps-script