【问题标题】:Counting number of cells w/ same background color of each row计算每行具有/相同背景颜色的单元格数
【发布时间】:2015-03-11 00:35:54
【问题描述】:

我正在使用 Google 电子表格,其中填充了来自 Google 表单的回复。现在,我有电子表格通过将单元格的值与每列的顶部单元格进行比较(使用条件格式)来自动为包含“错误”响应的单元格的背景着色。对于列 CF 到 CH,我想设置为“分数”部分。


使用上面的图片,我已经手动说明了我的最终目标是什么:
我希望能够为每一行计算具有特定背景颜色(在本例中为浅红色 1 [#de6666])的单元格,并在 CH 列中的相应单元格中输出数字.从那里,我计划使用该数字通过与总问题数进行比较来计算原始分数和百分比分数。



我做了一些搜索,希望找到一个已经回答过的类似问题,可能对我的情况有所帮助,并找到了这两个:(1) & (2)

现在,我对编码艺术一窍不通,如果我理解不正确或需要额外帮助,我想请您耐心等待我。 我的问题是,有没有办法调整这些以使其执行我想要执行的操作?还是需要全新的自定义函数?

【问题讨论】:

    标签: google-sheets


    【解决方案1】:

    我遇到了同样的问题,这是我的解决方案,符合我所看到的您的需求。 它并不完全通用,但只要您更改范围以满足您的需要,它就可以正常工作。 我尽力解释了让我感到困惑的部分,以便您可以在需要时对其进行调整。 祝你好运,希望这会有所帮助!

    function countbackgrounds(){
      var book = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = book.getActiveSheet();
      var lastRow = sheet.getLastRow();
      var cellColors = sheet.getRange(4, 1, lastRow-3, 57).getBackgrounds();//(startrow, startcolumn,numRows,numCols)
      /*since you want all rows and columns before CF:CH, since CF is column 58, you  want 57 columns.
      //to exclude your headers you want to start on a certain row, 
      //be mindful of which row number you are starting on, it looks to me like 4, 
      //so subtract 'startrow-1' from 'lastRow' so you code doesnt continue past your entries. */
      var colorY = "#de6666"; 
      var count = 0;
      for(var i = 0; i < cellColors.length; i++) {
        var thisRow = sheet.getRange(i+4, 60 ); //get output cell for this row in CH
        /*note: 'i' is an index number within the loop not the row number, if you are starting at another row 
        //you will need to change this, just add the 'startrow' number */
        for(var j = 0; j < cellColors[0].length ; j++) { 
          if(cellColors[i][j] == colorY) { 
            count = count + 1;
          };//end if statement
        };//end for each cell in a row 
        thisRow.setValue(count); 
        count = 0;//reset count to 0 before next row
      };//end for each row
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-23
      • 2017-01-05
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      相关资源
      最近更新 更多