我正在做google docs api,我想用google docs根据我的要求使用PHP.AS创建一个google文档,我想要一些段落,然后插入一个表,然后再插入一个段落,如下图所示-
我能插入这样的文字-
$requests[] = new Google_Service_Docs_Request(array(
'insertText' => [
'text' => 'Paragraph 1',
'location' => [
'index' => 1,
]
]
));
我还能添加这样的桌子-
$requests [] = new Google_Service_Docs_Request([
'insertTable' => [
'rows' => 4,
'columns' => 4,
'endOfSegmentLocation' => [
'segmentId' => ''
],
],
]);
但是在添加这个表之后,我想添加另一个段落,如图所示,我在这个表请求之后添加了另一个插入文本请求,如下所示-
$requests[] = new Google_Service_Docs_Request(array(
'insertText' => array(
'text' => "Paragraph 2",
'endOfSegmentLocation' => array(
'segmentId' => '',
),
),
));
但是像这样,我想在插入表之后添加更多的段落,为此,我需要索引位置,但我无法做到这一点。有人能帮我吗?如果有人能帮我解决这个问题的话,怎么添加像上面所示的pic.It这样的内容将是一个很大的帮助。
发布于 2022-02-11 01:09:49
在您的情况下,我认为当内容以相反的顺序插入时,可以忽略index
。在本例中,下面的示例脚本如何?
示例脚本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);
示例脚本2:
或者,如果您想在文本“第1段”之后添加“第2段”的文本并插入一个表,则可以使用以下脚本追加文本。
$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
附加到文档主体。此外,在这种情况下,用于放置文本的index
可以被忽略。'text' => "Paragraph 2"
,当段落与最后一段相同时,您可以通过'text' => "\nParagraph 2"
创建新的段落。示例脚本3:
如果要通过检索索引插入文本,还可以使用以下示例脚本。
$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);
参考文献:
发布于 2022-02-10 16:31:11
我没有PHP,所以我只能向您提供如何在表后插入段落的请求和处理。
医生样本:
插入表后,需要使用get
请求并将body/content/endIndex
作为字段参数值传递给文档的最后一个endIndex
。这将从使用上面的示例文档返回此响应:
{
"body": {
"content": [
{
"endIndex": 1
},
{
"endIndex": 576
},
{
"endIndex": 614
},
{
"endIndex": 615
}
]
}
}
您必须从响应中迭代所有endIndex
并获得最后一个(例如,615
)。从最后一个1
值中减去endIndex
,然后在下一个插入段落响应中将其作为索引位置传递。
抽样请求:
{
"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
的文本,以便有一些空间。get
最后一个endIndex
了。https://stackoverflow.com/questions/71067581
复制相似问题