使用 api google doc 在 header 中居中图像

Center an image in header with api google doc

我试图在我的 header 中插入一张图片。没关系。 现在我想将图片居中,但我卡住了。

$requests[] = new \Google_Service_Docs_Request(array(
        'insertInlineImage' => array(
            'uri' => 'https://myPicture.png',
            'location' => array(
                'segmentId' => $document->getDocumentStyle()->getDefaultHeaderId(),
                'index' => 0
            ),
        ),

    ));
  • 您想在 Google 文档中 header 的中心插入内联图像。
  • 您想使用 google-api-php-client 和 PHP 来实现此目的。
  • 您已经能够使用 Google 文档 API.
  • 获取和放置 Google 文档的值

如果我的理解是正确的,这个答案怎么样?请将此视为几个可能的答案之一。

修改点:

  • 为了让图片居中,请在batchUpdate的请求body中加入UpdateParagraphStyleRequest,因为InsertInlineImageRequest没有alignment.
  • 的属性

修改后的脚本:

$documentId = '###';  // Please set the Document ID.
$segmentId = $document->getDocumentStyle()->getDefaultHeaderId();

$service = new Google_Service_Docs($client);
$requests = [
    new Google_Service_Docs_Request([
        'insertInlineImage' => [
            'location' => ['index' => 0, 'segmentId' => $segmentId],
            'uri' => 'https://icir.int.demedicis.fr/img/logo/veolia.png'
        ]
    ]),
    new Google_Service_Docs_Request([
        'updateParagraphStyle' => [
            'range' => ['startIndex' => 0, 'endIndex' => 1, 'segmentId' => $segmentId],
            'paragraphStyle' => ['alignment' => 'CENTER'],
            'fields' => 'alignment'
        ]
    ])
];
$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(['requests' => $requests]);
$result = $service->documents->batchUpdate($documentId, $batchUpdateRequest);

注:

  • $document->getDocumentStyle()->getDefaultHeaderId()不能使用时,请将headerID设置为字符串值kix.###
  • 这是一个简单的修改。所以请将此反映到您的实际脚本中。

参考文献:

如果我误解了你的问题,这不是你想要的方向,我很抱歉。