我是.gs的新手,所以这应该不难。
我有一个Google电子表格,其中有一个列中的值(比如A列)。我使用.gs创建了一个自定义菜单,用户将在其中选择一个选项。
单击其中一个选项(新组件),弹出窗口将显示一个下拉菜单,用户可以在其中选择值。
CustomMenu.gs
function onOpen() {
var ui = SpreadsheetApp.getUi();
// Or DocumentApp or FormApp.
ui.createMenu('bq Library Menu')
.addItem('Add new component', 'newComponent')
.addSeparator()
.addSubMenu(ui.createMenu('Modify existing component')
.addItem('Remove component', 'removeComponent'))
.addToUi();
}
function newComponent() {
var html = HtmlService.createHtmlOutputFromFile('NewComponentForm')
.setWidth(400)
.setHeight(300);
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.showModalDialog(html, 'New Component Form');
}NewComponentForm.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<h2>Clickable Dropdown</h2>
<p>Select manufacturer from the list</p>
<div class="dropdown">
<button onclick="loadManufacturers()" class="dropbtn">Dropdown</button>
<div id="myDropdown" class="dropdown-content"></div>
</div>
</body>
我希望下拉菜单显示电子表格中A列中的所有元素。我尝试了几种选择,但没有得到所需的结果。
那么,如何继续执行下拉填充过程呢?
发布于 2019-01-16 23:11:19
如果我的理解是正确的,这次修改怎么样?我认为你的处境有几个答案,所以请把这看作是其中之一。
此修改脚本的流程如下所示。
google.script.run,在Google脚本端运行getValuesFromSpreadsheet()功能。getValuesFromSpreadsheet(),从电子表格中检索值并返回到Javascript。withSuccessHandler(),检索返回的值。使用这些值,将创建一个复选框。selected()并检索所选值。当反映上述流时,修改后的脚本如下所示。
气体:
请将以下Google脚本添加到CustomMenu.gs中。
function getValuesFromSpreadsheet() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
return sheet.getRange(1, 1, sheet.getLastRow(), 1).getValues(); // Retrieve values and send to Javascript
}
HTML:
请将以下Javascript添加到NewComponentForm.html。
<script>
function loadManufacturers() {
google.script.run.withSuccessHandler(function(ar) {
let select = document.createElement("select");
select.id = "select1";
select.setAttribute("onchange", "selected()");
document.getElementById("myDropdown").appendChild(select);
ar.forEach(function(e, i) {
let option = document.createElement("option");
option.value = i;
option.text = e;
document.getElementById("select1").appendChild(option);
});
}).getValuesFromSpreadsheet();
};
function selected() {
const value = document.getElementById("select1").value;
console.log(value);
}
</script>google.script.run从电子表格检索值。withSuccessHandler,从Google脚本中检索值。注意:
参考文献:
如果我误解了你的问题,我很抱歉。
https://stackoverflow.com/questions/54217028
复制相似问题