Google SpreadSheets get cell color
Asked Answered
L

3

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;
 
}
Ladon answered 16/10, 2021 at 18:57 Comment(2)
Can I ask you about your current issue?Attrition
Yes, of course!Ladon
M
0

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

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

Ejemplos

Marchpast answered 19/10, 2021 at 13:27 Comment(2)
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.Monophysite
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 sameLadon
M
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

Magnetomotive answered 19/10, 2021 at 12:59 Comment(0)
R
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))

Rural answered 9/11, 2023 at 12:3 Comment(0)
M
0

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

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

Ejemplos

Marchpast answered 19/10, 2021 at 13:27 Comment(2)
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.Monophysite
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 sameLadon

© 2022 - 2025 — McMap. All rights reserved.