alexandre
alexandre

Reputation: 21

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

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 :)

Upvotes: 2

Views: 443

Answers (2)

NightEye
NightEye

Reputation: 11214

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.

Upvotes: 2

Fischbrot
Fischbrot

Reputation: 828

Common practice for this is to first get the contents of the DOC and append it to a single big file.

Here I have a reference for you:

https://stackoverflow.com/a/61323430/10648655

Upvotes: 1

Related Questions