I have the following formula in cell E9:
=sumColoredCells(A1:C6,E1)
and the following formula in cell F9:
=sumColoredCells(A1:C6,F1)
sumColoredCells is a custom function created with the help of the following script code, in this function you need to pass the range in first parameter in which you want to sum the values of the colored cells, and also you need to pass the cell in the second parameter which have the background that you need to sum in the range (provided in first parameter).
Put the following is the script code in the script editor of your spreadsheet:
////////////////////////////////////////////////////
/**
* @param {range} sumRange Range to be evaluated
* @param {range} colorRef Cell with background color to be searched for in sumRange
* @return {number}
* @customfunction
*/
function sumColoredCells(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;
};
NOTE:
When you change the color of any cell the value will not get auto updated in the sum result,
so you need to temporary change the value of any of one of the cell in the range (and then change it back as you want..)
this will update the colored cells sum result...
For getting count instead of sum of values of cells having a background color, look at the following link:
http://igoogledrive.blogspot.com/2015/11/google-spreadsheet-count-of-colored.html
Great!
ReplyDeleteI really like the count and total custom functions!
Could you also make a custom average function based on the same principles?
Excellent code! One modification that I would like to see is getting the range of another spreadsheet, it errors on doing that.
ReplyDeleteI can't get this to work. Can someone help mw?
ReplyDeleteStill having problems Lauren? What exactly is happening?
DeleteIs my script correct?
Deletefunction sumColoredCells(A1:K85,yellow) {
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 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;
};
This comment has been removed by the author.
ReplyDeleteWorks great... Thank you
ReplyDeleteHey Seats for Fans, thank you very much for this nice solution. However, when trying to implement it I receive an error mistake regarding the pop method which is not allowed to be 0 (line 13 & 19). Can you help me here? Thanks a lot!
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteI am getting a Circular dependency error! Help!
ReplyDeleteWhat do you write in your cell E9?
DeleteE9=sumColoredCells(A1:C6,E1)
E1 is the reference color.
It says ERROR. Can someone help me?
ReplyDeleteIt's giving an error, can someone help me with this?
ReplyDeleteTypeError: Cannot call method "pop" of null. (line 14, file "SumColouredCells")
The error occurs because your localization uses the ";" instead of ",". You need to start debugging on the macro page, it will find all the lines in which you need to replace the "," sign with ";". After that, the script works correctly.
DeleteSorry for my poor English))
Михаил Гончаров This is not true... If I change it there is still the problem with calling "pop"
DeleteChange these lines:
Deletevar countRangeAddress = activeformula.match(/\((.*)\;/).pop().trim();
var colorRefAddress = activeformula.match(/\;(.*)\)/).pop().trim();
The script works for me. The only inconvenience is that the sum does not get updated automatically, which I hope it has a solution by now considering this script has been published for so many years....Google.......
ReplyDeleteIm with Hallilujah, the script works as advertised, but could there be the addition of Autosum?
DeleteThere is a workaround. I've been playing with this code to figure it out (a little commenting in the code would have been helpful BTW...especially formula.match). I also needed the total to update if data changed.
DeleteYou simply need to reference any unused cell as part of a formula which includes sumColoredCells. So =AnyUnusedCell + sumColoredCells(SumRange, colorCellRange). The result (since this is an unused cell) will just be the result of the function. But since you have used a cell reference in the formula (AnyUnusedCell), Google Sheets has to reevaluate it anytime a cell value in the spreadsheet is changed. So, the function will then recalculate and will show the updated information.
Hope this helps!
sorry, I don't understand the workaround method, can you guide more details? many thanks.
DeleteThe error
ReplyDeleteTypeError: Cannot call method "pop" of null. (line 13, file "Code")
It's always happening, what can be done so it will not happen?
The error TypeError: Cannot call method "pop" of null. (line 13, file "Code")
ReplyDeleteIt's always happening, what can be done so it will not happen?
Everybody who has "TypeError: Cannot call method "pop" of null" error, that is happening because in the code provided match() is looking for "," as a divider between function arguments, but now it is ";".
ReplyDeleteSo you have to change match(/\((.*)\,/) to match(/\((.*)\;/)
and
change match(/\,(.*)\)/) to match(/\;(.*)\)/)
worked for me
Thanks! It has solved the issue.
DeleteThis comment has been removed by the author.
DeleteThis comment has been removed by the author.
DeleteHi, nope, is not working, the "TypeError: Cannot call method "pop" of null" still happening
ReplyDeleteHow do you make the cells update automatically?
ReplyDeleteHi,
ReplyDeletecould you please suggest a solution how to fix issue during the debug of Sum script? I'd copy-paste and receiving the error
TypeError: It's not possible to execute method "pop" object null. (line 14, file "SumColoredCells")
Actually the code: var rangeA1Notation = formula.match(/\((.*)\,/).pop();
This comment has been removed by the author.
ReplyDeleteHi,
ReplyDeleteI have the same problem. Has anyone figured out the solution?
Any luck? @Filipp Makarov fix isn't working anymore.
ReplyDeleteI have updated two lines in code (colored in green):
Deletevar formula = activeRange.getFormula().toString();
formula = formula.replace(new RegExp(';','g'),',');
Hi, I have this error
ReplyDeleteThe error TypeError: Cannot call method "pop" of null. (line 13, file "Code")
Somebody can help me with this issue? Thanks XD
This comment has been removed by the author.
ReplyDeleteGot a solution for initial tasks:
ReplyDelete1. count amount of green cells;
2. count sum of values of green cells.
Code and leaving comments are available at GitHub:
https://github.com/tooHotSpot/googleSheetsTrick/blob/master/Code.gs
The count function works properly but not the sum...
DeleteHello, if i want to input two different colours, how could i do that without getting error about the range?
ReplyDeleteI have the same problem. I would like to use various colors as my color references but the function only seems to take one cell into account as reference
ReplyDeleteThis is awesome! Really saved me.
ReplyDeleteAny chance, though, that this can be modified to be a 'sum by font colour' too? Thank you!
I get an error.
ReplyDeleteI used
=sumColoredCells(N4:N15,N4)
and I get return #NAME?
Same with me. Anyone know what I am doing wrong?
DeleteI had to remove the very first piece of code google put into the script page. "function myFunction()" Keep the "{" part
DeleteThis comment has been removed by the author.
DeleteJust use =sumColoredCells(N4:N15;N4) instead of =sumColoredCells(N4:N15,N4)
DeleteI am getting an error that states: Error
ReplyDeleteRange not found (line 21). I copied and pasted the code. Any suggestions?
I got this error too, the lines in green doesnt work for me
DeleteHi, it gives me error - Range not found.... what should I do? Is the script ok? I made copy and paste from above. Please help! Thanks :)
ReplyDelete{
/**
* @param {range} sumRange Range to be evaluated
* @param {range} colorRef Cell with background color to be searched for in sumRange
* @return {number}
* @customfunction
*/
function sumColoredCells(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;
};
}
it works with
ReplyDelete=sumColoredCells(C2:C9;C3)
it changes back to (C2:C9,C3), and continues to give the same error regarding the "range not found".... thank you!
DeleteWhat would you change in the script to sum multiple colors in one function? Thank you.
ReplyDeleteHi!
ReplyDeleteThanks for the script! Im trying to sum time values and the script it self works but it results in a weird format. Any idea how to edit the script to be able to sum time values correctly?
I debugged this and got it working using the following code. I hope it helps someone.
ReplyDelete/**
* @param {range} sumRange Range to be evaluated
* @param {range} colorRef Cell with background color to be searched for in sumRange
* @return {number}
* @customfunction
*/
function sumColoredCells(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().replace(/\s/g, "");
var range = activeSheet.getRange(rangeA1Notation);
var bg = range.getBackgrounds();
var values = range.getValues();
var colorCellA1Notation = formula.match(/\,(.*)\)/).pop().replace(/\s/g, "");
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;
};
I didn't look what you fixed but your script is the only one that finally worked! Thanks
DeleteThank you very much friend, ... you are the best !!!
DeleteThank you very much friend, ... you are the best !!!
DeleteGood Job, finally freaking worked.
Deletemy comand error on line "var colorCell = activeSheet.getRange(colorCellA1Notation);" is range not found Whats is solutions?
DeleteHi just wondering if this script is able to be used within a sumif function?
ReplyDeleteI tried =sumIF(G2:G6,"Apples",sumColoredCells(H2:H6,P2)) but its not working :(
Buen día
ReplyDeleteme salta este error, alguien conoce como solucionarlo.
TypeError: No se puede llamar al método "pop" de null. (línea 17, archivo "Código")
Cambia la , por ; como está en estas líneas:
Deletevar countRangeAddress = activeformula.match(/\((.*)\;/).pop().trim();
var colorRefAddress = activeformula.match(/\;(.*)\)/).pop().trim();
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteIt is wonderful! You Really Saved Me.
ReplyDeleteIs there any chance that this, too, can be changed to get also the "amount of font color"? Thank!
this website
Yes:
Delete/**
* @param {range} sumRange Range to be evaluated
* @param {range} colorRef Cell with background color to be searched for in sumRange
* @return {number}
* @customfunction
*/
function sumColoredCells(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.getFontColors();
var values = range.getValues();
var colorCellA1Notation = formula.match(/\,(.*)\)/).pop();
var colorCell = activeSheet.getRange(colorCellA1Notation);
var color = colorCell.getFontColor();
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;
};
It is not working in my sheets. It says unknown filter. Can someone please help me?
DeleteHi there, I am having the issue where when inputting the formula I am getting the error "Result was not a number". I have the inputted as "=sumColoredCells(A2:A193,A11)". Any help would be appreciated. Thanks!
ReplyDeleteYou need to move your output box. Your range includes the cell you are trying to put your count/sum in. Move it to a cell out of your range.
DeleteI keep getting an error:
ReplyDeleteTypeError: Cannot call method "getSheet" of null. (line 9, file "sumbycolor")
sumbycolor is just the name of the function I chose to use.
the same situation. the same error
Deletesolved. chenged , to ; FND do not forget to change in the formula =sumColoredCells(A1:C6,E1) also , TO ; :)
DeleteThis comment has been removed by the author.
ReplyDeleteHi everyone. I use this sumColoredCells function but it will NOT auto refresh/calculate if you change cell colors. The only way it refreshes is to change a value in the range. A work-around I found was to create a checkbox in unused cell somewhere to add 1 (or use insignificant fractional (.000001) if you are rounding) to a number in the range, and unchecking will add 0. This will act as a refresh "toggle" switch. Not really ideal, I know... but this is for my personal use and only a minor hassle.
ReplyDeleteis there a way to only count the colored cells if they follow a certain text in the column left to it?
ReplyDeleteSuch as column A being a list of vendor and Column B being a status of invoice (highlighted by color to signify status)
If I wanted to find the total amount of unpaid invoices (highlighted in RED) that Vendor B has for instance. So therefore only summing the red amounts associated with Vendor B in column two and ignoring red cells from other vendors.
I imagine a VLookup type formula added to this, but am not practiced enough to figure it out.
If there are dates in the selected range, how do we update the script to avoid sum the dates???
ReplyDeleteI am looking for a way to change the background color of a cell based on the background color of another cell in GOOGLE SHEETS. I have used conditional formatting to color cells in 'column A' based on text found in a cell but then I would like the neighboring cell in 'column B' to match the color found in the neighboring column A cell. I will then use the 'sum by color' as found in this video to add the colored cells in 'column B'. The video is exactly what I was looking for and it works flawlessly, just need to figure out how to color a cell based on another cell to use this to its full potential. Thank you!
ReplyDeleteVocê tem um script para contar celulas coloridas ao inves de somá-las? Obrigada!
ReplyDeleteConsegui aqui!!
Delete/**
* @param {range} sumRange Range to be evaluated
* @param {range} colorRef Cell with background color to be searched for in sumRange
* @return {number}
* @customfunction
*/
function contarCelulasColoridas(contRange,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+1;
return total;
};
hello, I have the formula exactly the same as shown but it is not allowing me select a function to run in script editor? The function is on the script, but not being detected by GS. How do I enter this in for the formular?
ReplyDeleteNot working for me here the sheet link can someone please solve the problem.
ReplyDeleteThis script work if the cell with format to sum isn't the same of the formula... it's correct?
ReplyDeleteThis script work if the cell with format to sum isn't the same of the formula... it's correct?
ReplyDeleteworked so good!!! THANK YOU FOR THIS! :)
ReplyDeleteTHANK YOU FROM IS RUSSIA:)
ReplyDeleteThank you! It really worked for me!
ReplyDeleteThank You. It worked.
ReplyDeleteBelow code has worked for me.
ReplyDelete/**
* @param {range} sumRange Range to be evaluated
* @param {range} colorRef Cell with background color to be searched for in sumRange
* @return {number}
* @customfunction
*/
function sumColoredCells(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().replace(/\s/g, "");
var range = activeSheet.getRange(rangeA1Notation);
var bg = range.getBackgrounds();
var values = range.getValues();
var colorCellA1Notation = formula.match(/\,(.*)\)/).pop().replace(/\s/g, "");
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;
};
Your comment has been very helpful. This worked for me too. Thank you so much!
DeleteIt works!!! Thanks a lot!!!
DeleteI also have "TypeError: Cannot read property 'pop' of null"
ReplyDeleteI tried replacing "," by ";" and I tried the scripts suggested in the comments above,but nothing works. Anyone has the same issue and was able to fix it? Thanks a lot!
ciao, hai poi risolto con questa funzione? io ancora ho difficoltà ad usarla
Deleteciao, ho bisogno di usare questa funzione, ma non mi funziona
ReplyDeletedopo aver eseguito, compare questo messaggio: TypeError: Cannot read property 'pop' of null
sumColoredCells @ myfunction.gs:14
qualcuno potrebbe scrivere il codice corretto per poter usare questa benedetta funzione (sommaCelleColorate)
I am getting the error "Result was not a number" , please help
ReplyDeleteIs it possible to add another column lookup to this?
ReplyDeleteUse Case #1.
I have values in a column that I want to group and sum but only if a specific color. In column A would be "Type" Plumbing, Electrical, Floors, etc and column B would be Cost and it would result field(s) would consider the color and the value. If cost is green and "target type" is Plumbing. I'd have multiple columns for reference and a different sumColoredCells reference for each.
Hi, it works, but you need some changes to the code.
ReplyDeletefunction sumColoredCells (sumRange, colorRef) {
these "sumRange" and "colorRef" parameters are not used in the code, so they must be changed to "rangeA1Notation" and "colorCellA1Notation".
as follows:
function sumColoredCells (rangeA1Notation, colorCellA1Notation) {
Or just simply delet it...
Deletefunction sumColoredCells () {
The code works! Thanks.
DeleteHas anyone figured out how to update the code to make the cell auto update the sum when any of the values changes?
Thanks
I would also appreciate a workaround with an autorefresh.
ReplyDeleteSince often the numbers do not change, but the colors do, for what I need it, so, it is a bit annoying that we need to retype values all the time, making it prone to mistakes as well. Any thoughts? My skills in coding this thing are null :)
This code is returning the error TyperError: Cannot read property 'getSheet' of null
ReplyDeleteThis isn't working for me. I copy/pasted the code and it looks exactly the same. When I input the formula on my spreadsheet, I get #NAME? and if I hover over the cell, it says 'Error, Unknown Function'
ReplyDelete