'How to combine Google Docs documents without previous versions changing style

I'm using a script (below) to combine several Google documents into one, and it mostly works, but the output document is introducing different styling (font, line-spacing) in various places – I think wherever the source documents have edits in the version history. It will do this mid-word, even, if that's where the edit was made, or for entire sections that have been added in since the original version.

Is there a way to just pull the current version, with none of the historical changes, and keep the styling intact? I've read the documentation but can't find anything about the document version information. Thank you

function mergeGoogleDocs() {
  var folderToMergeId = 'XXX'
  var baseDoc = DocumentApp.openById(XXX); // output doc
  var folder = DriveApp.getFolderById(folderToMergeId);
  var docsToMerge = [];
  var files = folder.getFiles();

  while (files.hasNext()){
    file = files.next();
    docsToMerge.push([file.getId(), file.getName()]);
  }
  
// Clear the output document and add in the files to merge
  baseDoc.getBody().clear();
  
  for (var i = 0; i < docsToMerge.length; i++ ) {
      
    var otherBody = DocumentApp.openById(docsToMerge[i][0]).getBody();       
    var totalElements = otherBody.getNumChildren();

    for( var j = 0; j < totalElements; ++j ) {
      var element = otherBody.getChild(j).copy();
      var type = element.getType();
      if( type == DocumentApp.ElementType.PARAGRAPH )
        baseDoc.getBody().appendParagraph(element);
      else if( type == DocumentApp.ElementType.TABLE )
        baseDoc.getBody().appendTable(element);
      else if( type == DocumentApp.ElementType.LIST_ITEM )
        baseDoc.getBody().appendListItem(element);
      else
        throw new Error("Unknown element type: "+type);
    }
  }
  
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source