Skip to content

Instantly share code, notes, and snippets.

@tojibon
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tojibon/0f8258853bf831075da5 to your computer and use it in GitHub Desktop.
Save tojibon/0f8258853bf831075da5 to your computer and use it in GitHub Desktop.
A Google Spreadsheet Function to count number of cells in a column which all are used selected background colors.
/*
---------------------------------------------------------------------------------------
Returns total number of rows for selected background colors separated by comma, in a column range.
@ param string colors - A set of background hex colors separated by comma. Ex: '#ff0000,#000000,#AEAEAE'
@ param string rangeSpecification - Column field range of rows. Ex: 'E1:E20'
@ return int number of rows with colors listed background colors.
---------------------------------------------------------------------------------------
*/
function countCellsWithBackgroundColors(colors, rangeSpecification) {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var backGroundColors = sheet.getRange(rangeSpecification).getBackgrounds();
var colorArray = colors.split(",");
var x = 0;
for (var i = 0; i < backGroundColors.length; i++) {
for (var j = 0; j < backGroundColors[i].length; j++) {
for (var c = 0; c < colorArray.length; c++) {
if (colorArray[c] == backGroundColors[i][j]) {
x++;
}
}
}
}
return x;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment