如何根据有色单元格更新单元格的值? [英] How to update a cell's value according to colored cells?

查看:95
本文介绍了如何根据有色单元格更新单元格的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Google表格电子表格中,我有一些绿色和红色的单元格.我想要一个具有这些绿色/红色单元格计数的单元格.

In a Google Sheets spreadsheet, I have some cells green and red-colored. I want a cell having the count of those green/red colored cells.

是否有公式可以执行此操作或为此提供任何自定义代码?

Is there a formula to do this thing or any custom code for this?

推荐答案

Google Apps脚本应该可以帮助您入门(请参阅电子表格示例):

This Google Apps Script should get you started (see example spreadsheet):

function countRedBackgrounds() {
  var COUNT_RED = 0;
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Cell colors");
  var dataRange = sheet.getDataRange();
  for (var i = 1; i<dataRange.getNumRows(); i++) {
    for (var j = 1; j<dataRange.getNumColumns(); j++) {
      if (dataRange.getCell(i,j).getBackground() == "#ff0000")
        COUNT_RED++;
    }
  }
  dataRange.getCell(1,1).setValue(COUNT_RED);
}

这篇关于如何根据有色单元格更新单元格的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆