2
votes

In a Google Sheets spreadsheet, I have some cells green and red-colored. I want a cell having the count of those green/red colored cells.

Is there a formula to do this thing or any custom code for this?

1
I'm not aware of any way to could access the cell's color, but if the color is somehow related to the cell contents, I'm sure you could write a formula and/or script to do it. Is there any relationship between a cell's color and its contents?opowell
Color is not related to contentasma

1 Answers

1
votes

This Google Apps Script should get you started (see example spreadsheet):

function countRedBackgrounds() {
  var COUNT_RED = 0;
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Cell colors");
  var dataRange = sheet.getDataRange();
  for (var i = 1; i<dataRange.getNumRows(); i++) {
    for (var j = 1; j<dataRange.getNumColumns(); j++) {
      if (dataRange.getCell(i,j).getBackground() == "#ff0000")
        COUNT_RED++;
    }
  }
  dataRange.getCell(1,1).setValue(COUNT_RED);
}