I would like to copy the content of one google doc to another. The content includes text, tables and images.
My code copies the text and the tables. However, whatever is contained in table cells is not copied.
I made a simplified version of the code and single document accessible here: https://docs.google.com/document/d/1hcQzBuMA6E15u8VtW2lWGL7XCcU3qVsDhn-5jiznQP4/edit?usp=sharing.
The code simply copy-pastes the content of the google document which includes a table containing a table/images. The same problem occurs. The content of the cell is not copied. see screenshot
Here is the simplified version of the code:
function test() {
// Make Copy of template file
doc = DocumentApp.getActiveDocument();
body =doc.getBody();
/// Copy elements from source file to target file one bich one
var totalElements = body.getNumChildren();
var types = new Array;
for( var iel = 0; iel < totalElements; iel++ ) {
current_body = doc.getBody();
var element = body.getChild(iel).copy();
var type = element.getType();
types.push(type);
switch(type){
case DocumentApp.ElementType.PARAGRAPH:
body.appendParagraph(element);
break;
case DocumentApp.ElementType.TABLE:
var newTable =body.insertTable( body.getNumChildren()-1, element );
CopyTableCellByCell( element, newTable );
break;
case DocumentApp.ElementType.LIST_ITEM:
body.appendListItem(element);
break;
case DocumentApp.ElementType.INLINE_IMAGE:
body.appendImage(element);
break;
}
}
doc.saveAndClose();
}
// recursive function that replaces each cell by first clearing it and Copying the content from the original table
function CopyTableCellByCell( srcTable, dstTable ) {
var numRows = srcTable.getNumRows();
var srcRow, numCells, dstCell, icell;
var types = new Array;
for ( var irow = 0; irow < numRows; irow++ ) {// EACH ROW
srcRow = srcTable.getRow( irow );
dstRow = dstTable.getRow( irow );
numCells = srcRow.getNumCells();
for ( icell = 0; icell < numCells; icell++ ) { // EACH CELL
dstCell = dstRow.getCell( icell );
dstCell.clear();
var srcCell = srcTable.getCell( irow, icell );
var numCellChildren = srcCell.getNumChildren(); // ==> outputs 1 paragraph child instead of a paragraph and a table.
for ( var ich = 0; ich < numCellChildren; ich++ ) { // EACH CHILD
var cellChild = srcCell.getChild( ich );
var childCopy = cellChild.copy();
var type = childCopy.getType();
types.push(type);
switch( type ){
case DocumentApp.ElementType.PARAGRAPH:
dstCell.insertParagraph( ich, childCopy );
break;
case DocumentApp.ElementType.LIST_ITEM:
var atts = childCopy.getAttributes();
var newListItem = dstCell.insertListItem( ich, childCopy );
newListItem.setAttributes( atts );
break;
case DocumentApp.ElementType.TABLE:
var newTable = dstCell.insertTable( ich, childCopy );
CopyTableCellByCell( cellChild, newTable );
break;
case DocumentApp.ElementType.INLINE_IMAGE:
var parpar = childCopy.getParent();
var ttt = parpar.getType();
destImg = parpar.insertInlineImage(l, childCopy.getBlob());
dstImg.setWidth(childCopy.getWidth());
dstImg.setHeight(childCopy.getHeight());
break;
}
}
}
}
}
Thanks for your help.