How does one copy a Docs document into another one?

3,134 views
Skip to first unread message

data q-metrics

unread,
Jun 15, 2020, 4:47:39 AM6/15/20
to Google Apps Script Community

I've read the documentation which says that `.copy()` does a deep copy. In the case of a Table or a Paragraph this copy can then be used to create a duplicate in a different document. 

Interestingly, Body can also be `.copy()`-d but I don't see any equivalent method for setting the Body of a Docs document to the deeply copied copy. 

Rather more helpfully for me would be the ability to append or insert this deep copy into the second document. Is there anything like this available?

On a related note, taking a deep copy of a table and inserting it into another document works fine EXCEPT for images that are embedded in the table. The frame goes over but not the image data.

Ederd

unread,
Sep 12, 2020, 4:38:05 PM9/12/20
to Google Apps Script Community
I had a similar question and was disappointed that no one had provided you with an answer. I finally found an answer that works for me and perhaps will help others.

function copyDoc() {
  var sourceDoc = DocumentApp.getActiveDocument().getBody();
  var targetDoc = DocumentApp.openById(' ID ');
  var totalElements = sourceDoc.getNumChildren();

  for( var j = 0; j < totalElements; ++j ) {
    var body = targetDoc.getBody()
    var element = sourceDoc.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);
      }
//    ...add other conditions (headers, footers...
    }
  targetDoc.saveAndClose();
}

// This function copied ALL of the source Document and posted it at the bottom of the other target Document text;
// including the correct formatting. The targetDoc need not be Active when the Script is run.
// The code written by Serge insas as an answer in Stack Overflow Nov 14, 2013.

Alex Shisller da Silva Pinto

unread,
Feb 10, 2023, 4:26:02 PM2/10/23
to Google Apps Script Community
This code was really helpfull, but I couldn't coppy the footer and header as well. I tried to add the code lines to copy header and footer, but it didn't work

Leah Janss Lafond

unread,
Feb 11, 2023, 3:14:28 PM2/11/23
to Google Apps Script Community
Hi,
This  copies my whole document, including header, footer, images, etc. I've got a spreadsheet with the file ID I want copied in col1. The script takes the fileID, makes a copy of the document,  saves it to a new doc in my specified folder, then puts the new ID in col 2. 
Hope it helps you. 

function copyDocuments() {
  let ss = SpreadsheetApp.getActiveSpreadsheet();
  let sheet = ss.getSheetByName("Sheet1");
  
  // Replace "FOLDER_ID" with yours
  var folder = DriveApp.getFolderById("YOUR_FOLDER_ID");
  
  for (var i = 2; i <= sheet.getLastRow(); i++) {
    var docId = sheet.getRange(i, 1).getValue();
    var sourceDoc = DriveApp.getFileById(docId);
    
    var newDoc = sourceDoc.makeCopy("Copy of " + sourceDoc);
    folder.addFile(newDoc);
    var newDocId = newDoc.getId();
    
    sheet.getRange(i, 2).setValue(newDocId);
  }
}

All the best, 

Leah 
Reply all
Reply to author
Forward
0 new messages