Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
138 views
in Technique[技术] by (71.8m points)

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...