Skip to content

Instantly share code, notes, and snippets.

@marcup
Last active October 19, 2020 23:21
Show Gist options
  • Save marcup/a0fb674a1373f5e61b65fc09916ab721 to your computer and use it in GitHub Desktop.
Save marcup/a0fb674a1373f5e61b65fc09916ab721 to your computer and use it in GitHub Desktop.
Google Docs Script that gives the sum of values in cells of a sheet that don't match with a specified background colour
/**
* @param {range} sumRange Range to be evaluated
* @param {range} colorRef Cell without background color to be searched for in sumRange
* @return {number}
* @customfunction
*/
function sumNonMatchingColoredCells(sumRange,colorRef) {
var activeRange = SpreadsheetApp.getActiveRange();
var activeSheet = activeRange.getSheet();
var formula = activeRange.getFormula().toString();
formula = formula.replace(new RegExp(';','g'),',');
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 total = 0;
for(var i=0;i<bg.length;i++)
for(var j=0;j<bg[0].length;j++)
if( bg[i][j] != color )
total=total+(values[i][j]*1);
return total;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment