5

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?

1 Answer 1

14

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

Sign up to request clarification or add additional context in comments.

6 Comments

Figured this out minutes after posting the question. The fact it's 1-indexed is not mentioned anywhere on the API documentation.
I guess it's just the 3 of us who have ever used the Google API aside from the internal team. Haha. :D
Whats really annoying is the documentation says that it is 0 based developers.google.com/docs/api/reference/rest/v1/documents/…
I guess I am the fourth one.
Google Docs is actually 0-based, but the first item in a document is always the first section break which you can hang section info onto. So when adding content, you are adding it at index 1 inside the first section. Agreed that this is not clear in the docs.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.