4
votes

I have a document (native Google doc) in Google Drive that I need to update from my Java app. What I did so far is to download the doc as html

String downloadUrl = doc.getExportLinks().get("text/html");
HttpResponse resp = service.getRequestFactory()
                           .buildGetRequest(new GenericUrl(downloadUrl))
                           .execute();
String contents = IOUtils.toString(resp.getContent());

Then I update the contents in the String object and send the update to Drive:

ByteArrayContent mediaContent = ByteArrayContent.fromString("text/html", contents);
service.files().update(doc.getId(), doc, mediaContent).execute();

This works fine for very simple documents. But if the document contains an image, it disappears. The src attribute of the img tag is empty.

Does Google provide some other methods of updating the contents of a Google Document? Is there an API similar to the Google Spreadsheet API?

1
@IwishIcouldthinkofagood, thanks for the link but I am not trying to update Microsoft Document but native Google Drive document. - Martin Dimitrov
sorry I was focusing on the Apache POI part - Scary Wombat

1 Answers

1
votes

Unfortunately Google doesn't provide a fine-grained REST API for manipulating the contents of a Google Docs document. Google Apps Script provides a service that does this, but it may be difficult to integrate it into your Java application.