javagoogle-docs-apigoogle-api-java-client

How to insert text to tablecell google doc api with java client libary


i want insert text to table 2x3, when insert text with new rows i see strange behavior. How to calculate correct index to insert content in tablecell, or document for that?

List<Request> requests = new ArrayList<>();
        requests.add(new Request().setInsertTable(new InsertTableRequest()
                .setLocation(new Location().setIndex(1))
                .setRows(2)
                .setColumns(3)));

        requests.add(new Request().setInsertText(new InsertTextRequest()
                .setText("A1")
                .setLocation(new Location().setIndex(5))));

        requests.add(new Request().setInsertText(new InsertTextRequest()
                .setText("B1")
                .setLocation(new Location().setIndex(9))));

        requests.add(new Request().setInsertText(new InsertTextRequest()
                .setText("c1")
                .setLocation(new Location().setIndex(13))));

        requests.add(new Request().setInsertText(new InsertTextRequest()
                .setText("c2")
                .setLocation(new Location().setIndex(20))));

        requests.add(new Request().setInsertText(new InsertTextRequest()
                .setText("c3")
                .setLocation(new Location().setIndex(24))));

        BatchUpdateDocumentRequest body = new BatchUpdateDocumentRequest().setRequests(requests);
        BatchUpdateDocumentResponse response = service.documents().batchUpdate(docId, body).execute();

Result of code above

Result of code above

Expection

enter image description here


Solution

  • I find the answer for my question with below link, this is best sample for understand doc structure, i find all info needed and other behavior.

    https://developers.google.com/docs/api/samples/output-json

    enter image description here