【问题标题】:Combine Google Docs documents合并 Google Docs 文档
【发布时间】:2014-07-23 12:43:42
【问题描述】:

是否可以将 100 个 Google Docs 文档合并为一个? 我试过复制粘贴,但是好像太长了,无法复制cmets。

【问题讨论】:

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


    【解决方案1】:

    这可以通过 Google Apps 脚本来完成。见this example。最相关的部分(示例假设文件夹中只有 Google Docs):

    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* 方法以将它们添加到合并/目标文件中。

    【讨论】:

    • 您好,谢谢-您知道我必须在哪里运行此代码才能合并文档吗?
    【解决方案2】:

    我扩展了@noltie 的答案,以支持递归地合并文件夹结构中的文档,从任意文件夹(不一定是 google 文档的根文件夹)开始,并防止由于太多未保存的更改而导致脚本失败。

    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上使用上述代码创建并运行脚本

    【讨论】:

      【解决方案3】:

      以上两种方法对我来说都失败了,脚本返回了一个红色菱形:

      服务不可用:Docs Dismiss

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

      已解决 - 列表中有一个不属于我的文档或由转换创建的文档。删除它,我们就走了。

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

          将所有文件下载为 Docx,然后使用 Microsoft Word 或 Open Office 使用“主文档”功能合并文档。 (Word 也将此称为“大纲”。)

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-07-03
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多