开发人员如何以编程方式编辑 Google 文档?有文档 API 吗?

发布于 2024-08-24 19:59:26 字数 95 浏览 16 评论 0原文

(据我所知)似乎没有一个 API 可以编辑 Google 文档(不是电子表格,而是基于 HTML 的文档)。有人做过类似的事情吗?也许通过下载 HTML 版本、编辑并上传更改?

There doesn't seem to be (to my knowledge) an API to edit Google Docs (not spreadsheets, their HTML based documents). Has anyone done something like the? Maybe by downloading the HTML version, editing and uploading the changes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

疯到世界奔溃 2024-08-31 19:59:26

更新(2019 年 5 月)Google Docs API 正式于 2019 年 2 月推出。该文档位于下面我 7 月更新的链接中。发布几周后,我制作了一个高级视频概述,介绍了使用 API 的邮件合并应用程序的外观喜欢。 (这不是完整的 G Suite 开发展示 剧集,但确实链接到工作示例。)

更新(2018 年 7 月)Google 文档团队在 Google Cloud NEXT '18 上预先宣布即将推出 REST API。有兴趣参与新 API 抢先体验计划的开发者应在 https://developers.google.com/docs 上注册。下面的原始答案仍然有效,因为 REST API 将成为您以编程方式访问 Google 文档的第二种方式。

原始答案(2017 年 3 月):(大多数其他答案已过时。)Google 文档目前没有 REST API,但开发人员可以使用 Google Apps 脚本,在 Google 云中托管并运行的服务器端 JavaScript 应用。如果您不熟悉 Apps 脚本或使用它编辑 Google 文档,这里有一些学习资源:

简单示例:如果您有一个现有文档(云端硬盘)文件 ID 为 DOCUMENT_ID_GOES_HERE,以下是使用 Apps 脚本对其进行基本编辑的方法,对名称和名称进行伪“邮件合并”。通过电子邮件发送到给定占位符 {NAME}{ADDR} 的文档:

function mergeNameEmail() {
    // Open a document by ID
    var doc = DocumentApp.openById(DOCUMENT_ID_GOES_HERE);

    // Access the body of the document
    var body = doc.getBody();

    // Merge name & address from template
    body.replaceText("{NAME}", "Ima Developer");
    body.replaceText("{ADDR}", "123 Main St, Anytown, XX 00000");
}

UPDATE (May 2019) The Google Docs API was officially launched in Feb 2019. The documentation is located at the link from my update in July below. A few weeks after launch, I produced a high-level video overview of what a mail merge application using the API would look like. (It's not a full-fledged G Suite Dev Show episode but does link to a working sample.)

UPDATE (Jul 2018) The Google Docs team pre-announced a forthcoming REST API at Google Cloud NEXT '18. Developers interested in getting into the early access program for the new API should register at https://developers.google.com/docs. The original answer below still stands as the REST API will become the second way you can access Google Docs programmatically.

Original answer (Mar 2017): (Most other answers are outdated.) Google Docs does not currently have a REST API, however developers can programmatically access (CRUD) documents using Google Apps Script, server-side JavaScript apps that are hosted at and run in Google's cloud. If you're new to Apps Script or to editing Google Docs with it, here are some learning resources:

Simple example: if you have an existing Doc with a (Drive) file ID of DOCUMENT_ID_GOES_HERE, here's how you'd basically edit it with Apps Script, doing a pseudo "mail merge" of name & email into the document given placeholders {NAME} and {ADDR}:

function mergeNameEmail() {
    // Open a document by ID
    var doc = DocumentApp.openById(DOCUMENT_ID_GOES_HERE);

    // Access the body of the document
    var body = doc.getBody();

    // Merge name & address from template
    body.replaceText("{NAME}", "Ima Developer");
    body.replaceText("{ADDR}", "123 Main St, Anytown, XX 00000");
}
甜警司 2024-08-31 19:59:26

文档列表 API 自 2012 年 9 月起已被弃用,看起来就像它可能2015 年 4 月后停用一样。

正如问题所示,使用 Drive API 更新 HTML 版本似乎是唯一的其他方法。我一直在尝试这个,并且遇到了一些问题。

  1. 评论将转换为引用并添加到文档末尾。
  2. 如果其他人通过浏览器编辑文档,他们在 API 读取和更新时间之间所做的任何更改都会丢失。
  3. 对文档的更新可能会破坏格式。例如,我多次更新了文档,并且某些元素(h1、h2 等)之间的垂直间距每次都不断变宽,从而破坏了文档。
  4. 当 API 更新发生时,文档中任何人的光标都会移动到页面顶部。

可能还有更多问题。这些只是我最近几天发现的。

The Document List API has been deprecated since September 2012 and looks like it could be retired after April 2015.

Updating the HTML version using the Drive API, as the question suggests, looks to be the only other way. I have been trying this and I have experienced a few of issues.

  1. Comments are converted into citations and added to end of document.
  2. If someone else is editing the doc via the browser any changes made by them between the API read and update time are lost.
  3. Updates to a doc can break the formatting. For example I updated a doc several times and the vertical spacing between some elements (h1's, h2's etc) kept widening each time and ruined the doc.
  4. When an API update occurs the cursor of anyone in the doc is moved to the top of the page.

There may be more issues. These are just the ones I have found in the last few days.

娇女薄笑 2024-08-31 19:59:26

不太确定这是否正是您正在寻找的内容,但您是否在这里查看过 http://code.google.com/apis/documents/docs/3.0/developers_guide_protocol.html 看起来它允许编辑内容(无论如何 v3.0)。

Not really sure if this is what you're looking for exactly but have you taken a look here http://code.google.com/apis/documents/docs/3.0/developers_guide_protocol.html It looks like it allows editing for content (v3.0 anyway).

北渚 2024-08-31 19:59:26

有 com.google.api.services.drive.model.File.getExportLinks

您可以获取 Google 文档作为 docx(例如),使用您最喜欢的 docx 编辑器对其进行编辑,然后再次上传。请参阅示例来执行此操作(从GoogleDriveDownloadAsDocx)在上下文中docx4j 的。请注意自述文件

或者对任何其他导出格式执行相同的操作。

There is com.google.api.services.drive.model.File.getExportLinks

You can get a Google Doc as a docx (for example), edit it using your favourite docx editor, then upload again. See the samples for doing this (starting with GoogleDriveDownloadAsDocx) in the context of docx4j. Note the README.

Or do the same with any of the other export formats.

停顿的约定 2024-08-31 19:59:26

(2019) Google 现在为文档、幻灯片、表格、驾驶。

(2019) Google now provides API for docs, slides, sheets, drive.

迷迭香的记忆 2024-08-31 19:59:26

有一个示例应用程序可以实现此目的,Dr.编辑,在 Google 云端硬盘的文档中。

There is a sample app for this, Dr. Edit, on Google Drive's documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文