无法通过 Docs API 将文本添加到新的 Google Doc

我的程序正在创建文档,每个文档都有需要放入其中的文本。任何调用 的尝试都会InsertTextRequest引发错误。


List<Request> requests = new ArrayList<>();


requests.add(new Request().setInsertText(new InsertTextRequest()

                .setText("Simple test.")

                .setLocation(new Location().setIndex(0))));


BatchUpdateDocumentRequest body = new BatchUpdateDocumentRequest()

                .setRequests(requests);


BatchUpdateDocumentResponse response = docService.documents()

                .batchUpdate(file.getId(), body).execute();

Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request

{

  "code" : 400,

  "errors" : [ {

    "domain" : "global",

    "message" : "Invalid requests[0].insertText: The insertion index must be inside the bounds of an existing paragraph. You can still create new paragraphs by inserting newlines.",

    "reason" : "badRequest"

  } ],

  "message" : "Invalid requests[0].insertText: The insertion index must be inside the bounds of an existing paragraph. You can still create new paragraphs by inserting newlines.",

  "status" : "INVALID_ARGUMENT"

}

即使尝试在添加文本之前添加换行符也无法解决此问题。


List<Request> requests = new ArrayList<>();


requests.add(new Request().setInsertText(new InsertTextRequest()

                .setText("\n")

                .setLocation(new Location().setIndex(0))));


requests.add(new Request().setInsertText(new InsertTextRequest()

                .setText("Simple test.")

                .setLocation(new Location().setIndex(0))));


BatchUpdateDocumentRequest body = new BatchUpdateDocumentRequest()

                .setRequests(requests);


BatchUpdateDocumentResponse response = docService.documents()

                .batchUpdate(file.getId(), body).execute();

这也会调用相同的错误。如何正确添加文本?


隔江千里
浏览 84回答 1
1回答

FFIVE

将对象的索引设置Location为 1,因为它是从 1 开始索引的。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java