RedYetiDev
RedYetiDev

Reputation: 679

How to I copy one google doc into another

I am wondering how to copy a google doc, but include the style, links and font, from within google scripts. I have tried to use the getBody()

DocumentApp.getActiveDocument().getBody().clear()
document2 = DocumentApp.openById("Document_ID").getBody().getText()
DocumentApp.getActiveDocument().getBody().appendParagraph(document2)

But that only copies that raw text.

EDIT: Removed documents because the question was solved

Upvotes: 2

Views: 207

Answers (1)

NightEye
NightEye

Reputation: 11204

Your problem is that getText will only return the raw text, nothing more. Treat them as objects instead, iterate, then append one by one.

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 will be used for each element type.
  }
  destination.saveAndClose();
}

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

Upvotes: 2

Related Questions