Count the cells with same color in google spreadsheet

后端 未结 5 469
夕颜
夕颜 2021-01-03 21:15

I\'m trying count the number of cells with the same background color and put the result in other cell with a script in google apps script, but I can\'t do it. I have the nex

5条回答
  •  天涯浪人
    2021-01-03 21:58

    You can use this working script:

    /**
    * @param {range} countRange Range to be evaluated
    * @param {range} colorRef Cell with background color to be searched for in countRange
    * @return {number}
    * @customfunction
    */
    
    function countColoredCells(countRange,colorRef) {
      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 count = 0;
    
      for(var i=0;i

    Then call this function in your google sheets:

    =countColoredCells(D5:D123,Z11)
    

提交回复
热议问题