Igor
Igor

Reputation: 197

Combine Google Docs documents

Is it possible to merge 100 Google Docs documents into one? I've tried copy-pasting, but it seems too long and it's not possible to copy comments.

Upvotes: 6

Views: 11601

Answers (4)

Metric Rat
Metric Rat

Reputation: 316

Both the above fail for me with the script returning a red lozenge:

Service unavailable: Docs Dismiss

(the documents in the folder are found, as are the document id's, and the combined doc is created, but empty)

Fixed that - had a document in the list that wasn't owned by me or was created by conversion. Removed that and away we go.

Upvotes: 0

Red Mercury
Red Mercury

Reputation: 4310

I expanded on @noltie's answer to support merging docs in a folder structure recursively, starting from an arbitrary folder (not necessarily the root folder of google docs) and guard agains script failures on too many unsaved changes.

function getDocsRec(rootFolder) {
  var docs = [];

  function iter(folder) {
    var childFolders = folder.getFolders();
    while (childFolders.hasNext()) {
      iter(childFolders.next());
    }

    var childFiles = folder.getFiles();
    while (childFiles.hasNext()) {
      var item = childFiles.next();
      var docName = item.getName();
      var docId   = item.getId();
      var doc     = {name: docName, id: docId};
      docs.push(doc);
    }
  }

  iter(rootFolder);
  return docs;
}

function combineDocs() {
  // This function assumes only Google Docs files are in the root folder
  // Get the id from the URL of the folder.
  var folder = DriveApp.getFolderById("<root folder id>");
  if (folder == null) { Logger.log("Failed to get root folder"); return; }

  var combinedTitle = "Combined Document Example";
  var combo = DocumentApp.create(combinedTitle);
  var comboBody = combo.getBody();

  // merely get the files recursively, does not get them in alphabetical order.
  var docArr = getDocsRec(folder);

  // Log all the docs we got back. Click "Edit -> Logs" to see.
  docArr.forEach(function(item) {
    Logger.log(item.name)
  });

  // this sort will fail if you have files with identical names
  // docArr.sort(function(a, b) { return a.name < b.name ? -1 : 1; });

  // Now load the docs into the combo doc.
  // We can't load a doc in one big lump though;
  // we have to do it by looping through its elements and copying them
  for (var j = 0; j < docArr.length; j++) {

    // There is a limit somewhere between 50-100 unsaved changed where the script
    // wont continue until a batch is commited.
    if (j % 50 == 0) {
      combo.saveAndClose();
      combo = DocumentApp.openById(combo.getId());
      comboBody = combo.getBody();
    }

    var entryId = docArr[j].id;
    var entry = DocumentApp.openById(entryId);
    var entryBody = entry.getBody();
    var elems = entryBody.getNumChildren();
    for (var i = 0; i < elems; i++) {
      var elem = entryBody.getChild(i).copy();
      switch (elem.getType()) {
        case DocumentApp.ElementType.HORIZONTAL_RULE:
          comboBody.appendHorizontalRule();
          break;
        case DocumentApp.ElementType.INLINE_IMAGE:
          comboBody.appendImage(elem);
          break;
        case DocumentApp.ElementType.LIST_ITEM:
          comboBody.appendListItem(elem);
          break;
        case DocumentApp.ElementType.PAGE_BREAK:
          comboBody.appendPageBreak(elem);
          break;
        case DocumentApp.ElementType.PARAGRAPH:
          comboBody.appendParagraph(elem);
          break;
        case DocumentApp.ElementType.TABLE:
          comboBody.appendTable(elem);
          break;
        default:
          var style = {};
          style[DocumentApp.Attribute.BOLD] = true;
          comboBody.appendParagraph("Element type '" + elem.getType() + "' could not be merged.").setAttributes(style);
      }
    }
   // page break at the end of each entry.
   comboBody.appendPageBreak();
  }
}

You can create and run a script with the above code on https://script.google.com/home

Upvotes: 3

noltie
noltie

Reputation: 86

This can be done with Google Apps Script. See this example. The most relevant parts (example assumes nothing but Google Docs in the folder):

function combine() {
  var folder = DriveApp.getRootFolder();
  if (folder == null) { Logger.log("Failed to get root folder"); return; }
  var combinedTitle = "Combined Document Example";
  var combo = DocumentApp.create(combinedTitle);
  var comboBody = combo.getBody();
  var hdr = combo.addHeader();
  hdr.setText(combinedTitle)

  var list = folder.getFiles();
  while (list.hasNext()) {
    var doc = list.next();
    var src = DocumentApp.openById(doc.getId());
    var srcBody = src.getBody();
    var elems = srcBody.getNumChildren();
    for (var i = 0; i < elems; i++ ) {
      elem = srcBody.getChild(i).copy();
      // fire the right method based on elem's type
      switch (elem.getType()) {
        case DocumentApp.ElementType.PARAGRAPH:
          comboBody.appendParagraph(elem);
          break;
        case // something
      }
    }
  }
} 

Note that you don't copy the source document's contents in one lump; you have to loop through them as individual elements and fire the correct append* method to add them to the merged/destination file.

Upvotes: 5

CheddarCheese
CheddarCheese

Reputation: 1

Download all the files as Docx, then use Microsoft Word or Open Office to merge the documents using the "master document" feature. (Word also refers to this as "Outline.")

Upvotes: -2

Related Questions