使用节点googleapis将两个Google文档合并在一起 [英] Merge two google documents together using node googleapis

查看:57
本文介绍了使用节点googleapis将两个Google文档合并在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将两个Google文档合并为一个.我知道如何复制一个文档,但是我需要创建一个包含两个Google文档内容的新文档.

I need to merge two google docs into one. I know how to copy one doc, but I need to create a new document which contains the contents of two google docs.

我想出了以下几点:

    const auth = await authorize(credentials)
    const docs = google.docs({version: 'v1', auth})
    const drive = google.drive({version: 'v3', auth})

    const file1ToCopy = await docs.documents.get({documentId: FILE_1_ID})
    const file2ToCopy = await docs.documents.get({documentId: file_2_ID})
    const {body: body1ToCopy} = file1ToCopy.data
    const {body: body2ToCopy} = file2ToCopy.data

    await drive.files.create({
        resource: {
            name: `test ${moment().toString()}`,
            mimeType: 'application/vnd.google-apps.document',
        },
        media: {
            body: {...body1ToCopy, ...body2ToCopy}, // This is not the correct way, body accepts only stream also the bodies received from documents.get are objects, so they probably can not be combined like this...
        },
    })

如您所见,我可以获取单个文档的正文,但无法将它们插入一个文件中.

As you can see I am able to get the individual document's bodies but I am unable to insert them into one file.

有人可以帮我吗?

推荐答案

文件:创建.您应该首先检索要添加到目标文档中的内容,然后使用Docs API插入这些内容.

Instances of Document are not valid arguments to provide to Files: create. You should first retrieve the contents you want to add to your destination Document, and then insert these contents using Docs API.

我建议您(1)制作第一个文档的副本(此副本将成为合并的文档),(2)从第二个文档中检索内容,以及(3)插入检索到的内容在上一步中复制到文档.

I'd suggest you to (1) make a copy of the first of the Docs (this copy will be the merged Doc), (2) retrieve the contents from the second Doc, and (3) insert the contents retrieved in previous step to the copied Doc.

您可以按照以下方式进行操作:

You could do something along the following lines:

使用文件:复制.第二份文档中的内容将添加到此副本中.

Make a copy of the first Document with Files: copy. The contents from the second Document will be appended to this copy.

要从第二个文档中提取正文内容,必须考虑 Google文档的结构:文档.get 检索文档的实例,它包含要添加到新文档中的内容,但不是内容本身.

In order to extract the body content from the second Doc, you have to take into account the structure of a Google Document: documents.get retrieves an instance of Document, which contains the contents you want to append to your new document, but is not the contents themselves.

文档资源中的正文内容基本上是由内的 StructuralElements 的列表组成内容,例如:

The body content in a Document resource is basically made of a list of StructuralElements inside content, like this:

{
  "documentId": string,
  "title": string,
  "body": {
    "content": [
      {
        object (StructuralElement)
      }
    ]
  },
  // Other document props
}

您将必须遍历 content 中的每个 StructuralElement ,并存储在下一步中附加内容所需的所有信息(至少应包括类型结构元素的名称(因为取决于此,必须使用一种或另一种方法)以及文本内容).我建议您将这些信息存储到一个对象数组中,以备后用.

You would have to iterate through each StructuralElement in content and store all the information needed to append the content in next step (these would include, at least, the type of structural element —since depending on this, one method or another would have to be used— and the text content). I'd suggest you to store this information to an array of objects, to be used later.

作为示例,可以在此处找到有关如何从文档中提取文本的相当不错的解释(即使该示例仅包含Java和Python中的示例,将其翻译"为特殊语言也不应该特别困难)节点):从文档中提取文本.您可以保留其他属性的信息,例如元素类型(不仅仅是文本),但是基本思想非常相似.

As an example, a pretty decent explanation of how to extract the text from a Document can be found here (even though the example only contains samples in Java and Python, it shouldn't be specially hard to "translate" this to Node): Extract the text from a document. You could keep information on other properties, like the element type (not just the text), but the basic idea is very similar.

成功从第二个文档中检索结构元素后,您将必须使用Docs API通过

Once you have successfully retrieved the structural elements from the second document, you would have to use Docs API to append these elements to the merged document using documents.batchUpdate.

根据每次必须附加的结构元素类型,可能需要不同的请求.例如,如果结构元素是仅包含文本的段落,则可以使用插入文本请求以附加文本.或者,如果您想追加表格,则必须使用

Depending on which type of structural element you have to append every time, a different request might be needed. For example, if the structural element is a paragraph that only contains text, you can use InsertTextRequest to append the text. Or if you want to append a table, you would have to use InsertTableRequest. Because of this, the script would have to check each structural element type before appending it to the Document.

这篇关于使用节点googleapis将两个Google文档合并在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆