我正在 Google 表格中创建侧边栏。我可以毫无问题地将其显示在选择列表中。我正在尝试根据其中一张工作表中的范围填充选择列表。我已经尝试了 stackoverflow 上的大量建议,并且多次阅读了 Google 的 HTML 服务文档。我似乎还缺少一些东西。根据我的测试,似乎从未执行对服务器的 google.script.run 调用。这是我的 code.gs 文件:
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Schedule')
.addItem('New schedule', 'newSchedule')
.addItem('Edit start date', 'newDate')
.addSeparator()
.addItem('Insert names', 'openInsert')
.addToUi();
}
function openInsert() {
var html = HtmlService.createHtmlOutputFromFile('Insert')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setTitle('Insert Names')
.setWidth(300);
SpreadsheetApp.getUi()
.showSidebar(html);
}
function getNames() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Publishers");
var names = sheet.getRange(2, 1, sheet.getLastRow(),1).getValues();
return names;
}
这是我的 Insert.html 文件:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
<script>
function returnedNames(names) {
var selectList = new Array("publisher1","publisher2","publisher3");
for (sel in selectList) {
var dropDown = document.getElementById(sel);
for (i = 0; i < names.length; i++) {
var opt = document.createElement("option");
opt.text = names[i][0];
opt.values = names[i][0];
dropDown.options.add(opt);
}
}
}
google.script.run.withSuccessHandler(returnedNames).getNames();
</script>
</head>
<body>
<div class="block form-group">
<label for="publisher1">First publisher</label>
<select id="publisher1"> </select>
<label for="publisher2">Second publisher</label>
<select id="publisher2"> </select>
<label for="publisher3">Third publisher (optional)</label>
<select id="publisher3"> </select>
</div>
</body>
</html>
最佳答案
我相信在您的文档真正准备好处理之前,您的服务器函数就被调用了。我想如果您使用开发人员工具检查浏览器控制台窗口,您会看到弹出错误。
由于您本质上想要这些填充 onload,因此将加载事件添加到 body 标记。让该事件调用您的服务器函数。如果这不起作用,请在整个代码序列中使用服务器端的 Logger.log 和客户端的 console.log 的组合,并查看您得到的内容以及代码遵循的路径。
关于javascript - 用于填充选择列表的 Google Apps 脚本服务器调用从未运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37517320/