How to merge the two cells of a table in a Google text Document without this weird result?

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)


[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.


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