Google电子表格:所有粗体单元格的总和

Den*_*sen 4 google-sheets google-apps-script

我正在尝试在Google Spreadsheet中学习脚本,我已经有了一些简单的脚本可以工作,但这个是一个真正的痛苦.

我想制作一个使用onEdit()函数更新特定单元格的脚本,以显示电子表格中所有粗体值的总和.

外汇:

1 2 3

4

然后该单元格的值为(3 + 4)7.

希望它有意义!

Tom*_*ood 6

这有点晚了,但值得回答,我一直在研究类似的问题.

使用的公式是:

=sumIfBold(A1:B4,COLUMN(A1), ROW(A1))
Run Code Online (Sandbox Code Playgroud)

该脚本是:

/**
 * 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;
}
Run Code Online (Sandbox Code Playgroud)