无法通过Docs API向新的Google文档添加文本 [英] Unable to add text to new Google Doc via Docs API

查看:80
本文介绍了无法通过Docs API向新的Google文档添加文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序正在创建文档,每个文档都有需要输入的文本.任何尝试调用InsertTextRequest的操作都会引发错误.

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索引的.

Set the index of the Location object to 1, since it's 1-indexed.

这篇关于无法通过Docs API向新的Google文档添加文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆