Update a cell's value according to colored cel

2020-06-25 05:17发布

问题:

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

Can I have a formula to do this thing or any custome code for this?

回答1:

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);
}