java - Unable to add text to new Google Doc via Docs API
问题描述
My program is creating documents and each document has text that needs to go into it. Any attempt to call an InsertTextRequest
invokes an error.
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"
}
Even trying to add a newline character before adding the text does not fix this issue.
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();
This also invokes the same error. How can I properly add text?
解决方案
将Location
对象的索引设置为 1,因为它是 1 索引的。
推荐阅读
- python - 调整窗口的所有内容?
- flutter - 如何在颤动中测试 showDialog 内容?
- unix - 如何从压缩文件列表 (.gz) 中 grep 一个文件的内容
- r - 如何用另一列中的值替换不同列中的值?(右)
- python - 重复将数据附加到 CSV
- ruby-on-rails - 如何在 Rails 中将 safe_html 放入 button_to
- arrays - 如何在 PowerShell 函数中返回 ArrayList 而索引不成为返回值的一部分?
- angular - 角度垫分隔器垂直宽度未调整
- javascript - 如何将箭头指向 Aframe 中的对象?
- javascript - 使用 Javascript 将可编辑内容从 true 更改为 false