获取和更新谷歌文档版本

时间:2021-06-06 18:35:00

标签: google-drive-api google-docs-api

我有一个 Google 文档,其中包含多个使用 UI 添加的命名版本,我需要能够

  • 使用 API 获取指向特定版本的文档的 link
  • 使用 API 添加新版本。

背后的想法是使用这个 link 将文档连接到 git 存储库中的特定提交

enter image description here

在文档中找不到任何参考,我在堆栈溢出中发现了一些问题,但没有人给出正确的解决方案。

1 个答案:

答案 0 :(得分:3)

问题 1 的答案:

<块引用>

使用 API 获取指向特定版本的文档的链接

遗憾的是,在现阶段,似乎没有任何方法可以检索每个修订版直接打开 Google 文档的 URL。 Ref 作为一种变通方法,当为每个修订版打开 Google 文档作为导出时,可以使用此方法。 在这种情况下,您可以使用以下示例请求。

curl \
  -H 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
  'https://www.googleapis.com/drive/v3/files/{documentId}/revisions?fields=*'

运行此请求时,得到如下结果。

{
  "kind": "drive#revisionList",
  "revisions": [
    {
      "kind": "drive#revision",
      "id": "1",
      "mimeType": "application/vnd.google-apps.document",
      "modifiedTime": "###",
      "published": false,
      "lastModifyingUser": {
        "kind": "drive#user",
        "displayName": "###",
        "photoLink": "###",
        "me": true,
        "permissionId": "###",
        "emailAddress": "###"
      },
      "exportLinks": {
        "application/rtf": "###",
        "application/vnd.oasis.opendocument.text": "###",
        "text/html": "###",
        "application/pdf": "###",
        "application/epub+zip": "###",
        "application/zip": "###",
        "application/vnd.openxmlformats-officedocument.wordprocessingml.document": "###",
        "text/plain": "###"
      }
    },
    ,
    ,
    ,
  ]
}
  • 对于上述返回值,例如,当检索到第 1 个索引处的 application/vnd.openxmlformats-officedocument.wordprocessingml.documentexportLinks 时,即为修订号 1 的 DOCX 数据的 URL。
  • 在这种情况下,网址不会公开共享。所以请注意这一点。当您想访问它时,需要登录。如果您想让用户访问它,请与用户共享。

问题 2 的答案:

<块引用>

使用 API 添加新版本。

不幸的是,在这种情况下,Drive API 的“修订”中没有创建修订的方法。但是,当使用 Google Docs API 和 Google Apps Script 更新文档时,会添加新的修订号。我认为这可能适用于您的情况。

参考:

相关问题