Jan 24, 2022

Getting color codes of font or background color from cell in google sheets

Hi :)

I managed to implement my own little function in excel where I could get the font color code from a cell. After setting things up I could only write =myFunction in the cell right next to the one I wanted to get the color code from and it showed me a value like 44 e.q. Is this somehow also possible in google sheets? That would be awesome. Even a workaround would be great, bcs its messing my whole spreadsheet up when I can get these color codes.

Here's a little screenshot of what im looking for. I set the =ColorCode_Font function in the name manager of excel. If you need the formula feel free to ask so i will try to translate it from german to english. I used an old Excel4 macro function, probably that already helps.

Thanks for any advise or tips.
Kind regards
Locked
Informational notification.
This question is locked and replying has been disabled.
Community content may not be verified or up-to-date. Learn more.
Last edited Jan 24, 2022
Recommended Answer
Jan 24, 2022
Hi KG

Thanks a lot for your feedback, I will keep that in mind next time I'll post something :)

I'm not using CF to colour the background of the cells (it's either known - black or unknown - red). I have to do that manually since there is no way to get the data, for which I made this spreadsheet, out of the game at all or atleast in any easy way. I think I worked out something in Apps Script on myself which should work pretty well. Maybe someone can correct me if I'm wrong with my attempt or make use of this. Probably not the best solution but it works.

function getFontColor() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[2];
var range = sheet.getRange(ss.getActiveCell().getRow(),ss.getActiveCell().getColumn()).offset(0,-1);

return range.getFontColor();

}

function getBackgroundColor() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[2];
var range = sheet.getRange(ss.getActiveCell().getRow(),ss.getActiveCell().getColumn()).offset(0,-2);

return range.getBackgroundColor();

}

That returns the hex code of either to font or the background color when used in your spreadsheet. Trying to convert these into Excel color codes in any way so I can work on with these a little bit better.

You might wonder why I used the offset function, I'll explain that rq to prevent confusion. It's because im working with 2 help columns to count all the different background and font colours. The data isn't sensitive at all so I'll add another sc so future readers will understand a little bit better ;) 1st help column is for the font color, therefore .offset(0,-1)​, the 2nd help column is for the background color, so .offset(0,-2).


Hope that will help future readers with the same issue in any way.
Greetings Skyhawk
Last edited Jan 24, 2022
Diamond Product Expert Gill K recommended this
Helpful?
Recommended Answer
Jan 24, 2022
Hi skyhawk28_,

It isn't directly possible in Sheets to get the colour info using formulas.
But there are add-ons that may provide the functionality you need.  Search for "Function by color", in the google Workspace Marketplace, or similar apps.  Maybe Custom Count and Sum.

But note that there may be an easier way.  How are those cells getting coloured in the first place - are you doing that manually, or with conditional formatting?
If by CF, then the same logic that you use to colour the cells can also be used to identify which colour each cell is, and you could do formulas based on that same logic.

To make it easier for us to develop and test possible solutions, and to demonstrate them for you, would you please take a look at this post about Best Practices When Asking a Question and share a sample sheet that has the permissions set for “Anyone with link” can EDIT?  This will also guide you in avoiding sharing any sensitive data.  

And this Blank Sheet Maker​​​​​​​ can create a shareable sheet for you - just add your sample data.

Thanks!  KG
Original Poster Skyhawk28_ marked this as an answer
Helpful?
All Replies (2)
Recommended Answer
Jan 24, 2022
Hi skyhawk28_,

It isn't directly possible in Sheets to get the colour info using formulas.
But there are add-ons that may provide the functionality you need.  Search for "Function by color", in the google Workspace Marketplace, or similar apps.  Maybe Custom Count and Sum.

But note that there may be an easier way.  How are those cells getting coloured in the first place - are you doing that manually, or with conditional formatting?
If by CF, then the same logic that you use to colour the cells can also be used to identify which colour each cell is, and you could do formulas based on that same logic.

To make it easier for us to develop and test possible solutions, and to demonstrate them for you, would you please take a look at this post about Best Practices When Asking a Question and share a sample sheet that has the permissions set for “Anyone with link” can EDIT?  This will also guide you in avoiding sharing any sensitive data.  

And this Blank Sheet Maker​​​​​​​ can create a shareable sheet for you - just add your sample data.

Thanks!  KG
Original Poster Skyhawk28_ marked this as an answer
Recommended Answer
Jan 24, 2022
Hi KG

Thanks a lot for your feedback, I will keep that in mind next time I'll post something :)

I'm not using CF to colour the background of the cells (it's either known - black or unknown - red). I have to do that manually since there is no way to get the data, for which I made this spreadsheet, out of the game at all or atleast in any easy way. I think I worked out something in Apps Script on myself which should work pretty well. Maybe someone can correct me if I'm wrong with my attempt or make use of this. Probably not the best solution but it works.

function getFontColor() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[2];
var range = sheet.getRange(ss.getActiveCell().getRow(),ss.getActiveCell().getColumn()).offset(0,-1);

return range.getFontColor();

}

function getBackgroundColor() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[2];
var range = sheet.getRange(ss.getActiveCell().getRow(),ss.getActiveCell().getColumn()).offset(0,-2);

return range.getBackgroundColor();

}

That returns the hex code of either to font or the background color when used in your spreadsheet. Trying to convert these into Excel color codes in any way so I can work on with these a little bit better.

You might wonder why I used the offset function, I'll explain that rq to prevent confusion. It's because im working with 2 help columns to count all the different background and font colours. The data isn't sensitive at all so I'll add another sc so future readers will understand a little bit better ;) 1st help column is for the font color, therefore .offset(0,-1)​, the 2nd help column is for the background color, so .offset(0,-2).


Hope that will help future readers with the same issue in any way.
Greetings Skyhawk
Last edited Jan 24, 2022
Diamond Product Expert Gill K recommended this
false
11117657359576942971
true
Search Help Center
true
true
true
true
true
35
false
false
Search
Clear search
Close search
Main menu