Last active
May 11, 2022 22:56
-
-
Save marcup/fb5f0fd27de53b91c0e3cd595256a12f to your computer and use it in GitHub Desktop.
Google Docs Script to count number of cells in a sheet that match with a specified background colour
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {range} countRange Range to be evaluated | |
* @param {range} colorRef Cell with background color to be searched for in countRange | |
* @return {number} | |
* @customfunction | |
*/ | |
function countMatchingColoredCells(countRange,colorRef) { | |
var activeRange = SpreadsheetApp.getActiveRange(); | |
var activeSheet = activeRange.getSheet(); | |
var formula = activeRange.getFormula(); | |
var rangeA1Notation = formula.match(/\((.*)\,/).pop(); | |
var range = activeSheet.getRange(rangeA1Notation); | |
var bg = range.getBackgrounds(); | |
var values = range.getValues(); | |
var colorCellA1Notation = formula.match(/\,(.*)\)/).pop(); | |
var colorCell = activeSheet.getRange(colorCellA1Notation); | |
var color = colorCell.getBackground(); | |
var count = 0; | |
for(var i=0;i<bg.length;i++) | |
for(var j=0;j<bg[0].length;j++) | |
if( bg[i][j] == color ) | |
count=count+1; | |
return count; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello...
I'm receiving the following error:
Error
TypeError: Cannot read property 'pop' of null (line 13).
Help ?