【问题标题】:Google spreadsheet: Sum of all bold cells谷歌电子表格:所有粗体单元格的总和
【发布时间】:2012-12-24 07:08:30
【问题描述】:

我正在尝试在 Google 电子表格中学习脚本,并且我已经获得了一些简单的脚本来工作,但是这个真的很痛苦。

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

效果:

1 2 3

4

那么单元格的值为 (3+4) 7。

希望它有意义!

【问题讨论】:

  • 是 A1=[1 2 3] 还是 A1=1, B1=2, C1=3?我猜是后者,因为不可能在单元格中使单个字符变为粗体(刚刚尝试过)

标签: google-apps-script google-sheets


【解决方案1】:

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

要使用的公式是:

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

脚本是:

/**
 * 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;
}

【讨论】:

    【解决方案2】:

    我想为汤姆的出色回答补充一点。它当前返回一个字符串。要返回一个数字,请将return x; 更改为return x*1;

    此外,对于任何希望将其转换为斜体而不是粗体的人,以下是代码:

    /**
     * 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;
    }
    

    【讨论】:

      【解决方案3】:

      这些答案很棒,但对我来说,当通过 Z 列时它会中断(第 26 列到第 27 列,当我们有两个字母时)。

      添加了一个小改动来解决这个问题(在斜体版本上)

          /**
           * 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;
          }
      

      仅在我的情况下进行了测试...

      【讨论】:

        【解决方案4】:

        感谢这段代码,但不幸的是它返回了一个字符串。我在底部的 FOR 循环块中做了一个小更新,并删除了一些虚假的反引号。

            /**
         * 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 sumIf_Italic(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*1;
                }
              }
            }
          }
          return x*1;
        }
        

        【讨论】:

          猜你喜欢
          • 2013-09-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多