How can developers programmatically edit a Google document? Is there a document API?

There seems to be no (as far as I know) API for editing Google Docs (and not spreadsheets, their HTML-based documents). Has anyone done something like this? Perhaps downloading the HTML version, editing and uploading the changes?

+11
google-apps-script google-docs google-api google-docs-api google-api-client
source share
6 answers

Not sure if this is exactly what you are looking for exactly, but you looked here http://code.google.com/apis/documents/docs/3.0/developers_guide_protocol.html It looks like it allows you to edit content (v3.0 anyway )

+2
source share

The API list of documents has been deprecated since September 2012 and looks like this: retired after April 2015 .

Updating the HTML version using the Drive API, as follows from this question, seems to be the only way. I tried this and I had a few problems.

  • Comments are converted to quotes and appended to the end of the document.
  • If someone edits a document through a browser, any changes made by them between the time the API was read and the update are lost.
  • Document updates may break formatting. For example, I updated the document several times and the vertical distance between some elements (h1, h2, etc.) was constantly expanding and destroying the document.
  • When updating the API, the cursor of any of the documents moves to the top of the page.

There may be more problems. These are the ones that I found in the last few days.

+3
source share

UPDATE (May 2019) The Google Docs API was officially launched in February 2019. The documentation is located on the link from my July update below. A few weeks after the launch, I prepared a general overview of the video of what the merge application using the API will look like. (This is not a full episode of the G Suite Dev Show , but a link to a working sample.)

UPDATE (July 2018) The Google Docs team previously announced the upcoming REST API on Google Cloud NEXT '18. Developers interested in gaining access to the early access program for the new API should register at https://developers.google.com/docs . The original answer below remains valid as the REST API will be the second way you can programmatically access Google Docs.

Original answer (March 2017) : (Most of the other answers are deprecated.) Google Docs currently do not have a REST API, however developers can programmatically access documents (CRUDs) using a Google Apps script , server-side JavaScript applications that hosted and run in the Google Cloud. If you're new to application scripting or editing Google Docs with it, here are a few tutorial resources:

A simple example: if you have an existing Doc with the identifier (Drive) of the DOCUMENT_ID_GOES_HERE file, here is how you can basically edit it using the Apps script by doing the pseudo "mail merge" name & send an email to the document with the indicated 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"); } 
+3
source share

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

You can get the Google document as a docx (for example), edit it using your favorite docx editor, and then download again. See samples for this (starting with GoogleDriveDownloadAsDocx ) in the context of docx4j. Pay attention to README .

Or do the same with any other export format.

+1
source share

(2019) Google now provides an API for documents, slides, sheets, disks.

+1
source share

There is an example application for this, Dr. Edit in the Google Drive documentation.

0
source share

All Articles