跳到主要内容

如何根据Google工作表中的单元格颜色对单元格进行计数或求和?

根据特定的单元格背景色对单元格值进行计数或求和,以获得结果,如以下屏幕截图所示。 本文,我将讨论如何在Google表格和Microsoft Excel中解决此任务。

使用Google工作表中的脚本根据单元格颜色计算单元格值

在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 加 > 按颜色计数,请参见屏幕截图:

2。 在 按颜色计数 对话框,选择 标准格式 来自 上色方式 下拉列表,然后选择 背景 来自 计数类型 下拉菜单,请参见屏幕截图:

3。 然后点击 生成报告 按钮,并立即生成带有计算结果的新工作表,请参见屏幕截图:

备注:借助此强大功能,您还可以根据条件格式或字体颜色计算单元格值。

单击立即下载并免费试用 Kutools for Excel!

最佳办公生产力工具

🤖 Kutools 人工智能助手:基于以下内容彻底改变数据分析: 智能执行   |  生成代码  |  创建自定义公式  |  分析数据并生成图表  |  调用 Kutools 函数...
热门特色: 查找、突出显示或识别重复项   |  删除空白行   |  合并列或单元格而不丢失数据   |   不使用公式进行四舍五入 ...
超级查询: 多条件VLookup    多值VLookup  |   跨多个工作表的 VLookup   |   模糊查询 ....
高级下拉列表: 快速创建下拉列表   |  依赖下拉列表   |  多选下拉列表 ....
列管理器: 添加特定数量的列  |  移动列  |  切换隐藏列的可见性状态  |  比较范围和列 ...
特色功能: 网格焦点   |  设计图   |   大方程式酒吧    工作簿和工作表管理器   |  资源库 (自动文本)   |  日期选择器   |  合并工作表   |  加密/解密单元格    按列表发送电子邮件   |  超级筛选   |   特殊过滤器 (过滤粗体/斜体/删除线...)...
前 15 个工具集12 文本 工具 (添加文本, 删除字符,...)   |   50+ 图表 类型 (甘特图,...)   |   40+ 实用 公式 (根据生日计算年龄,...)   |   19 插入 工具 (插入二维码, 从路径插入图片,...)   |   12 转化 工具 (小写金额转大写, 货币兑换,...)   |   7 合并与拆分 工具 (高级组合行, 分裂细胞,...)   |   ... 和更多

使用 Kutools for Excel 增强您的 Excel 技能,体验前所未有的效率。 Kutools for Excel 提供了 300 多种高级功能来提高生产力并节省时间。  单击此处获取您最需要的功能...

产品描述


Office Tab 为 Office 带来选项卡式界面,让您的工作更加轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
Comments (79)
Rated 5 out of 5 · 1 ratings
This comment was minimized by the moderator on the site
Hey I just wanted to thank you for this guide. It totally worked! Awesome! 👍
This comment was minimized by the moderator on the site
Hii, i need help!
Did somebody have the right script?
if u have it, can u pls send me?
thanks
This comment was minimized by the moderator on the site
i need the auto update for the sumcoloredcells formula
do you know how can i have it?
This comment was minimized by the moderator on the site
questo funziona

// restituisce la somma dei valori in un range in base al colore di sfondo della cella
// uso: =SommaSeSfondo(range;cella colore di riferimento )
// =SommaSeSfondo(B1:B8;E1)

function SommaSeSfondo(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;
};
This comment was minimized by the moderator on the site
Hi Samal,

You have to use the formula:

=SommeCouleurs(A:A;A1) => A:A : columns that contain the colors to count and A1 : The cell that contains the example of the color.
This comment was minimized by the moderator on the site
'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?
This comment was minimized by the moderator on the site
'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?
This comment was minimized by the moderator on the site
'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?
This comment was minimized by the moderator on the site
Bonjour a tous,

J'utilise Google Sheet en version Française et voici le script le plus fonctionnel que j'ai trouvé:

function SommeCouleurs(plage,couleur) {
var activeRange = SpreadsheetApp.getActiveRange();
var activeSheet = activeRange.getSheet();
var formule = activeRange.getFormula();


var laplage = formule.match(/\((.*)\;/).pop();
var range = activeSheet.getRange(laplage);
var bg = range.getBackgrounds();
var values = range.getValues();

var lacouleur = formule.match(/\;(.*)\)/).pop();
var colorCell = activeSheet.getRange(lacouleur);
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;

};


function CompteCouleurs(plage,couleur) {
var activeRange = SpreadsheetApp.getActiveRange();
var activeSheet = activeRange.getSheet();
var formule = activeRange.getFormula();


var laplage = formule.match(/\((.*)\;/).pop();
var range = activeSheet.getRange(laplage);
var bg = range.getBackgrounds();
var values = range.getValues();

var lacouleur = formule.match(/\;(.*)\)/).pop();
var colorCell = activeSheet.getRange(lacouleur);
var color = colorCell.getBackground();

var count = 0;


for(var i=0;i<bg.length;i++)
for(var j=0;j<bg[0].length;j++)
if( bg[i][j] == color )
count=count+1;
return count;
};

Formule a utiliser:

=CompteCouleurs(A2:A;A1)

Il s'actualise automatiquement.
En espérant que ca vous aidera
This comment was minimized by the moderator on the site
Добрый день!
Проверил, автоматически не пересчитывает, если изменить цвет в диапазоне ((
This comment was minimized by the moderator on the site
非常感謝大大分享~很實用我是用Google 試算表編輯
請問程式碼能教怎麼加總指定的文字顏色的數字總和嗎? (把 儲存格顏色 改為 文字顏色) 拜託~~
This comment was minimized by the moderator on the site
Hi, I have set it in google sheets as You have described, but it showed me error "Type error: Cannot read property 'pop' of null". PLS can you help me?
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations