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?
conditional formattinggoogle sheets
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?
Best Answer
Why
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.
How
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:
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.
What
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.
Huh?
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 to5
.""
in Sheets.