如何根据Google工作表中的单元格颜色对单元格进行计数或求和?
根据特定的单元格背景色对单元格值进行计数或求和,以获得结果,如以下屏幕截图所示。 本文,我将讨论如何在Google表格和Microsoft Excel中解决此任务。
在Google工作表中使用脚本对基于单元格颜色的单元格值求和
在Microsoft Excel中使用Kutools for Excel对单元格颜色的单元格值进行计数或求和
使用Google工作表中的脚本根据单元格颜色计算单元格值
以下脚本可以帮助您根据特定的单元格颜色来计算单元格值,请执行以下操作:
1。 点击 工具 > 脚本编辑器,请参见屏幕截图:
2。 在打开的项目窗口中,单击 文件 > 新 > 脚本文件 要打开代码窗口,请参见屏幕截图:
3。 在提示框中,请输入此脚本代码的名称,请参见屏幕截图:
4。 点击 OK 然后将以下代码复制并粘贴以将原始代码替换到代码模块中,请参见屏幕截图:
function countColoredCells(countRange,colorRef) { var activeRg = SpreadsheetApp.getActiveRange(); var activeSht = SpreadsheetApp.getActiveSheet(); var activeformula = activeRg.getFormula(); var countRangeAddress = activeformula.match(/\((.*)\,/).pop().trim(); var backGrounds = activeSht.getRange(countRangeAddress).getBackgrounds(); var colorRefAddress = activeformula.match(/\,(.*)\)/).pop().trim(); var BackGround = activeSht.getRange(colorRefAddress).getBackground(); var countCells = 0; for (var i = 0; i < backGrounds.length; i++) for (var k = 0; k < backGrounds[i].length; k++) if ( backGrounds[i][k] == BackGround ) countCells = countCells + 1; return countCells; };
5。 然后保存此脚本代码,然后返回工作表,输入以下公式: = countcoloredcells(A1:E11,A1) 放入空白单元格,然后按 输入 获得计算结果的键。 看截图:
备注:在此公式中: A1:E11 是您要使用的数据范围, A1 是用您要计数的特定颜色填充的单元格。
6。 重复上述公式以计算其他特定的彩色单元格。
在Google工作表中使用脚本对基于单元格颜色的单元格值求和
要对具有特定单元格颜色的单元格值求和,请应用以下脚本代码。
1. 单击 工具 > 脚本编辑器 转到项目窗口,然后单击 文件 > 新 > 脚本文件 要插入另一个新的代码模块,然后在提示框中输入此脚本的名称,请参见屏幕截图:
2。 点击 OK 在打开的代码模块中,复制并粘贴以下脚本代码以替换原始代码,请参见屏幕截图:
function sumColoredCells(sumRange,colorRef) { var activeRg = SpreadsheetApp.getActiveRange(); var activeSht = SpreadsheetApp.getActiveSheet(); var activeformula = activeRg.getFormula(); var countRangeAddress = activeformula.match(/\((.*)\,/).pop().trim(); var backGrounds = activeSht.getRange(countRangeAddress).getBackgrounds(); var sumValues = activeSht.getRange(countRangeAddress).getValues(); var colorRefAddress = activeformula.match(/\,(.*)\)/).pop().trim(); var BackGround = activeSht.getRange(colorRefAddress).getBackground(); var totalValue = 0; for (var i = 0; i < backGrounds.length; i++) for (var k = 0; k < backGrounds[i].length; k++) if ( backGrounds[i][k] == BackGround ) if ((typeof sumValues[i][k]) == 'number') totalValue = totalValue + (sumValues[i][k]); return totalValue; };
3。 然后保存此代码,返回到工作表,然后输入以下公式: = sumcoloredcells(A1:E11,A1) 放入空白单元格,然后按 输入 获取计算结果的关键,请参见屏幕截图:
备注:在此公式中: A1:E11 是您要使用的数据范围, A1 是您要求和的具有特定背景颜色的单元格。
4。 然后,您可以重复上述公式以求和其他特定的彩色单元格。
在Microsoft Excel中使用Kutools for Excel对单元格颜色的单元格值进行计数或求和
在Microsoft Excel中,要根据特定的单元格颜色对单元格值进行计数或求和, Kutools for Excel“ 按颜色计数 实用程序可以帮助您尽快完成此任务。
Kutools for Excel : 带有300多个便捷的Excel加载项,可以在30天内免费试用. |
安装后 Kutools for Excel,请这样做:
1。 选择您要基于单元格颜色进行计数或求和的单元格,然后单击 Kutools Plus > 按颜色计数,请参见屏幕截图:
2。 在 按颜色计数 对话框,选择 标准格式 来自 上色方式 下拉列表,然后选择 背景 来自 计数类型 下拉菜单,请参见屏幕截图:
3。 然后点击 生成报告 按钮,并立即生成带有计算结果的新工作表,请参见屏幕截图:
备注:借助此强大功能,您还可以根据条件格式或字体颜色计算单元格值。
最佳办公效率工具
Kutools for Excel解决了您的大多数问题,并使您的生产率提高了80%
- 重用: 快速插入 复杂的公式,图表 以及您以前使用过的任何东西; 加密单元 带密码 创建邮件列表 并发送电子邮件...
- 超级公式栏 (轻松编辑多行文本和公式); 阅读版式 (轻松读取和编辑大量单元格); 粘贴到过滤范围...
- 合并单元格/行/列 不会丢失数据; 拆分单元格内容; 合并重复的行/列...防止细胞重复; 比较范围...
- 选择重复或唯一 行; 选择空白行 (所有单元格都是空的); 超级查找和模糊查找 在许多工作簿中; 随机选择...
- 确切的副本 多个单元格,无需更改公式参考; 自动创建参考 到多张纸; 插入项目符号,复选框等...
- 提取文字,添加文本,按位置删除, 删除空间; 创建和打印分页小计; 在单元格内容和注释之间转换...
- 超级滤镜 (将过滤方案保存并应用于其他工作表); 高级排序 按月/周/日,频率及更多; 特殊过滤器 用粗体,斜体...
- 结合工作簿和工作表; 根据关键列合并表; 将数据分割成多个工作表; 批量转换xls,xlsx和PDF...
- 超过300种强大功能。 支持Office / Excel 2007-2019和365。支持所有语言。 在您的企业或组织中轻松部署。 完整功能30天免费试用。 60天退款保证。

Office选项卡为Office带来了选项卡式界面,使您的工作更加轻松
- 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
- 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
- 每天将您的工作效率提高50%,并减少数百次鼠标单击!

You are guest
or post as a guest, but your post won't be published automatically.
-
To post as a guest, your comment is unpublished.· 1 months agoAutoupdate of the formulaA tip is to create a script that inserts a row in the range and then removes the same row again. And then set up a time-based trigger for the function =)
-
To post as a guest, your comment is unpublished.
-
To post as a guest, your comment is unpublished.Thank you so much for these scripts, Truly appreciated, it's not to get something that works out of the box.Question how would I go about modifying the values sum script to pull part of a cell, for exammple a cell that contains the following:909 & 1909
{1 of 2}
SM & M (3.5)How would I go about pulling the "3.5" in parenthesis and sum that numerical value coming out multiple cells of the same color. I know how to pull the value out in google sheets but can't figure out how to combine it with the cell color script.Thank you. -
To post as a guest, your comment is unpublished.Has anyone updated this for the Sum colored cells? i'm ok with copying and pasting the forumla for now but it would be nice if i highlighted more cells, it would add those cells to the total automatically. I'm a newb when it comes to reading code, i tried a few things based on all the comments but alas, i'm still new.
Thanks,
-
To post as a guest, your comment is unpublished.PLEASE SOMEONE COULD HELP ME ?
I WAS LOOKING FOR HOW TO SOLVE THAT A MONTH AT LEAST. I TRIED MANY THINGS, CHANGED COMMAS .... AND NOTHING SOLVE THAT...
------------- TypeError: Cannot call method "pop" of null. (line 5, file "SumByColor") -----------------
//*ORIGINAL - var countRangeAddress = activeformula.match(/\((.*)\,/).pop().trim();
//*I TRYING - var countRangeAddress = activeformula.match(/\((.*)\;/).pop().trim();
//*ORIGINAL - var colorRefAddress = activeformula.match(/\,(.*)\)/).pop().trim();
//*I TRYING - var colorRefAddress = activeformula.match(/\;(.*)\)/).pop().trim();
some charitable soul please help me, I don't have much programming ... #sad -
To post as a guest, your comment is unpublished.HI, Brian! Thank's for your effort to give us the updated code. The auto update feature doesn't works for me. It doesn't write a new random number in A1 automatically, s i have to do it manually to get refresh. Is it as planed or Do i miss something? Thanks for your reply.
-
To post as a guest, your comment is unpublished.
-
To post as a guest, your comment is unpublished.Hi all, I wanted to give you an updated version of how to count the number of cells that are a given color. This version solves a couple of issues:
1. The count not updating when the doc changes and
2. Errors from adding another parameter to the function
// Unsed third argument
function countColoredCells(countRange,colorRef,unUsed) {
var activeRg = SpreadsheetApp.getActiveRange();
var activeSht = SpreadsheetApp.getActiveSheet();
var activeformula = activeRg.getFormula();
// The regex matches the parentheses as an array, gets the arguments as a string,
// then splits the arguments on the comma into another array
var arrayOfArguments = activeformula.match(/\((.*)\)/).pop().trim().split(',');
// Get the first argument which is the range
var countRangeAddress = arrayOfArguments[0];
// Get the second argument, which is the reference color
var colorRefAddress = arrayOfArguments[1];
var backGrounds = activeSht.getRange(countRangeAddress).getBackgrounds();
var BackGround = activeSht.getRange(colorRefAddress).getBackground();
var countCells = 0;
for (var i = 0; i < backGrounds.length; i++)
for (var k = 0; k < backGrounds[i].length; k++)
if ( backGrounds[i][k] == BackGround )
countCells = countCells + 1;
return countCells;
};
// Writes a random number to A1
function onEdit(e)
{
SpreadsheetApp.getActiveSheet().getRange('A1').setValue(Math.random());
}
Usage: =countColoredCells(B2:E22,A28,$A$1)-
To post as a guest, your comment is unpublished.Do we paste all of this to replace the original code?
-
-
To post as a guest, your comment is unpublished.
-
To post as a guest, your comment is unpublished.
-
To post as a guest, your comment is unpublished.Hi everyone. I use this sumColoredCells function but it will NOT auto refresh 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 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.
-
To post as a guest, your comment is unpublished.
-
To post as a guest, your comment is unpublished.
-
-
To post as a guest, your comment is unpublished.
-
To post as a guest, your comment is unpublished.There were some errors on the code, like the range not being used at all and that the calculation was only made when the parameters were changed. With this version the calculation will be updated when there is an update on the sheet and the range has to be passed as a string, also some dummy cell has to be used to save a random value, you can put the color of the text just like the background to make it invisible:
=countColoredCells("B3:B145","B1",$A$1)
function countColoredCells(countRange, colorRef, unUsed) {
var backGrounds = SpreadsheetApp.getActiveSheet().getRange(countRange).getBackgrounds();
var backGround = SpreadsheetApp.getActiveSheet().getRange(colorRef).getBackground();
var countCells = 0;
for (var i = 0; i < backGrounds.length; ++i)
for (var k = 0; k < backGrounds[i].length; ++k)
if ( backGrounds[i][k] == backGround )
++countCells;
return countCells;
}
function onEdit(e)
{
SpreadsheetApp.getActiveSheet().getRange('A1').setValue(Math.random());
} -
To post as a guest, your comment is unpublished.A quick solution but not the best is to add a new parameter and never use it, something like this:
function sumColoredCells(sumRange,colorRef,test)
The test parameter is never used, this issue here is that the values only will be refresed if first of all you set the color and then change the value. -
To post as a guest, your comment is unpublished.
-
To post as a guest, your comment is unpublished.
-
To post as a guest, your comment is unpublished.
-
To post as a guest, your comment is unpublished.
-
To post as a guest, your comment is unpublished.Hello,
This function works, however, when i try to sum 2 instances of it in the same cell as in (in my case):
=sumColoredCells(A103:AW103, C171) + sumColoredCells(A138:AW138, C171)
It gives me an #Error - Range not found (line 6).
It doesn't work even if i use SUM() or ADD(). Any ideas?
Thanks in advance. -
To post as a guest, your comment is unpublished.The SumColoredCells Script works great. Do you know if there is a way to display (in a single cell) the sum of 2 colors? For example, if I wanted the sum of both the Yellow and Green Cells to display in a single cell, is that possible? I would think the formula would be:
=SUM(sumcoloredcells(A1:E11,A1)),(sumcoloredcells(A1:E11,A5)) ....But that results in an ERROR. Any ideas?-
To post as a guest, your comment is unpublished.Hello, Geoff,
May be there is no direct formula to solve your problem, but, you can sum the colored cells separately first, and then, sum the two calculated results as you need.
-
-
To post as a guest, your comment is unpublished.
-
To post as a guest, your comment is unpublished.Hello,
The formula does not work for me.
If I do this in spreadsheet only errors will occur. -
To post as a guest, your comment is unpublished.im trying to do something like this
=countColoredCells (H62:H,B2)countif("MBA13R15") -
To post as a guest, your comment is unpublished.Hi there,
Can I need some help with a similar formula to SUM total of number keywords matching and reflecting the number of colors.
What I what to achieve it determine the number of laptops which is a keyword but recognise the Available based on color of the cells? -
To post as a guest, your comment is unpublished.
-
To post as a guest, your comment is unpublished.does it auto update when new data is added?
-
To post as a guest, your comment is unpublished.
-
To post as a guest, your comment is unpublished.Does not seem like it
-
To post as a guest, your comment is unpublished.Did you ever figure out how to get this to auto update?
-
To post as a guest, your comment is unpublished.Did anyone find a way to auto update?
-
To post as a guest, your comment is unpublished.I need to figure out how to get it to auto update too!... Anyone find the answer?
-
To post as a guest, your comment is unpublished.Did anyone find a way to auto update?
-
-
-
-
-
-
To post as a guest, your comment is unpublished.
-
To post as a guest, your comment is unpublished.
-
To post as a guest, your comment is unpublished.
-
To post as a guest, your comment is unpublished.Hey, thanks for this script. I'm having a problem in the script for google sheets: TypeError: Cannot call method "pop" of null. (line 5, file "SumByColor"). Can you please help?, thanks
-
To post as a guest, your comment is unpublished.The problem maybe is your regional configuration, this script thinks you are separating parameters by ',', but it is not your case probably. In my case, I use ';', so I modified the script, changing the character in the lines
var countRangeAddress = activeformula.match(/\((.*)\;/).pop().trim();
var colorRefAddress = activeformula.match(/\;(.*)\)/).pop().trim(); -
To post as a guest, your comment is unpublished.Hello, I have the same problem, have you fix it?
-
To post as a guest, your comment is unpublished.
-
-
To post as a guest, your comment is unpublished.Your sumColoredCells() function is incorrect as it doesn't use sumRange
-
To post as a guest, your comment is unpublished.Thanks for the help with "Count cells based on color for google sheets". Limitation seems to be that when cell colors are changed the sheet does not refresh and totals are incorrect. I have to delete the code in the total cell and copy again from neighbour cell. Not ideal, but it works. Thanks again.
-
To post as a guest, your comment is unpublished.
-
To post as a guest, your comment is unpublished.Hi! Great script but I run into a problem when trying to edit the values within the coloured cells. The script does not refresh the values if I change the colour of the cell. I can only force it to refresh if i delete the contents of the cell, change the colour and then enter the value again.