Question

I've writen this function (thanks, @Mogsdad) to merge cells in a table in a text google document, like this:

enter image description here

function onOpen() {
  // Add a menu with some items, some separators, and a sub-menu.
  DocumentApp.getUi().createMenu('Sample')
      .addItem('merge cells of a table', 'mergeCells')          
      .addToUi();
}



function mergeCells() {
  var body = DocumentApp.getActiveDocument().getBody();
  for (var p= 0; p< body.getNumChildren(); p++) {
    var child = body.getChild(p);
    if (child.getType() == DocumentApp.ElementType.TABLE){
      // Assume we've already located our table
      var table = child;
      var tableRow = table.getChild(2); // gets third row
      var tableCell = tableRow.getChild(1); // gets second cell in row
      tableCell.merge(); // Merges seconde cell with first cell.      
      }
  }
}

But when I run the code, I got this weird result (very different of the expected, with the merged cell with the same dimensions of the table):

enter image description here

Is there a way to fix it? (merged cell with the same dimensions)

Was it helpful?

Solution

[edit to address updates in Google Docs]

This currently not possible. You can know if a cell is merged by looking calling getColSpan and getRowSpan but there are no setter methods.

Please star the following issue to be notified by updates regarding this.

The merge function you found is not specific to table cells, it is there to merge any element with a previous sibling of the same type, joining their content.


[original answer]

If you were expecting to have a merged cell that, like what you can do in a spreadsheet, that is not possible. Simply because that's not possible in Google Documents (at least not yet). Therefore the API cannot do this (it can only do things that are also possible manually).

This merge function is not specific to table cells as you probably imagined. It is working as designed.

OTHER TIPS

You can do this by a workaround. Add a drawing and add a table in this drawing document. In this document te option 'merge cell's' is a possibility if you select 2 cells and press the right mouse button. See this youtube video for a tutorial

Use the Advanced Documents Service, batchUpdate and mergeTableCells.

function mergeCells() {
  const documentId = 'DOCUMENT_ID';
  const resource = {
    requests: [
      {
        "mergeTableCells": {
          "tableRange": {
            "tableCellLocation": {
              "tableStartLocation": {
                "index": 2
              },
              "rowIndex": 0,
              "columnIndex": 0
            },
            "rowSpan": 1,
            "columnSpan": 2
          }
        }
      }
    ]
  }
  Docs.Documents.batchUpdate(resource, documentId);
}

Resources

One workaround is to embed tables within tables instead of merging cells. That way you can still programmatically add or remove cells/rows without ruining the tables. If you set the cell padding to 0, any stray paragraphs to 1pt font and remove any cell borders and you can achieve almost the same effect without any merged cells.

For example, I've got a cell with 4 columns, where I want the last column to be merged as as a single cell. I also want to be able to add or remove rows with Apps Script.

Table embedded within a table:
Table embedded within a table

This way I can add or remove rows from the embedded table on the left and the right cell will remain "merged". You will have trouble getting the cell borders to line up, but if you can do without them you can still get it looking nice, like this:

And without borders:
And without borders

Here is how to solve the merged cell in tables of a MS document when converting to a Google document: The idea is to go back to the MS word document and remove the merged cells and then copy and paste it or convert MS word to Google document. In this way we can get easy conversion of tables in MS documents to Google documents. But, within Google documents the cell can't be merged.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top