I have followed this script How to copy content and formatting between Google Docs? to copy the content from one google doc and paste it to another, which works great, however everytime the content is pasted, there is a space on top of the pasted content, see below. How can the content be pasted properly?
Source file: https://docs.google.com/document/d/1xVpJM4hSN3fosFXR16JbZ1_7r0_PxV92T-G24X5LQRo/edit
Target file: https://docs.google.com/document/d/1g9oon4e0FDBF2fbexVCR-uxKko3B6-Hpj850kiH3qXo/edit
Basically the table from the source file will get copied and pasted to the target file for multiple times, and the tables should sit side by side on the target file without space on top which breaks the format.
appscript is inbedded in the source file
function copyDoc() {
var sourceDoc = DocumentApp.getActiveDocument().getBody();
// var targetDoc = DocumentApp.create('CopyOf'+DocumentApp.getActiveDocument().getName());
var targetDoc = DocumentApp.openById('1g9oon4e0FDBF2fbexVCR-uxKko3B6-Hpj850kiH3qXo');
var totalElements = sourceDoc.getNumChildren();
for( var j = 0; j < totalElements; ++j ) {
var body = targetDoc.getBody()
var element = sourceDoc.getChild(j).copy();
var type = element.getType();
if( type == DocumentApp.ElementType.PARAGRAPH ){
body.appendParagraph(element);
}
else if( type == DocumentApp.ElementType.TABLE){
body.appendTable(element);
}
else if( type == DocumentApp.ElementType.LIST_ITEM){
body.appendListItem(element);
}
// ...add other conditions (headers, footers...
}
targetDoc.saveAndClose();
}
Edit1: Based on TheWizEd
's answer, here's how it looks like, format looks very off.