0

I'm using the following script in Google Sheets. It works well but I want to limit it to a specific tab rather than every tab in the book.

function countBackground(color, inputRange) {
  var inputRangeCells = SpreadsheetApp.getActiveSheet().getRange(inputRange);
  var rowColors = inputRangeCells.getBackgrounds();
  var count = 0;

  for(var r = 0; r < rowColors.length; r++) {
    var cellColors = rowColors[r];
    for(var c = 0; c < cellColors.length; c++) {
      if(cellColors[c] == color) {
        count++;
      }
    }
  }

  return count;
}

How can I do this?

  • Instead of getActiveSheet, switch to getSheetByName or getSheets. – Ron Kloberdanz Aug 20 '18 at 21:14
  • Thanks Ron, I'll try this later today. - Had to switch off the original script it was affecting every tab – Peter Kay Aug 21 '18 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.