开发人员如何以编程方式编辑 Google 文档?有文档 API 吗?
(据我所知)似乎没有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
更新(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}
的文档: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:
to you.
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}
:文档列表 API 自 2012 年 9 月起已被弃用,看起来就像它可能2015 年 4 月后停用一样。
正如问题所示,使用 Drive API 更新 HTML 版本似乎是唯一的其他方法。我一直在尝试这个,并且遇到了一些问题。
可能还有更多问题。这些只是我最近几天发现的。
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.
There may be more issues. These are just the ones I have found in the last few days.
不太确定这是否正是您正在寻找的内容,但您是否在这里查看过 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).
有 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.
(2019) Google 现在为文档、幻灯片、表格、驾驶。
(2019) Google now provides API for docs, slides, sheets, drive.
有一个示例应用程序可以实现此目的,Dr.编辑,在 Google 云端硬盘的文档中。
There is a sample app for this, Dr. Edit, on Google Drive's documentation.