Count if Text is Bold in Google Sheet


Is there a way we can count or display the sum of bold text in the spreadsheet?


Solution:

Running a script in Google Apps Script.


Cons:

    - when the text is changed or updated the sum or count does not update

    - need to run the script every time when you edit the spreadsheet


function countboldcells() {
var book = SpreadsheetApp.getActiveSpreadsheet();
var sheet = book.getActiveSheet();
var range_input = sheet.getRange("A2:A18");
var range_output = sheet.getRange("A19");
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.
}

Change the code (text in green) to the range of the text.

var range_input = sheet.getRange("A2:A18"); 

Change the code (text in green ) to the cell where the sum will be displayed.

var range_output = sheet.getRange("A19");

No comments:

Powered by Blogger.