计算Google表格脚本中的粗体单元格 [英] Count Bold Cells in Google Sheets Script

查看:55
本文介绍了计算Google表格脚本中的粗体单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说实话,我并不是一个编码员,但是我设法弄乱了计算单元格背景颜色的方式,但是努力使它能够用于计算粗体字体的单元格.我在下面详细介绍了我的函数,该函数仅计数6个具有粗体字体的单元格,但是有13个具有粗体字体的单元格.

So, I'm not much of a coder to be honest, but I've managed to fumble my way through counting cell background colour, but struggling to get it to work for counting cells where the font is bold. I've detailed my function below, which counts only 6 cells with a bold font style, but there is 13 cells with a bold font style.

function countboldcells() {
  var book = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = book.getActiveSheet();
  var range_input = sheet.getRange("E2:S7");
  var range_output = sheet.getRange("G14");
  var cell_styles = range_input.getFontStyle();
  var count = 0;

 for(var r = 0; r < cell_styles.length; r++) {
    for(var c = 0; c < cell_styles[0].length; c++) {
      if(cell_styles.isBold = true) {
        count = count + 1;
      }
    }
     range_output.setValue(count);
  }

}

推荐答案

元素并获取

getFontWeights() is the method that will return bold or not. Then the easy way to count them would be to flatten the array, filter all of the "bold" elements and get the length of the filtered list

function countboldcells() {
  var book = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = book.getActiveSheet();
  var range_input = sheet.getRange("E2:S7");
  var range_output = sheet.getRange("G14");

  // Get the fontWeights of the range and flatten the array
  var cell_styles = range_input.getFontWeights().join().split(",");

  // Filter out any value that is not "bold"
  var filter_bold = cell_styles.filter(function (e) { return e == "bold" });

  // Set the count
  range_output.setValue(filter_bold.length);

}

这篇关于计算Google表格脚本中的粗体单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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