【问题标题】:Google Spreadsheet cell reference to merged cell谷歌电子表格单元格对合并单元格的引用
【发布时间】:2017-01-31 20:57:15
【问题描述】:

我想引用一个合并单元格并从合并单元格范围内的任何单元格中获取合并单元格的值。

在 C1 中使用 =CONCATENATE(A2, " ",B2) 并将其向下拖动适用于:未合并的单元格和合并单元格的第一个单元格。它不适用于合并组中的后续单元格。这在 MS Excel 中可以按预期工作,但在 Google 电子表格中却不行,有没有办法实现这一点?

【问题讨论】:

    标签: google-sheets google-spreadsheet-api


    【解决方案1】:

    我编写了以下脚本,它仅适用于合并列(这是我想要的):

    /**
     * Returns the value at the top of a merged cell. If the merged cell is blank, not at the top of the sheet, and below another merged cell, the the function overflows into the above merged cell and returns incorrect input.
     *
     * @param {int} column Column number of the cell.
     * @param {int} row Row number of the cell.
     * @return The value shown in the merged cell.
     * @customfunction
     */
    function FIRST_IN_MERGED_COLUMN(column, row) {
      var sheet = SpreadsheetApp.getActiveSheet();
    
      var cell = sheet.getRange(row, column);
    
      if (!cell.isPartOfMerge())
        return cell.getValue();
      else if (cell.getValue() != "")
        return cell.getValue();
      else {
        var r = row;
        var newCell = cell, oldCell = cell;
        while (r > 1) {
          if (!(newCell = sheet.getRange(--r, column)).isPartOfMerge())
            return oldCell.getValue();
          if (newCell.getValue() != "")
            return newCell.getValue();
          oldCell = newCell;
        }
        return ""; // Blank merged cell at top of sheet
      }
    }
    

    不幸的是,如果一个合并列在另一合并列的正上方,并且底部合并列是空白的,那么脚本将使用顶部合并列的值。

    在下面使用,D列在B列显示公式,成功或错误情况在C列显示。

    【讨论】:

      【解决方案2】:

      我找到了更好的方法:

      // example inputs for cell position
      var row = 5;
      var column = 7;
      
      // Get the range that the cell belongs and then get the displayed value.
      var cell = sheet.getRange(row, column);
      
      var result;
      
      if (!cell.isPartOfMerge()) {
        result = cell.getValue();
      } else {
        // get the range the cell belongs
        var range = cell.getMergedRanges()[0];
      
        // getting the column and the row of the top left cell in the range
        var colRef = range.getColumn();
        var rowRef = range.getRow();
      
        // getting the value
        result = range.getDisplayValue();
      }
      

      【讨论】:

        【解决方案3】:

        这是我尝试整理所有其他答案的所有最佳属性。我已经用我能想到的各种方式对它进行了折磨测试,它似乎保持得很好。也就是说,欢迎报告错误。

        用法:用引号中的单元格引用调用它:=MERGED_CELL_VALUE("H19"),而不是=MERGED_CELL_VALUE(H19)。如果您在不带引号的情况下调用该函数,我无法使该函数始终引发信息性错误,因为 H19 的 contents 很容易是 A2 或其他内容。但它通常会以一种有用的方式抱怨。

        如果您希望能够对此使用点击拉式自动填充,有一个相当简单的解决方法。像这样使用CELL()=MERGED_CELL_VALUE(CELL("address", H19))。现在,H19 是裸露的,所以如果你移动它就会更新它。这个技巧也适用于H$19 等。

        /**
         * Returns the value at the top of a merged cell.
         *
         * Inspired by the code at:
         * https://stackoverflow.com/questions/41967357/google-spreadsheet-cell-reference-to-merged-cell
         *
         * @param {str} target The targeted cell, as a string in A1 format.
         * @return The value shown in the merged cell.
         * @customfunction
         */
        function MERGED_CELL_VALUE(target) {
          var sheet = SpreadsheetApp.getActiveSheet();
          try {
            var targetRange = sheet.getRange(target);
          } catch(e) {
            if (target == "") {
              throw "Empty target specified.  Did you forget to enclose the targeted cell in quotes?"
            }
            else {
              throw "Invalid target '" + target + "'.  Did you forget to enclose the cell ID in quotes?";
            }
          }
        
          if (!(targetRange.getNumColumns() == targetRange.getNumRows() == 1)) {
            throw "This function does not work on ranges (e.g. 'A1:B3'), only individual cells.";
          }
        
          var cell = targetRange.getCell(1, 1); // n.b. Range.getCell() is 1-indexed because FML.
        
          if (!cell.isPartOfMerge()) {
            // If the cell is not part of a merge, return the cell's value.
            return cell.getValue();
          }
          else if (cell.getValue() != "") { // n.b. this is a special case for getValue(); empty returns "".
            // If the cell has a value, even if it's merged (!!??), return the value.
            return cell.getValue();
          }
          else {
            /*
             * This might turn out to be a bug; we pick getMergedRanges()[0].  Hopefully that matches how GSheets's logic works.
             * There is a getDisplayValue(), but that always returns a String, which is unlikely to fit our goal in
             * all cases.
             */
            return cell.getMergedRanges()[0].getCell(1, 1).getValue();
          }
        }
        

        【讨论】:

          【解决方案4】:

          我结合了这个函数的其他答案:

          function MERGED_CELL_VALUE(column, row) {
            var sheet = SpreadsheetApp.getActiveSheet();
          
            var cell = sheet.getRange(row, column);
          
            if (!cell.isPartOfMerge())
              return cell.getValue();
            else if (cell.getValue() != "")
              return cell.getValue();
            else {
              var range = cell.getMergedRanges()[0];
              return range.getDisplayValue();
            }
          }
          

          【讨论】:

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