0
votes

I am trying to copy the contents of a GDoc into another GDoc. This works pretty well for all the different element types. Including a table (Enum DocumentApp.ElementType.TABLE). However, if the table contains an inline image (Enum DocumentApp.ElementType.INLINE_IMAGE) the image is not copied correctly.

Here is a link to the source GDoc example. https://docs.google.com/document/d/14kXjC0CTkEgmD7Ttv0YKL9ubfDRHbL1hCVOqOiyiEDU/edit#. Find the row in the table with the New Zealand flag. The flag is not copied correctly into the target GDoc's new table.

I am simply finding the table object in the source document (above) and using Body::insertTable(childIndex,table) to insert it into the target GDoc's Body. Most other elements in the table copy OK. Including an embedded Google Drawing. But not the inline image.

3

3 Answers

1
votes

i had a similar problem and in my case, the GLYPH_TYPE of numbered lists got lost, too. But i detected that the images will be fine copied if the CellChildren are copied one by one.

So i solved my problem by copying the new table at whole and then replacing the contents of each dstTable cell one by one from the original table.

This works fine now even with nested tables, since the function calls itself, if another table within the table is detected. And the problem with the lost ListItem attribute is solved, too by setting the attributes additionally after the insert.

Here is my well working code segment:

Firstly i detect the table and insert it in the dstBody ...

...
dstChild = srcChild.copy();
switch( dstChild.getType() ) {
  case DocumentApp.ElementType.PARAGRAPH:
    ...
  case DocumentApp.ElementType.LIST_ITEM:
    ...
  case DocumentApp.ElementType.TABLE:
    var newTable =
    dstBody.insertTable( dstBody.getNumChildren()-1, dstChild );
    copyTableCellByCell( dstChild, newTable );
    break;
....
}

And this is the maybe recursive function that replaces each cell by first clearing it and copying the content from the original table:

function copyTableCellByCell( srcTable, dstTable ) {
  var numRows = dstTable.getNumRows();
  var dstRow, numCells, dstCell, actCellIndex;
  for ( var actRowIndex = 0; actRowIndex < numRows; actRowIndex++ ) {
    dstRow = dstTable.getRow( actRowIndex );
    numCells = dstRow.getNumCells();
    for ( actCellIndex = 0; actCellIndex < numCells; actCellIndex++ ) {
      dstCell = dstRow.getCell( actCellIndex );
      dstCell.clear();
      var srcCell = srcTable.getCell( actRowIndex, actCellIndex );
      var numCellChildren = srcCell.getNumChildren();
      for ( var y = 0; y < numCellChildren; y++ ) {
        var cellChild = srcCell.getChild( y );
        var childCopy = cellChild.copy();
        switch( childCopy.getType() ) {
          case DocumentApp.ElementType.PARAGRAPH:
            dstCell.insertParagraph( y, childCopy );
            break;
          case DocumentApp.ElementType.LIST_ITEM:
            // that the GLYPH_TYPE doesn't get lost
            var atts    = childCopy.getAttributes();
            var 
            newListItem = dstCell.insertListItem( y, childCopy );
            newListItem.setAttributes( atts );
            break;
          case DocumentApp.ElementType.TABLE:
            var newTable =
            dstCell.insertTable( y, childCopy );
            copyTableCellByCell( cellChild, newTable );
            break;
        }
      }
      // remove the very first empty paragraph in the cell
      while ( (y = dstCell.getNumChildren()) > numCellChildren ) {
        dstCell.getChild( y - 1 ).removeFromParent();
      }
    }
  }
}

This could be fine tuned, of course. You could just search and pick out the inline images, if you want the servers have to do less work.

I hope, this helps in any way. Thank you very much for your attention,

Richard

0
votes

Methods that you can use to copy an Inline Image can be found in Class InlineImage and there are even additional methods if an inline image was contained within a ListItem or Paragraph.

0
votes

I found a none coding solution to this bug.

Insert the image as a "drawing" in the source document.

  1. Click the table cell where images needs to be inserted.
  2. Click "insert" on the document's menu.
  3. Click "insert drawing".
  4. In drawing pane add image to be inserted.
  5. Save and close.

The result will be an image in the table that gets copied perfectly along with the type TABLE.