Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Spreadsheet cell reference to merged cell

I would like to reference a merged cell and get the value of the merge cell from any of the cells that fall within the merged cell's range.

Using =CONCATENATE(A2, " ",B2) in C1 and dragging it down works for: un-merged cells, and the first cell of a merged cell. It does not work for subsequent cells in a merged group. This works as expected in MS Excel, but not Google spreadsheets, is there a way to achieve this?

Spreadsheet example

like image 865
Morgoth Avatar asked Jan 31 '17 20:01

Morgoth


People also ask

Can you reference a merged cell?

Right-click the merged cell B1:D1, select "paste special -> formulas" You should see the merged cell being 0. Type Col1 in the merged cell. You should now see all B2, C2, D2 to be Col1, i.e. now you can reference the merged cell as you expect it to be.

What is the cell reference of a merged cell?

In excel, merged cells are referred to as the address of the top left-most cell that was merged. Say you merge A2:A8 - to reference this cell just use A2. Similarly Merged A2:C8 would be A2.

Can you do formulas with merged cells?

On the Home tab, click Merge & Center. Right-click the A1 cell, and then click Copy. Right-click the merged cell, and then click Paste Special. In the Paste Special dialog box, click Formulas, and then click OK.

How do you link merged cells?

Click the first cell and press Shift while you click the last cell in the range you want to merge. Important: Make sure only one of the cells in the range has data. Click Home > Merge & Center. If Merge & Center is dimmed, make sure you're not editing a cell or the cells you want to merge aren't inside a table.


1 Answers

I wrote the following script, which works only for merged columns (which is what I wanted):

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

Unfortunately, if one merged column is directly above another merged column, and the bottom merged column is blank, then the script will use the value of the top merged column.

It is used below, column D shows the formula in column B, and success or error case are shown in column C.

enter image description here

like image 116
Morgoth Avatar answered Sep 23 '22 04:09

Morgoth