【问题标题】:How to combine N google docs in one using docs-api (node)如何使用 docs-api(节点)将 N 个谷歌文档合二为一
【发布时间】:2021-01-20 14:44:21
【问题描述】:

伙计们,我需要获取 N 个谷歌文档并将它们合并成一个大谷歌文档。

是否可以使用 docs-api 或 drive-api 来做到这一点?

我能够识别它是段落还是表格,但是如何将此内容插入到一个文件中,保持原始样式。

我需要在服务器端进行,因为我们目前无法进行人工交互。

谢谢:)

【问题讨论】:

    标签: javascript node.js google-drive-api google-docs-api


    【解决方案1】:

    通常的做法是首先获取 DOC 的内容并将其附加到单个大文件中。

    这里我有一个参考:

    https://stackoverflow.com/a/61323430/10648655

    【讨论】:

    • 谢谢,我正在尝试执行此操作,但我的问题是当我必须执行 insertText 请求时...我不知道要执行此操作的索引 =/
    • 什么索引?你能解释一下你的意思吗?
    • 例如,我有一个特定的文档,有 N 个段落,这些段落有不同的样式,例如斜体、粗体、不同的颜色等,我如何将文本和段落样式复制到一个新文件..
    【解决方案2】:

    您需要遍历源文档中的所有对象,然后将它们一一追加到目标文档中。

    function appendContents() {
      var source = DocumentApp.getActiveDocument().getBody();
      var destination = DocumentApp.openById("DOC ID");
    
      var numElements = source.getNumChildren();
    
      for (var i = 0; i < numElements; ++i ) {
        var body = destination.getBody()
        var element = source.getChild(i).copy();
        var type = element.getType();
        if( type == DocumentApp.ElementType.PARAGRAPH ){
          body.appendParagraph(element);
        }
        // Add other element types if you are expecting other elements
        // e.g. LIST_ITEM, TEXT, TABLE
        // Note that different append function will be used for each element type.
      }
      destination.saveAndClose();
    }
    

    如果您想添加其他元素类型,请参阅可用ElementTypes 列表。

    【讨论】:

    • 感谢您的回答。我忘了说我不能使用 Google AppScript……对不起,我的错。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 2020-06-30
    • 1970-01-01
    • 2013-09-30
    • 2019-03-15
    • 1970-01-01
    相关资源
    最近更新 更多