Skip to content

Instantly share code, notes, and snippets.

@aquiladev
Created January 5, 2018 12:35
Show Gist options
  • Save aquiladev/79b626999982d0417932f614c7352238 to your computer and use it in GitHub Desktop.
Save aquiladev/79b626999982d0417932f614c7352238 to your computer and use it in GitHub Desktop.
function sumColoredCells(sumRange) {
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 sum = 0;
for(var i=0;i<bg.length;i++)
for(var j=0;j<bg[0].length;j++)
if(bg[i][j] == activeRange.getBackground())
sum += parseFloat(values[i][j]);
return sum;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment