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-
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.
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?
$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);
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);
Paragraph 2
can be appended to the document body. Also, In this case, index
for putting the text can be ignored.'text' => "Paragraph 2"
, when the paragraph is the same with the last paragraph, you can create new paragraph by 'text' => "\nParagraph 2"
.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);