Search code examples
phpgoogle-apigoogle-docsgoogle-docs-api

How we can insert multiple paragraphs after the insertion of table in google docs using google docs api with PHP?


I am working on google docs api.I want to create a google docs with google docs api using PHP.AS per my requirement I want some paragraphs then insert a table and then again a paragraph as shown in below picture- enter image description here

I am able to insert texts like this -

$requests[] = new Google_Service_Docs_Request(array(
            'insertText' => [
                'text' => 'Paragraph 1',
                'location' => [
                    'index' => 1,
                ]
            ]
        ));

I am also able to add table like this-

$requests [] = new Google_Service_Docs_Request([
    'insertTable' => [
        'rows' =>  4,
        'columns' =>  4,
        'endOfSegmentLocation' => [
          'segmentId' => ''
        ],
    ],
]);

but after adding this table I want to add another paragraph as shown in pic.I have added another insertion text request after this table request like this -

$requests[] = new Google_Service_Docs_Request(array(
            'insertText' => array(
                'text' => "Paragraph 2", 
                'endOfSegmentLocation' => array(
                    'segmentId' => '',
                    ),
            ),
        ));

But like this i want to add more paragraphs after the insertion of table and for that I need index location but I am not able to do that. Can anybody help me on this how to add contents like this as shown in above pic.It would be a great huge help if anyone can help me through this.I want this in PHP.


Solution

  • In your situation, I thought that when the contents are inserted with the reverse order, index can be ignored. In this case, how about the following sample script?

    Sample script 1:

    $documentId = "###"; // Please set the Google Document ID.
    
    $requests = [
        new Google_Service_Docs_Request(array(
            'insertText' => [
                'text' => 'Paragraph 1',
                'location' => [
                    'index' => 1,
                ]
            ]
        )),
        new Google_Service_Docs_Request([
            'insertTable' => [
                'rows' =>  4,
                'columns' =>  4,
                'location' => [
                  'index' => 1
                ],
            ],
        ]),
        new Google_Service_Docs_Request(array(
            'insertText' => [
                'text' => 'Paragraph 2',
                'location' => [
                    'index' => 1,
                ]
            ]
        ))
    ];
    $batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(
        'requests' => array_reverse($requests)
    ));
    $responses = $service->documents->batchUpdate($documentId, $batchUpdateRequest);
    

    Sample script 2:

    Or, if you want to append the text of "Paragraph 2" after the text "Paragraph 1" and a table were inserted, you can append the text using the following script.

    $requests = [
        new Google_Service_Docs_Request(array(
            'insertText' => [
                'text' => "Paragraph 2", // or "\nParagraph 2"
                'endOfSegmentLocation' => [
                    'segmentId' => ''
                ],
            ]
        )),
    ];
    $batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(
        'requests' => array_reverse($requests)
    ));
    $responses = $service->documents->batchUpdate($documentId, $batchUpdateRequest);
    
    • By this request, Paragraph 2 can be appended to the document body. Also, In this case, index for putting the text can be ignored.
    • About 'text' => "Paragraph 2", when the paragraph is the same with the last paragraph, you can create new paragraph by 'text' => "\nParagraph 2".

    Sample script 3:

    If you want to insert the text by retrieving the index, you can also the following sample script.

    $documentId = "###"; // Please set the Google Document ID.
    
    $requests = [
        new Google_Service_Docs_Request(array(
            'insertText' => [
                'text' => 'Paragraph 1',
                'location' => [
                    'index' => 1,
                ]
            ]
        )),
        new Google_Service_Docs_Request([
            'insertTable' => [
                'rows' =>  4,
                'columns' =>  4,
                'endOfSegmentLocation' => [
                    'segmentId' => ''
                ],
            ],
        ]),
    ];
    $batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(
        'requests' => array_reverse($requests)
    ));
    $responses = $service->documents->batchUpdate($documentId, $batchUpdateRequest);
    
    $obj = $service->documents->get($documentId);
    $content = $obj->getBody()->getContent();
    $requests = [
        new Google_Service_Docs_Request(array(
            'insertText' => [
                'text' => 'Paragraph 2',
                'location' => [
                    'index' => end($content)->getEndIndex() - 1,
                ]
            ]
        ))
    ];
    $batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(
        'requests' => $requests
    ));
    $responses = $service->documents->batchUpdate($documentId, $batchUpdateRequest);
    

    References: