在Google表格脚本中计算粗体单元格

时间:2017-12-12 21:23:56

标签: javascript google-apps-script google-sheets

所以,说实话,我并不是一个程序员,但是我已经设法通过计算细胞背景颜色来摸索我的方式,但是努力让它用于计算字体粗体的细胞。我在下面详细介绍了我的功能,它只使用粗体字体样式计算了6个单元格,但有13个单元格采用粗体字体样式。

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

}

3 个答案:

答案 0 :(得分:2)

你的if语句需要在括号内加上3“=”     if(cell_styles.isBold === true)

答案 1 :(得分:2)

getFontWeights()是将返回bold的方法。然后,计算它们的简单方法是展平数组,filter所有"bold"元素并获取已过滤列表的length

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

}

答案 2 :(得分:0)

这是您的代码更正。解释在评论中。

namespace QuestionTwo
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> names = new List<string>();

            Console.Write("Enter the directory path of the data files: ");
            string filename = Console.ReadLine();
            filename.Replace(@"\", @"\\");

            Console.Write("Enter the year: ");
            string year = Console.ReadLine();

            Console.Write("Enter the gender (M/F): ");
            string gender = Console.ReadLine();

            Console.Write("Enter the name: ");
             string name = Console.ReadLine();

            filename += "\\yob" + year + ".txt";
            string line = "";
            try
            {
                using (StreamReader fin = new StreamReader(filename))
                {
                    while((line = fin.ReadLine()) != null)
                    {
                        string[] parsed = line.Split(',');

                        if (parsed[1] == gender)
                        {
                                names.Add(parsed[0]);
                        }
                    }
                }

                for (int i = 0; i < names.Count; i++)
                {
                    if (names[i] == (name)) 
                    {
                        Console.WriteLine(name + " is ranked #" + i + " in year " + year);
                    }
                    else //if (names[i] != (name)) // it is a logic error somewhere here 
                    {
                        Console.WriteLine("The name " + name
                            + " is not ranked in year " + year);
                    }
                }
            }
            catch(Exception e  )
            {
                Console.WriteLine("The file at {0} could not be read."+ e,  filename);
            }

            for(int c = 0; c < names.Count; c++)
            {
                Console.WriteLine((c + 1) + ". " + names[c]);
            }
            Console.ReadKey();
        }
    }
}