首页 > 解决方案 > 计算合并单元格时,按背景颜色计算行数失败——google sheet

问题描述

嘿伙计们,我在 google sheet excel 中,我想计算有多少行有基于背景颜色的合并单元格。

我使用 ScriptEditor 来解决它。我有下一个代码:

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;
};

有效,</p>

但如果我合并一些单元格,它就不起作用。

这个例子

我想要的是按背景颜色计算每个合并单元格块的行数。

标签: javascriptgoogle-sheetsgoogle-sheets-apigoogle-sheets-formula

解决方案


推荐阅读