0

what I try to do is the following: I would like to merge the bodys (with formatting, tables, images etc.) of three different google documents. I searched the web for hours but did not find a solution I was able to make work. Can anybody here help me?

Thank you in advance.

Best, Phil

user1582830
  • 79
  • 1
  • 10

1 Answers1

0

See the best answer of this post, code goes like this :

function mergeDocs() {
  var docIDs = ['list-of','documents','ids','you should have somehow'];
  var baseDoc = DocumentApp.openById(docIDs[0]);
  var body = baseDoc.getActiveSection();

  for( var i = 1; i < docIDs.length; ++i ) {
    var otherBody = DocumentApp.openById(docIDs[i]).getActiveSection();
    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 )
        body.appendParagraph(element);
      else if( type == DocumentApp.ElementType.TABLE )
        body.appendTable(element);
      else if( type == DocumentApp.ElementType.LIST_ITEM )
        body.appendListItem(element);
      else
        throw new Error("According to the doc this type couldn't appear in the body: "+type);
    }
  }
}

If some element types are not in the list you can easily add them following the same logic.

Community
  • 1
  • 1
Serge insas
  • 45,904
  • 7
  • 105
  • 131