简体   繁体   中英

How do you create a document in Google Docs programmatically?

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?

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).

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

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" (ie, 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.

Alex's answer, while undoubtedly correct, begs the question: "how do I do that via the Google Docs API?"

Here's a way (in Python, 'cause I'm that kind of guy):

import gdata.docs.service
import StringIO

client = gdata.docs.service.DocsService()
client.ClientLogin(username, password,
                   source='Spreadsheet Creation Example')

content = 'COL_A, COL_B, COL_C, COL_D\ndata1, data2, data3, data4'
ms = gdata.MediaSource(file_handle=StringIO.StringIO(content),
                       content_type='text/csv',
                       content_length=len(content))
entry = client.Upload(ms, 'Test Spreadsheet')

This is a small mashup of techniques that I found in http://code.google.com/p/gdata-python-client/source/browse/tests/gdata_tests/docs/service_test.py , which I in turn found via this post from the Google Group for the GData Docs API.

The key insights (for me anyway) were:

  1. realizing that the MediaSource constructor's formal parameter "file_handle" will take any file-like object, and
  2. discovering (as the OP's followup to the Google Group post mentions) that the unit tests are a great source of examples

(I wasn't able to find the Python-specific developer's guide referenced by Alex's doc link -- possibly it's been lost or buried in Google's move of documentation assets from code.google.com to developers.google.com. Alex's link now redirects to the more generic document that shows mostly .NET and Java examples, but only a little Python.)

As of Feb 4, 2019, Google Docs now has a REST API.

See documentation: https://developers.google.com/docs/api/

(Sep 2019) There are 3 ways to create a document in Google Docs programmatically:

  1. Google Docs REST API (low-level; Python, JS/Node.js, Java, C#/.NET, PHP, Ruby, Go, etc.)
  2. Google Apps Script (high-level; JavaScript-only)
  3. Google Drive API (low-level like Docs API above; both alternatives above can create or edit documents, but this one is create- or delete-only plus editing sharing/permissions)

The Docs API was officially launched in Feb 2019. 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.) Check out the various guides on using the API, including Quickstart examples in a variety of programming languages.

OTOH, Apps Script is a simpler, higher-level alternative. It's a custom server-side JavaScript runtime supporting apps that are hosted+executed in Google's cloud. Use objects to talk to various Google APIs (G Suite & beyond) without knowledge of HTTP, REST, nor OAuth2. You can also access external databases with its JDBC Service or call other apps via its URL Fetch Service .

With Apps Script, you can create standalone applications , document-bound applications (only works for a single document), or Google Docs Add-ons to extend the functionality of Google Docs. Here are the Google Docs Apps Script overview page as well as the Apps Script reference documentation for Google Docs (Document Service). I've also produced a variety of Apps Script videos if that's your preferred learning vehicle. If you're new to Apps Script, see my answer to a similar SO question for more learning resources.

Typically the Docs, Sheets, Slides, etc., APIs are used to perform document-oriented functionality while the Drive API is used primarily for file-based operations. However "create" is a special case where you can use either. See my answer to another SO question which shows the difference b/w creating a new Google Sheet via the Sheets API vs. the Drive API. (Both samples in Python.) Read this if you're interested in managing sharing or updating permissions of Google Docs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM