7

I wasted a whole day on this and still haven't solved it. How the hell do I create a function that returns the background color of a specified cell? How to get the range of the function's argument, and then its background color? This is what I tried:

/**
 * Returns the Hexadecimal value of a cell's background color.
 * @param {cel} input The cell to get color.
 * @customfunction
 */
function getColor(cel) {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(cel);
  var background = ss.getBackground();
  return background;
 
}

2
  • Can I ask you about your current issue?
    – Tanaike
    Commented Oct 16, 2021 at 23:21
  • Yes, of course! Commented Oct 17, 2021 at 7:01

3 Answers 3

1

The function you use is set-up as a custom function

  • This means that it can be called from the spreadsheet and returns a value to the cell from which it is called.

  • To call it you need to type into a cell preceeded by a = and with a range notation in quotes passed as funciton parameter.

Sample call:

=getColor("F2")

enter image description here

0
1

In case someone come back to this question, the initial code is correct, but it need to be called with the address of the cell.

Example:

=BACKGROUND_COLOR(cell("address"; A1))

0

Good morning attached proposal put A1 in quotation marks "A1"

function myBackgroundRanges(myRange,myTigger) {  
  return SpreadsheetApp.getActiveSpreadsheet()
          .getActiveSheet()
            .getRange(myRange)
              .getBackgrounds();}

Ejemplos

2
  • 1
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Oct 19, 2021 at 16:16
  • Thank you! It works but is there any way to change the result when the color changes? Now, when the color of the cell changes, the result remains the same Commented Oct 20, 2021 at 16:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.