I have three colors in column F: green, grey and red. What I want is for column G to display a number (1, 2, 3) depending on the color value of F.
Is there a way to do this?
Web Applications Stack Exchange is a question and answer site for power users of web applications. It only takes a minute to sign up.
Sign up to join this communityI have three colors in column F: green, grey and red. What I want is for column G to display a number (1, 2, 3) depending on the color value of F.
Is there a way to do this?
The cosmetic features of cells like width, font, color, and even number formatting are not themselves considered data by Google Sheets. The cells shown here are all set to the value 0.5, and both by what was typed into them and according to Sheets formulas, they are all identical:
In the future, always carefully consider if you really need to use anything outside the best practices of "normalized spreadsheet" usage; be it color-is-data, bi-directional data flow, spreadsheet-as-a-database-system, transactional writes, script onEdit dependence, or any other attractive divergence into bad plans.
Here, since those features are not data, we cannot natively use them as a data source in formulas. Achieving the new functionality the question described will therefore require extending the existing features of Sheets by way of a third-party app, an add-on, or Google Apps Script.
I've chosen, though don't normally recommend, Apps Script. If you're new to Apps Script, there are endless tutorials to follow. Two notes though: for one, Sheets has changed over the years so things will look the same if it's a more recent guide; and two, scripts copy-pasted from the web could potentially be malicious, accidentally destroy your data, or steal your file's info. With scripts, but even with Google-ecosystem Add-ons, you should ensure you understand what you're about to run.
That out of the way, you can choose Script Editor from the Tools menu, erase the starter function, enter some code, save it, name the project when prompted, and lastly when the script tries to access your Sheets file, click through (set, your account, advanced, allow, …) to grant it permission to run when prompted.
On to my example script. First, a bit of convenience:
function onOpen() {SpreadsheetApp.getUi().createMenu("Scripts").addItem("Number by Colors", "numberColors").addToUi();}
To run the rest of the code on demand right from the Sheets file, I opted to have the script add a menu to the Sheets interface. If you put the above in your script and save it, a new "Scripts" menu will be available in that Sheets file every time you open or reload the Sheets page (or to make it appear right away, choose Run→ Run Function→ onOpen from the script editor menus).
Here is a sample Sheets file to try it all out, to experiment, or before using it on real data. The script is included, and some colors to be turned to numbers. You'll still need to open the Script Editor once, run onOpen, and grant permissions as above.
The following should populate column G according to the background coloration of cells in column F. I'm not sure exactly which colors you used, so I chose the most-pale green and red, and "light gray 1", for their subtlety.
function numberColors() {
// ⬇ script 'Settings' below. UPDATE IF RESTRUCTURING OR RECOLORING SHEET! ⬇
var colorCol = 6, // 6 is F
numberCol = 7, // 7 is G
firstDataRow = 2, // first row in which to begin color checks (to skip headers)
palette = [
"#ffffff", // white (or your spreadsheet default)
"#d9ead3", // light green 3
"#d9d9d9", // light gray 1
"#f4cccc" // light red 3
];
// ⬆ script 'Settings' above. UPDATE IF RESTRUCTURING OR RECOLORING SHEET! ⬆
var numbers = [],
activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(),
dataNumRows = activeSheet.getMaxRows()-(firstDataRow-1),
colorColColors = activeSheet.getRange(firstDataRow, colorCol, dataNumRows, 1).getBackgrounds();
for (var rowIndex in colorColColors) {
numbers[rowIndex] = [];
var rowColor = colorColColors[rowIndex].toString(),
colorIndex = palette.indexOf(rowColor);
if (colorIndex == -1) {
numbers[rowIndex].push(rowColor); // custom output when cell color is missing from script palette.
} else if (colorIndex == 0) {
numbers[rowIndex].push(""); // custom output for first palette color (spreadsheet default). "" means blank.
} else {
numbers[rowIndex].push(colorIndex);
}
}
activeSheet.getRange(firstDataRow, numberCol, dataNumRows, 1).setValues(numbers);
}
Roughly restated in English:
//
line, which means a comment. You'll see these all over and I won't mention them again. Even mid-line, from any //
to the end of that line is just a note you can edit to best help you, or just remove.firstDataRow
assignment to 5
.""
in Sheets.