How to combine N google docs in one using docs-api (node)

Viewed 228

guys I need to get N google docs and combine them into one big google doc.

Is it possible to do it using docs-api or drive-api ?

I'm able to identify if its a paragraph or table, but how to insert this content into one file keeping the original style.

I need to do it on the server side, as we can't have human interaction at this time.

Thanks :)

2 Answers

You need to traverse all objects in the source document, then append them one by one into the destination document.

function appendContents() {
  var source = DocumentApp.getActiveDocument().getBody();
  var destination = DocumentApp.openById("DOC ID");

  var numElements = source.getNumChildren();

  for (var i = 0; i < numElements; ++i ) {
    var body = destination.getBody()
    var element = source.getChild(i).copy();
    var type = element.getType();
    if( type == DocumentApp.ElementType.PARAGRAPH ){
      body.appendParagraph(element);
    }
    // Add other element types if you are expecting other elements
    // e.g. LIST_ITEM, TEXT, TABLE
    // Note that different append function will be used for each element type.
  }
  destination.saveAndClose();
}

If you want to add other element types, see list of available ElementTypes.

Related