4
votes

I'm trying to learn scripting in Google Spreadsheet, and I have gotten some simple scripts to work, but this one is a real pain.

I want to make a script that uses the onEdit() function to update a specific cell to show the sum of all bold values in the spreadsheet.

Fx:

1 2 3

4

Then the cell would have a value of (3+4) 7.

Hope it makes sense!

3
Is it A1=[1 2 3] or A1=1, B1=2, C1=3? I guess the later, because it's not possible to make an individual character, in a cell, bold (just tried)Jacob Jan Tuinstra

3 Answers

7
votes

It's a bit late, but it's worth an answer and I've been working on a similar question.

The formula to use is:

=sumIfBold(A1:B4,COLUMN(A1), ROW(A1))

The script is:

/**
 * Sums cell values in a range if they are bold. The use of startcol and startrow
 * is to enable the formula to be copied / dragged relatively in the spreadsheet.
 * 
 * @param  {Array.Array} range    Values of the desired range
 * @param  {int} startcol The column of the range
 * @param  {int} startrow The first row of the range
 * 
 * @return {int}          Sum of all cell values matching the condition
 */
function sumIfBold(range, startcol, startrow){
  // convert from int to ALPHANUMERIC 
  // - thanks to Daniel at http://stackoverflow.com/a/3145054/2828136
  var start_col_id = String.fromCharCode(64 + startcol);
  var end_col_id = String.fromCharCode(64 + startcol + range[0].length -1);
  var endrow = startrow + range.length - 1

  // build the range string, then get the font weights
  var range_string = start_col_id + startrow + ":" + end_col_id + endrow
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var getWeights = ss.getRange(range_string).getFontWeights();

  var x = 0;
  var value;
  for(var i = 0; i < range.length; i++) {
    for(var j = 0; j < range[0].length; j++) {
      if(getWeights[i][j].toString() == "bold") {
        value = range[i][j];
        if (!isNaN(value)){
          x += value;
        }
      }
    }
  }
  return x;
}
0
votes

I want to add one tiny point to Tom's excellent answer. It currently returns a string. To return a number, change return x; to return x*1;

Also, for anyone looking to convert this to italics instead of bold, here's the code:

/**
 * Sums cell values in a range if they are italic. The use of startcol and startrow
 * is to enable the formula to be copied / dragged relatively in the spreadsheet.
 * 
 * @param  {Array.Array} range    Values of the desired range
 * @param  {int} startcol The column of the range
 * @param  {int} startrow The first row of the range
 * 
 * @return {int}          Sum of all cell values matching the condition
 */
function sumIfItalic(range, startcol, startrow){
  // convert from int to ALPHANUMERIC 
  // - thanks to Daniel at http://stackoverflow.com/a/3145054/2828136
  var start_col_id = String.fromCharCode(64 + startcol);
  var end_col_id = String.fromCharCode(64 + startcol + range[0].length -1);
  var endrow = startrow + range.length - 1

  // build the range string, then get the font styles
  var range_string = start_col_id + startrow + ":" + end_col_id + endrow
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var getStyles = ss.getRange(range_string).getFontStyles();

  var x = 0;
  var value;
  for(var i = 0; i < range.length; i++) {
    for(var j = 0; j < range[0].length; j++) {
      if(getStyles[i][j].toString() == "italic") {
        value = range[i][j];
        if (!isNaN(value)){
          x += value;
        }
      }
    }
  }
  return x*1;
}
0
votes

Those answers were great, but for me it breaks when pass the Z column (column number 26 to number 27, when we have two letters).

Added a small change to fix that (on the italic version)

    /**
     * Sums cell values in a range if they are italic. The use of startcol and startrow
     * is to enable the formula to be copied / dragged relatively in the spreadsheet.
     * 
     * @param  {Array.Array} range    Values of the desired range
     * @param  {int} startcol The column of the range
     * @param  {int} startrow The first row of the range
     * 
     * @return {int}          Sum of all cell values matching the condition
     * @customfunction
     */
    function sumIfItalic(range, startcol, startrow){
      // convert from int to ALPHANUMERIC 
      // - thanks to Daniel at http://stackoverflow.com/a/3145054/2828136
      var secondColChar = parseInt(startcol / 27); //26 is the number of col Z, 26 letters, works for only for two char
  
      var start_col_id = ""
      var end_col_id = ""
  
      if(secondColChar > 0) {
        start_col_id += String.fromCharCode(64 + (secondColChar));
        end_col_id += String.fromCharCode(64 + secondColChar + range[0].length -1);
      }
  
      start_col_id += String.fromCharCode(64 + (startcol % 27 + (secondColChar * 1))  );
      end_col_id += String.fromCharCode(64 + (startcol % 27) + (secondColChar * 1) + range[0].length -1);
  
      var endrow = startrow + range.length - 1


      // build the range string, then get the font styles
      var range_string = start_col_id + startrow + ":" + end_col_id + endrow
    
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var getStyles = ss.getRange(range_string).getFontStyles();
    
      var x = 0;
      var value;
      for(var i = 0; i < range.length; i++) {
        for(var j = 0; j < range[0].length; j++) {
          if(getStyles[i][j].toString() == "italic") {
            value = range[i][j];
            if (!isNaN(value)){
              x += value;
            }```
          }
        }
      }
      return x*1;
    }

tested only in my case, though...