[英]Count Bold Cells in Google Sheets Script
所以,說實話,我並不是一個程序員,但是我已經設法通過計算細胞背景顏色來摸索我的方式,但努力讓它用於計算字體粗體的細胞。 我在下面詳細介紹了我的功能,它只使用粗體字體樣式計算了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);
}
}
你的if語句需要在括號內有3“=”
if(cell_styles.isBold === true)
getFontWeights()是返回bold
的方法。 然后計算它們的簡單方法是展平數組, 過濾所有"bold"
元素並獲取過濾列表的長度
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);
}
這是您的代碼更正。 說明在評論中。
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.getFontWeights(); // getFontStyle can only return 'italic' or 'normal'
var count = 0;
for(var r = 0; r < cell_styles.length; r++) {
for(var c = 0; c < cell_styles[0].length; c++) { // isBold is a method for Google Documents only (not sheets)
if(cell_styles[r][c] === "bold") { // you need at least two '=' signs // also include the index of cell_styles
count = count + 1; // count += 1 would also work
}
}
}
range_output.setValue(count); // make sure you setValue only when booth loops are done.
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.