简体   繁体   English

用Java替换Google文档中的文本

[英]Replacing text in Google document from Java

I have a document (native Google doc) in Google Drive that I need to update from my Java app. 我在Google云端硬盘中有一个文档(原生Google文档),我需要从我的Java应用程序更新。 What I did so far is to download the doc as html 到目前为止我所做的是将文档下载为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: 然后我更新String对象中的内容并将更新发送到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. img标记的src属性为空。

Does Google provide some other methods of updating the contents of a Google Document? Google是否提供了一些更新Google文档内容的方法? Is there an API similar to the Google Spreadsheet API ? 是否有类似于Google Spreadsheet API的API

Unfortunately Google doesn't provide a fine-grained REST API for manipulating the contents of a Google Docs document. 遗憾的是,Google没有提供细粒度的REST API来操纵Google Docs文档的内容。 Google Apps Script provides a service that does this, but it may be difficult to integrate it into your Java application. Google Apps脚本提供了执行此操作的服务,但可能难以将其集成到Java应用程序中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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