Make a copy of an existing document copy the complete documen including the header footer and style

To copy an entire Google Document including headers, footers, and styles directly, you don’t need to individually replicate each element’s content as I previously described. Instead, you can use Google Drive’s copy function for a more straightforward approach. This function automatically copies all content, maintaining the original formatting, headers, and footers.

Here’s how you can modify the script to use Google Drive’s copy feature, and then move the new document to a specified folder:

function copyGoogleDocWithCompleteFormat() {
  var sourceDocId = 'source-document-id'; // Replace with your source document ID
  var targetFolderId = 'target-folder-id'; // Replace with your target folder ID
  
  // Use Google Drive's copy function to copy the entire document, including formatting, headers, and footers
  var sourceFile = DriveApp.getFileById(sourceDocId);
  var newFile = sourceFile.makeCopy(); // Creates a copy of the source document
  var newDoc = DocumentApp.openById(newFile.getId());
  
  // Rename the new document to indicate it's a copy or any other naming convention you prefer
  newDoc.setName(sourceFile.getName() + ' - Copy');
  
  // Move the new document to the specified folder
  moveDocumentToFolder(newFile.getId(), targetFolderId);

  // Log the URL of the new document for easy access
  Logger.log('New document created with URL: ' + newDoc.getUrl());
}

function moveDocumentToFolder(docId, folderId) {
  var file = DriveApp.getFileById(docId);
  var folder = DriveApp.getFolderById(folderId);
  folder.addFile(file); // Add the file to the target folder
  DriveApp.getRootFolder().removeFile(file); // Remove the file from the root directory if necessary
}

Replace 'source-document-id' with the actual ID of your source document and 'target-folder-id' with the ID of the Google Drive folder where you want the new document to be placed.

This script uses Google Drive’s makeCopy() function, which creates a complete copy of the document, including all elements like text, images, styles, headers, and footers. Then, it moves the new document to the specified folder and logs the new document’s URL for access.

Remember to save your changes and run the script. If this is your first time running the script or if you’ve modified the scopes, you might need to authorize permissions again. This approach ensures that the complete document is duplicated without missing any complex elements or formatting.