Reputation: 131
I´m new to .gs so this should not be difficult.
I have a Google Spreadsheet with values in a column (Lets say column A). I have created a custom menu using .gs where the user will select an option.
Clicking one of the options (new component) a popup appears with a dropdown menu where user should select from the values.
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>
I want the dropdown menu to show all elements in column A from the spreadsheet. I´ve tried several options but have not obtained the required result.
So, how do I need to proceed to implement the dropdown populating process?
Upvotes: 2
Views: 6982
Reputation: 201523
If my understanding is correct, how about this modification? I think that there are several answers for your situation, so please think of this as just one of them.
The flow of this modified script is as follows.
google.script.run
, run the function of getValuesFromSpreadsheet()
at Google Apps Script side.getValuesFromSpreadsheet()
, the values are retrieved from Spreadsheet and returned to Javascript.withSuccessHandler()
, the returned values are retrieved. Using the values, a select box is created.selected()
is run and retrieved the selected value.When above flow is reflected, the modified script is as follows.
Please add the following Google Apps Script to 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
}
Please add the following Javascript to 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
for retrieving the values from Spreadsheet.withSuccessHandler
, retrieve the values from Google Apps Script.If I misunderstood your question, I'm sorry.
Upvotes: 3