gpt4 book ai didi

java - 无法通过 Docs API 向新的 Google Doc 添加文本

转载 作者:行者123 更新时间:2023-12-01 14:10:33 25 4
gpt4 key购买 nike

我的程序正在创建文档,每个文档都有需要放入其中的文本。任何调用 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();

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

最佳答案

设置Location的索引反对 1,因为它是 1 索引的。

关于java - 无法通过 Docs API 向新的 Google Doc 添加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57300083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com