Following situation.
I have a drop down table in a cell.
The drop down options are taken from a range of cells, A1:A20 for example.
The cells in A1:A20 have different background colors.
When creating the drop down from the range A1:A20, the background color of all drop down options is white.
Is there a way to make it, so that the drop down options have the background color of the corresponding cells?
https://docs.google.com/spreadsheets/d/1dLg9QVy2sHN0fPdB1EGe1mbHi-2EGAZG7r8DuCMlyQI/edit?usp=sharing
I’ve been looking at tutorials online for the datavalidation rule builder.
But not a single one showed how to format the drop down options, if thats even possible,
Seprilox is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
Here’s another alternative way.
There’s no direct way to influence the color selection chip on the Data Validation itself what you can do instead is use on Edit and just change the background after selecting.
Depending on your end result this could be a helpful work around.
You can try this code using Google App script:
function applyDropdownColor() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
var sourceRange = sheet.getRange('A1:A20');
var backgrounds = sourceRange.getBackgrounds();
sheet.getRange('B1').setDataValidation(
SpreadsheetApp.newDataValidation()
.requireValueInRange(sourceRange, true)
.build()
);
var firstValue = sourceRange.getValues()[0][0];
if (firstValue) {
sheet.getRange('B1').setBackground(backgrounds[0][0]);
}
}
function onEdit(e) {
updateBackgroundColor(e);
}
function updateBackgroundColor(e) {
var sheet = e.source.getActiveSheet();
var targetCell = sheet.getRange('B1');
if (e.range.getA1Notation() === targetCell.getA1Notation()) {
var selectedValue = e.value;
var sourceRange = sheet.getRange('A1:A20');
var values = sourceRange.getValues();
var backgrounds = sourceRange.getBackgrounds();
values.forEach((row, i) => {
if (row[0] === selectedValue) {
targetCell.setBackground(backgrounds[i][0]);
}
});
}
}
This script creates a drop-down menu
in cell B1 that lets you choose from A1
and changes B1’s background color based on your selection.
The updateBackgroundColor
function ensures that when you pick a value from the drop-down, B1’s background color updates match the color in A1.
Sample Output:
Reference/s:
getBackground
onEdit
1
I can’t seem to find a way to do that dynamically (that is, for the colours of the options to change automatically if the reference cells’ background colours change) in Apps Script.
However, if the background colours of your reference range are meant to stay fixed, you can manually format the colour of the drop-down options in the Google Sheets ‘Data validation rules’ side menu:
user27425627 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3
Use the colorfulDropdownLists_ script.
For best effect, choose the Arrow display style instead of the Chip display style in dropdown Advanced options.
3