Script: SUM values if font is bold – Google Drive Spreadsheet

I was working on some calculations using Google Drive Spreadsheet.
Than I found out, that I would need SUM values only from cells, which has bold font… Than I found out, that there is no simple way, how to do that with basic functions included in Spreadsheet application. That was the moment, when I found out, that there is option create a custom fuction by my own needs using Google Apps Script!

I checked existing gallery of the scripts to find out a syntax of the Google Apps Script. Than I checked their amazing documentation and wrote my own script. Here is its code:

function sumWhereIsFontWeight(rangeSpecification) {

var sheet = SpreadsheetApp.getActiveSpreadsheet();
var range = sheet.getRange(rangeSpecification);

var x = 0;

for (var i = 1; i <= range.getNumRows(); i++) {
for (var j = 1; j <= range.getNumColumns(); j++) {

var cell = range.getCell(i, j);

if(cell.getFontWeight() == 'bold')
x += parseFloat(cell.getValue());
}
}

return x;
}

I also prepared function for the opposite situation, and called it sumWhereIsNotFontWeight, here is its code:

function sumWhereIsNotFontWeight(rangeSpecification) {

var sheet = SpreadsheetApp.getActiveSpreadsheet();
var range = sheet.getRange(rangeSpecification);

var x = 0;

for (var i = 1; i <= range.getNumRows(); i++) {
for (var j = 1; j <= range.getNumColumns(); j++) {

var cell = range.getCell(i, j);

if(cell.getFontWeight() != 'bold')
x += parseFloat(cell.getValue());
}
}

return x;
}

 

UPDATE (2013/11/15):

I have created new article where I explain more in detail, how to implement this script into Google Drive Spreadsheet, check it here.

You may also like