Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 394
Igor Avatar asked Nov 01 '22 20:11

Igor


1 Answers

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.

like image 104
noltie Avatar answered Nov 09 '22 06:11

noltie