我们如何使用 google docs api 和 PHP 在同一行中对齐图像和文本?
How can we align an image and texts in same single line using google docs api with PHP?
我正在使用 PHP 代码编写 google 文档 api。我想在同一个 line.Is 中插入图像和文本,可以使用 google 文档 api 来做到这一点吗?
我可以插入图片和文本,但不能单独插入 line.Is 有什么办法吗?
用于插入文本-
'insertText' => [
'text' => "hello",
'location' => [
'index' => 1,
]
]
以及插入图片-
"insertInlineImage" => [
"location" => [
"index" => 1
],
"uri" => "https://www.gstatic.com/images/branding/product/1x/docs_64dp.png",
"objectSize" => [
"height"=> [
"magnitude"=> 10,
"unit" => "PT"
],
"width"=> [
"magnitude"=> 10,
"unit"=> "PT"
]
]
]
现在,我注意到您的问题已更新。不幸的是,从您更新的问题来看,我认为在当前阶段,您的目标无法直接实现。因此,在这个更新的答案中,作为一种解决方法,如何使用 table 如下?
示例脚本:
$b = ["color" => ["color" => []], "dashStyle" => "SOLID", "width" => ["magnitude" => 0, "unit" => "PT"]];
$requests = [
new Google_Service_Docs_Request([
'insertTable' => [
'location' => ['index' => 1],
'columns' => 2,
'rows' => 1
]
]),
new Google_Service_Docs_Request([
"updateTableCellStyle" => [
"tableCellStyle" => [
"borderBottom" => $b,
"borderTop" => $b,
"borderLeft" => $b,
"borderRight" => $b,
],
"tableStartLocation" => ["index" => 2],
"fields" => "borderBottom,borderTop,borderLeft,borderRight"
]
]),
new Google_Service_Docs_Request([
"insertInlineImage" => [
"location" => [
"index" => 5
],
"uri" => "https://www.gstatic.com/images/branding/product/1x/docs_64dp.png",
"objectSize" => [
"height"=> [
"magnitude"=> 100,
"unit" => "PT"
],
"width"=> [
"magnitude"=> 100,
"unit"=> "PT"
]
]
]]),
new Google_Service_Docs_Request([
'insertText' => [
'text' => "hello1\nhello2\nhello3",
'location' => [
'index' => 8,
]
]]),
new Google_Service_Docs_Request([
"updateParagraphStyle" => [
"paragraphStyle" => [
"alignment" => "START"
],
"range" => [
"startIndex" => 5,
"endIndex" => 5,
],
"fields" => "alignment"
]]),
new Google_Service_Docs_Request([
"updateParagraphStyle" => [
"paragraphStyle" => [
"alignment" => "END"
],
"range" => [
"startIndex" => 8,
"endIndex" => 23,
],
"fields" => "alignment"
]]),
];
$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array('requests' => $requests));
$responses = $service->documents->batchUpdate($documentId, $batchUpdateRequest);
- 当此脚本为 运行 时,将创建 1 行 2 列的 table 并删除 table 边框。并且,图像和文本分别放在“A”和“B”列中。然后,每个单元格对齐。
注:
- 在此示例中,我使用了 1 行 2 列的 table。如果你想修改这个对齐方式,请根据你的实际情况修改上面的脚本。
已添加:
来自OP的后续回复,
But as you see in my picture I have shown it in my header and from your script what I have to change to make it on my header and for your information I am able to get my header id. Just help me to modify your answer to place that table and image in my header.
不幸的是,我没有注意到你想把它放在你的问题和显示脚本中的 header 中。在这种情况下,一个简单的修改反映在上面的脚本中。请在每个请求中添加 segmentId
,如下所示。
示例脚本:
$headerId = "kix.###"; // Please set your header ID.
$b = ["color" => ["color" => []], "dashStyle" => "SOLID", "width" => ["magnitude" => 0, "unit" => "PT"]];
$requests = [
new Google_Service_Docs_Request([
'insertTable' => [
'location' => ['index' => 1, "segmentId" => $headerId],
'columns' => 2,
'rows' => 1
]
]),
new Google_Service_Docs_Request([
"updateTableCellStyle" => [
"tableCellStyle" => [
"borderBottom" => $b,
"borderTop" => $b,
"borderLeft" => $b,
"borderRight" => $b,
],
"tableStartLocation" => ["index" => 2, "segmentId" => $headerId],
"fields" => "borderBottom,borderTop,borderLeft,borderRight"
]
]),
new Google_Service_Docs_Request([
"insertInlineImage" => [
"location" => ["index" => 5, "segmentId" => $headerId],
"uri" => "https://www.gstatic.com/images/branding/product/1x/docs_64dp.png",
"objectSize" => [
"height"=> [
"magnitude"=> 100,
"unit" => "PT"
],
"width"=> [
"magnitude"=> 100,
"unit"=> "PT"
]
]
]]),
new Google_Service_Docs_Request([
'insertText' => [
'text' => "hello1\nhello2\nhello3",
'location' => ['index' => 8, "segmentId" => $headerId]
]]),
new Google_Service_Docs_Request([
"updateParagraphStyle" => [
"paragraphStyle" => [
"alignment" => "START"
],
"range" => [
"startIndex" => 5,
"endIndex" => 5,
"segmentId" => $headerId
],
"fields" => "alignment"
]]),
new Google_Service_Docs_Request([
"updateParagraphStyle" => [
"paragraphStyle" => [
"alignment" => "END"
],
"range" => [
"startIndex" => 8,
"endIndex" => 23,
"segmentId" => $headerId
],
"fields" => "alignment"
]]),
];
$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array('requests' => $requests));
$responses = $service->documents->batchUpdate($documentId, $batchUpdateRequest);
注:
- 如果您使用第一个 header ID 的 header ID,请在 Google 文档中勾选使用第一个 header 的复选框。
用于插入文本-
'insertText' => [
'text' => "hello",
'location' => [
'index' => 1,
]
]
以及插入图片-
"insertInlineImage" => [
"location" => [
"index" => 1
],
"uri" => "https://www.gstatic.com/images/branding/product/1x/docs_64dp.png",
"objectSize" => [
"height"=> [
"magnitude"=> 10,
"unit" => "PT"
],
"width"=> [
"magnitude"=> 10,
"unit"=> "PT"
]
]
]
现在,我注意到您的问题已更新。不幸的是,从您更新的问题来看,我认为在当前阶段,您的目标无法直接实现。因此,在这个更新的答案中,作为一种解决方法,如何使用 table 如下?
示例脚本:
$b = ["color" => ["color" => []], "dashStyle" => "SOLID", "width" => ["magnitude" => 0, "unit" => "PT"]];
$requests = [
new Google_Service_Docs_Request([
'insertTable' => [
'location' => ['index' => 1],
'columns' => 2,
'rows' => 1
]
]),
new Google_Service_Docs_Request([
"updateTableCellStyle" => [
"tableCellStyle" => [
"borderBottom" => $b,
"borderTop" => $b,
"borderLeft" => $b,
"borderRight" => $b,
],
"tableStartLocation" => ["index" => 2],
"fields" => "borderBottom,borderTop,borderLeft,borderRight"
]
]),
new Google_Service_Docs_Request([
"insertInlineImage" => [
"location" => [
"index" => 5
],
"uri" => "https://www.gstatic.com/images/branding/product/1x/docs_64dp.png",
"objectSize" => [
"height"=> [
"magnitude"=> 100,
"unit" => "PT"
],
"width"=> [
"magnitude"=> 100,
"unit"=> "PT"
]
]
]]),
new Google_Service_Docs_Request([
'insertText' => [
'text' => "hello1\nhello2\nhello3",
'location' => [
'index' => 8,
]
]]),
new Google_Service_Docs_Request([
"updateParagraphStyle" => [
"paragraphStyle" => [
"alignment" => "START"
],
"range" => [
"startIndex" => 5,
"endIndex" => 5,
],
"fields" => "alignment"
]]),
new Google_Service_Docs_Request([
"updateParagraphStyle" => [
"paragraphStyle" => [
"alignment" => "END"
],
"range" => [
"startIndex" => 8,
"endIndex" => 23,
],
"fields" => "alignment"
]]),
];
$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array('requests' => $requests));
$responses = $service->documents->batchUpdate($documentId, $batchUpdateRequest);
- 当此脚本为 运行 时,将创建 1 行 2 列的 table 并删除 table 边框。并且,图像和文本分别放在“A”和“B”列中。然后,每个单元格对齐。
注:
- 在此示例中,我使用了 1 行 2 列的 table。如果你想修改这个对齐方式,请根据你的实际情况修改上面的脚本。
已添加:
来自OP的后续回复,
But as you see in my picture I have shown it in my header and from your script what I have to change to make it on my header and for your information I am able to get my header id. Just help me to modify your answer to place that table and image in my header.
不幸的是,我没有注意到你想把它放在你的问题和显示脚本中的 header 中。在这种情况下,一个简单的修改反映在上面的脚本中。请在每个请求中添加 segmentId
,如下所示。
示例脚本:
$headerId = "kix.###"; // Please set your header ID.
$b = ["color" => ["color" => []], "dashStyle" => "SOLID", "width" => ["magnitude" => 0, "unit" => "PT"]];
$requests = [
new Google_Service_Docs_Request([
'insertTable' => [
'location' => ['index' => 1, "segmentId" => $headerId],
'columns' => 2,
'rows' => 1
]
]),
new Google_Service_Docs_Request([
"updateTableCellStyle" => [
"tableCellStyle" => [
"borderBottom" => $b,
"borderTop" => $b,
"borderLeft" => $b,
"borderRight" => $b,
],
"tableStartLocation" => ["index" => 2, "segmentId" => $headerId],
"fields" => "borderBottom,borderTop,borderLeft,borderRight"
]
]),
new Google_Service_Docs_Request([
"insertInlineImage" => [
"location" => ["index" => 5, "segmentId" => $headerId],
"uri" => "https://www.gstatic.com/images/branding/product/1x/docs_64dp.png",
"objectSize" => [
"height"=> [
"magnitude"=> 100,
"unit" => "PT"
],
"width"=> [
"magnitude"=> 100,
"unit"=> "PT"
]
]
]]),
new Google_Service_Docs_Request([
'insertText' => [
'text' => "hello1\nhello2\nhello3",
'location' => ['index' => 8, "segmentId" => $headerId]
]]),
new Google_Service_Docs_Request([
"updateParagraphStyle" => [
"paragraphStyle" => [
"alignment" => "START"
],
"range" => [
"startIndex" => 5,
"endIndex" => 5,
"segmentId" => $headerId
],
"fields" => "alignment"
]]),
new Google_Service_Docs_Request([
"updateParagraphStyle" => [
"paragraphStyle" => [
"alignment" => "END"
],
"range" => [
"startIndex" => 8,
"endIndex" => 23,
"segmentId" => $headerId
],
"fields" => "alignment"
]]),
];
$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array('requests' => $requests));
$responses = $service->documents->batchUpdate($documentId, $batchUpdateRequest);
注:
- 如果您使用第一个 header ID 的 header ID,请在 Google 文档中勾选使用第一个 header 的复选框。