Translated by @Zhang Chi, updated at 2020-02-16

Developers.google.com/docs/api/co…

You can invoke the Google Docs API using HTTP requests or method calls from language-specific client libraries. These are roughly the same, but using client libraries is usually much simpler.

The API returns an HTTP response, which typically includes the result of the request invocation. When a request is made using a client library, the response is returned in a language-specific manner.

Request Methods

The Docs API supports the following request methods:

  • Create – Creates a new blank Google Docs document.
  • Get – Returns the full instance of the specified document. You can parse the returned JSON to extract document content, format, and other functionality.
  • BatchUpdate – Submits a list of edit requests to apply to the document and returns a list of results.

You can invoke these methods using client library methods, or directly as HTTP requests, and return the appropriate response depending on the invocation style. For more information on these requests and responses, see API Reference.

Batch Update Batch update

The batchUpdate method works by getting one or more request objects, each specifying a single request type to execute. There are many different types of requests. Here is a breakdown of the request types, divided into different categories.

Manipulating text

  • replaceAllText
  • insertText

Operating style

  • updateTextStyle
  • updateParagraphStyle
  • createParagraphBullets
  • deleteParagraphBullets

Operating range

  • createNamedRange
  • deleteNamedRange
  • deleteContentRange

Operating picture

  • insertInlineImage

A popular batch request pattern is as follows:

requests = [] requests.append(some request) requests.append(another one) requests.append(and another one) . . . body = . & requests & ... . batchUpdate(body)Copy the code

The API validates each request in the batchUpdate call before applying them: if any request is invalid, the entire batch fails, and the document remains unchanged.

Some requests return responses that contain detailed information about how the request is applied. Other requests simply return an empty reply. The order of the replies matches the order of the requests.

For example, suppose you call batchUpdate with four updates, and only the third update returns information. The response will have two empty replies, a reply to the third request and another empty reply (in that order).

Because other users may be editing the document, your changes may change as the collaborator changes. If there are no collaborators, the updated document should accurately reflect your changes. In any case, make sure the updates in your request are applied together atomically.