结合Google文档文档

时间:2014-07-23 12:43:43

标签: google-apps-script merge google-drive-api google-docs

是否可以将100个Google文档合并为一个? 我尝试过复制粘贴,但似乎太长了,无法复制评论。

5 个答案:

答案 0 :(得分:3)

可以使用Google Apps脚本完成此操作。见this example。最相关的部分(示例只假设文件夹中的Google文档):

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
      }
    }
  }
} 

请注意,您不能一次性复制源文档的内容;你必须作为单独的元素循环它们并激发正确的append *方法将它们添加到合并/目标文件。

答案 1 :(得分:1)

我扩展了@noltie的答案,以支持从任意文件夹(不一定是google docs的根文件夹)开始以递归方式将文档合并到文件夹结构中,并针对太多未保存的更改再次防止脚本失败。

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();
  }
}

您可以在https://script.google.com/home

上使用以上代码创建并运行脚本

答案 2 :(得分:0)

将所有文件下载为Docx,然后使用Microsoft Word或Open Office使用&#34;主文档&#34;合并文档。特征。 (Word也将此称为&#34;大纲。&#34;)

答案 3 :(得分:0)

以上两种情况对我来说都是失败的,脚本返回了红色菱形:

服务不可用:文档已关闭

(找到文件夹中的文档,找到文档ID的文档,并创建合并的文档,但为空)

解决了该问题-列表中有我不拥有的文件或不是通过转换创建的文件。删除它,然后我们就走了。

答案 4 :(得分:-1)

Google Docs不支持任何类型的合并。 您可以选择所有100个文档,下载它们并尝试离线合并它们。