-
-
Save jwdinkel/6e4051f057d62204907b to your computer and use it in GitHub Desktop.
This file contains 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
function getBackgroundColor(rangeSpecification) { | |
var sheet = SpreadsheetApp.getActiveSpreadsheet(); | |
return sheet.getRange(rangeSpecification).getBackground(); | |
} | |
function getForegroundColor(rangeSpecification) { | |
var sheet = SpreadsheetApp.getActiveSpreadsheet(); | |
return sheet.getRange(rangeSpecification).getFontColor(); | |
} | |
function sumWhereBackgroundColorIs(color, rangeSpecification) { | |
var condition = function (cell) { return cell.getBackground() == color; }; | |
return sumByCondition(color, rangeSpecification, condition); | |
} | |
function sumWhereBackgroundColorIsNot(color, rangeSpecification) { | |
var condition = function (cell) { return cell.getBackground() != color; }; | |
return sumByCondition(color, rangeSpecification, condition); | |
} | |
function sumWhereForegroundColorIs(color, rangeSpecification) { | |
var condition = function (cell) { return cell.getFontColor() == color; }; | |
return sumByCondition(color, rangeSpecification, condition); | |
} | |
function sumWhereForegroundColorIsNot(color, rangeSpecification) { | |
var condition = function (cell) { return cell.getFontColor() != color; }; | |
return sumByCondition(color, rangeSpecification, condition); | |
} | |
/* Thank you for count functions: */ | |
/* https://gist.github.com/WaffleSouffle */ | |
/* https://gist.github.com/WaffleSouffle/7675480 */ | |
function countWhereBackgroundColorIs(color, rangeSpecification) { | |
var condition = function (cell) { return cell.getBackground() == color; }; | |
return countByCondition(color, rangeSpecification, condition); | |
} | |
function countWhereBackgroundColorIsNot(color, rangeSpecification) { | |
var condition = function (cell) { return cell.getBackground() != color; }; | |
return countByCondition(color, rangeSpecification, condition); | |
} | |
function countWhereForegroundColorIs(color, rangeSpecification) { | |
var condition = function (cell) { return cell.getFontColor() == color; }; | |
return countByCondition(color, rangeSpecification, condition); | |
} | |
function countWhereForegroundColorIsNot(color, rangeSpecification) { | |
var condition = function (cell) { return cell.getFontColor() != color; }; | |
return countByCondition(color, rangeSpecification, condition); | |
} | |
/* Added skip empty cells in this fork: https://gist.github.com/jeffdgr8/1553faa6360ad04e9c17 */ | |
/* Thank you https://gist.github.com/jeffdgr8 */ | |
function sumByCondition(color, rangeSpecification, condition) { | |
var sheet = SpreadsheetApp.getActiveSpreadsheet(); | |
var range = sheet.getRange(rangeSpecification); | |
var x = 0; | |
for (var i = 1; i <= range.getNumRows(); i++) { | |
for (var j = 1; j <= range.getNumColumns(); j++) { | |
var cell = range.getCell(i, j); | |
if(condition(cell)) { | |
var cellVal = parseFloat(cell.getValue()); | |
if (isFinite(cellVal)) /* Thank you https://gist.github.com/jeffdgr8 */ | |
x += cellVal; | |
} | |
} | |
} | |
return x; | |
} | |
function countByCondition(color, rangeSpecification, condition) { | |
var sheet = SpreadsheetApp.getActiveSpreadsheet(); | |
var range = sheet.getRange(rangeSpecification); | |
var x = 0; | |
for (var i = 1; i <= range.getNumRows(); i++) { | |
for (var j = 1; j <= range.getNumColumns(); j++) { | |
var cell = range.getCell(i, j); | |
if(condition(cell)) | |
x += 1; | |
} | |
} | |
return x; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment