To display unique values from a Google Sheet's Column A in a dropdown within an HTML page using Google Apps Script, you'll need to set up a web app. The following steps guide you through creating a simple web application that reads unique values from Column A and displays them in a multi-select dropdown.
Step 1: Prepare the Google Apps Script
-
Open Google Apps Script: Go to your Google Sheet, click on Extensions > Apps Script.
-
Clear any existing code and paste the new script:
function doGet() {
return HtmlService.createHtmlOutputFromFile('Index');
}
function getUniqueValues() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getActiveSheet();
const dataRange = sheet.getRange("A1:A" + sheet.getLastRow()).getValues();
let flatValues = [].concat(...dataRange).filter(String); // Flatten and filter empty strings
let uniqueValues = [...new Set(flatValues)]; // Get unique values
return uniqueValues; // Return an array of unique values
}
- The
doGet() function serves the HTML file ('Index.html' in this case) when you access the web app URL.
- The
getUniqueValues() function fetches unique values from Column A and returns them.
- Create the HTML file: Click on
File > New > Html file, name it 'Index', and replace any existing code with the following:
<!DOCTYPE html>
<html>
<head>
<title>Dropdown from Sheet</title>
<script>
// Immediately-invoked function to load unique values for dropdown upon page load
(function() {
google.script.run.withSuccessHandler(function(values) {
const selectElement = document.getElementById('unique-values-dropdown');
values.forEach(value => {
const option = document.createElement('option');
option.text = value;
option.value = value;
selectElement.add(option);
});
}).getUniqueValues();
})();
</script>
</head>
<body>
<h3>Select a Value:</h3>
<select id="unique-values-dropdown" multiple style="width: 200px; height: 150px;">
<!-- Options will be added here by the script above -->
</select>
</body>
</html>
This HTML contains a <select> element configured for multiple selections and uses an immediately-invoked function expression (IIFE) to fetch unique values from the server-side getUniqueValues function upon page load.
Step 2: Deploy as Web App
-
Save and Name Your Project: Give your project a name if you haven't already.
-
Deploy: Click on Deploy > New deployment.
- Click on
Select type > Web app.
- Enter a description for your deployment.
- Under
Execute as, select Me.
- Under
Who has access, choose who you want to have access to the web app (e.g., Anyone).
- Click
Deploy.
-
Authorize and Grant Permissions: You might need to authorize the script and grant it permission to access your Google Sheet data.
-
Access the Web App: Once deployed, you'll get a URL to your web app. Open the URL in your browser to see your dropdown with unique values from your sheet.
This setup provides a basic example of how to display unique values from a Google Sheet in a dropdown on an HTML page using Google Apps Script. Remember, every time you make changes to your script or HTML file, you may need to create a new deployment to reflect those changes.