[英]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-我正在处理 google 文档 api。我想使用 PHP 创建一个带有 google 文档 api 的 google 文档。根据我的要求,我想要一些段落,然后插入一个表格,然后再插入一个段落,如下图所示-
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.任何人都可以帮助我如何添加如上图所示的内容。如果有人可以帮助我解决这个问题,那将是一个巨大的帮助。我想要这个在 PHP。
In your situation, I thought that when the contents are inserted with the reverse order, index
can be ignored.在你的情况下,我认为当内容以相反的顺序插入时, index
可以被忽略。 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.或者,如果要在插入文本“第 1 段”和表格后插入“第 2 段”的文本 append,则可以使用以下脚本插入 append 文本。
$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.应此要求,可将Paragraph 2
附加到文件正文中。 Also, In this case, index
for putting the text can be ignored.此外,在这种情况下,可以忽略用于放置文本的index
。'text' => "Paragraph 2"
, when the paragraph is the same with the last paragraph, you can create new paragraph by 'text' => "\nParagraph 2"
.关于'text' => "Paragraph 2"
,当段落与上一段相同时,可以通过'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);
I don't have PHP in me so I can only provide you the request and process on how to insert paragraphs after the table.我手头没有PHP,所以只能给大家提供表格后插入段落的要求和流程。
After inserting your table, you need to get
the document's last endIndex
by using get
request and pass body/content/endIndex
as your field parameter values.插入表格后,您需要使用get
请求get
文档的最后一个endIndex
,并将body/content/endIndex
作为您的字段参数值传递。 This will return this response from using the above sample doc:这将从使用上述示例文档返回此响应:
{
"body": {
"content": [
{
"endIndex": 1
},
{
"endIndex": 576
},
{
"endIndex": 614
},
{
"endIndex": 615
}
]
}
}
You have to iterate all the endIndex
from the response and get the last one (eg 615
).您必须从响应中迭代所有endIndex
并获取最后一个(例如615
)。 Subtract 1
from the last endIndex
value and then pass it as index location in the next insert paragraph response.从最后一个endIndex
值中减去1
,然后将其作为下一个插入段落响应中的索引位置传递。
{
"requests": [
{
"insertText": {
"location": {
"index": 614
},
"text": "\nLorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.\n"
}
}
]
}
\n
at both ends to have some space.我在两端用\n
将文本括起来以留出一些空间。get
the last endIndex
everytime.您还可以将所有段落 append 放入一个请求中,这样您就不必每次都get
最后一个endIndex
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.