English 中文(简体)
How can developers edit a Google Doc programmatically? Is there a Docs API?
原标题:

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?

最佳回答

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

问题回答

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:

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}:

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");
}

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.

  1. Comments are converted into citations and added to end of document.
  2. If someone else is editing the doc via the browser any changes made by them between the API read and update time are lost.
  3. Updates to a doc can break the formatting. For example I updated a doc several times and the vertical spacing between some elements (h1 s, h2 s etc) kept widening each time and ruined the doc.
  4. When an API update occurs the cursor of anyone in the doc is moved to the top of the page.

There may be more issues. These are just the ones I have found in the last few days.

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 now provides API for docs, slides, sheets, drive.

There is a sample app for this, Dr. Edit, on Google Drive s documentation.





相关问题
get atom feed and display in html (google-api)

Can someone help me out on this. I have a problem with the return results from an google-api call I want to echo them back as html, but firefox keeps displaying a feed page. In IE, I get an error ...

Using Google App Engine With My Dreamhost Registered Domain

I have registered a domain example.com using dreamhost, and currently have a standard wordpress blog set up on www.example.com. I d like to have appengine.example.com point to my Google Appengine ...

google calendar java api

I have an object of CalendarEntry I know that http://www.google.com/calendar/feeds/example@gmail.com/allcalendars/full is the feed url of all calendars but how I can get this feed url from ...

Google API for Internationalization + asp.net

Hai guys, Is there any google api for Internationalization with asp.net or any other open source api for it because i want my users to view my site in multiple indian languages like tamil,malayalam,...

google API translate, only a div into page

I have an HTML page, and I would use Google Translate to translate only a div into my page. <div id="google_translate_element"></div><script> function googleTranslateElementInit() { ...

How to use doctest on a Client script?

I am playing with Google Calendar API, creating some useful function. I another hand, I want to do it right puting some useful doctest and starting agile development. How to write doctest since the ...

using google.loader.ClientLocation

what will be best way to use using google.loader.ClientLocation to get client country & city name in jquery Thanks

热门标签