如何以编程方式在 Google Docs 中创建文档? [英] How do you create a document in Google Docs programmatically?
问题描述
Google 文档列表 API 的文档,似乎说您可以创建本地文档并上传.是否无法通过 API 在 Google 文档上实际创建和编辑文档?
The documentation for Google Documents List API, seems to say that you can create a local document and upload it. Is there no way to actually create and edit a document on Google Docs through an API?
推荐答案
虽然 文档 称其为上传",一切都归结为发送适当格式的HTTP POST
请求,所以当然它实际上可以是一个新的创建而不是实际上传"其他现有文件.(通过 POST
请求创建类似于通常描述为 REST API
的内容,但在实际的 REST
中,您通常会使用 PUT
请求而不是当然).
While the docs call it "uploading", everything boils down to sending an appropriately formatted HTTP POST
request, so of course it can actually be a new creation rather than an actual "upload" of an otherwise existing file. (Creation through POST
requests is similar to what's normally described as a REST API
, though in real REST
you'd typically use a PUT
request instead of course).
您只需要创建一个数据块,以列出的任何格式表示您的文档here -- 根据您的编程语言,最简单的可能是 text/csv
用于电子表格和 application/rtf
用于文本文档 -- 然后放入适当格式的 POST
数据.例如,要以最简单的方式(无元数据)制作电子表格,您可以POST
类似:
You just need to create a blob of data representing your document in any of the formats listed here -- depending on your programming language, simplest may be text/csv
for a spreadsheet and application/rtf
for a text-document -- then put in in an appropriately formatted POST
data. For example, to make a spreadsheet in the simplest way (no metadata), you could POST
something like:
POST /feeds/default/private/full HTTP/1.1
Host: docs.google.com
GData-Version: 3.0
Authorization: <your authorization header here>
Content-Length: 81047
Content-Type: text/csv
Slug: Example Spreadsheet
ColumnA, ColumnB
23, 45
提供专用 API 的每种特定编程语言都可以为这项不那么困难的任务提供帮助;例如,在 Python 中,根据文档,API 建议使用 ETags
以避免在多个客户端同时上传"(即创建或更新文档)时覆盖更改.但是直接准备 POST 总是可能的,因为几乎 REST API 被记录为协议作为所有特定于语言的 API 的基础.
Each specific programming language for which a dedicated API is supplied may offer help with this not-so-hard task; for example, in Python, per the docs, the API recommends using ETags
to avoid overwriting changes when multiple clients are simultaneously "uploading" (i.e., creating or updating docs). But preparing the POST directly is always possible, since the almost-REST API is documented as the protocol underlying all language-specific APIs.
这篇关于如何以编程方式在 Google Docs 中创建文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!