如何使用Google API在DOC檔案中熱連結圖像?
P粉449281068
P粉449281068 2023-07-21 13:47:02
0
1
520

我在Google Docs上建立了一個新的文檔文件,插入了一張圖片,並且(沒有插入連結圖片的選項,對嗎?)需要為它分配一個URL,以便圖片可以被點擊。

$docs_service = new Google_Service_Docs($client);
$drive_service = new Google_Service_Drive($client); 

$document = new Google_Service_Docs_Document(array(
    'title' => $file_name
));
$document = $docs_service->documents->create($document);   

$requests[] =
    new Google_Service_Docs_Request(array(
        'insertText' => array(
            'location' => array(
                'index' => 1,
            ),
            'text' => "n".$text
        )
    ));

$requests[] = new Google_Service_Docs_Request(array(
    'insertInlineImage' => array(
        'uri' => 'https://example.com/img.jpg',
        'location' => array(
            'index' => 1,
        ),
        'objectSize' => array(
            'height' => array(
                'magnitude' => 675,
                'unit' => 'PT',
            ),
            'width' => array(
                'magnitude' => 360,
                'unit' => 'PT',
            ),
        )
    )
));

$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(
    'requests' => $requests
));
$response = $docs_service->documents->batchUpdate($document->getDocumentId(), $batchUpdateRequest);

$doc = $docs_service->documents->get($document->getDocumentId(), ['fields' => 'body']);

但是我找不到正確的API函數。有一個類別InlineImage的setLinkUrl方法,但是如何取得InlineImage的實例呢?

另一種方法是迭代文件

$doc = $docs_service->documents->get($document->getDocumentId(), ['fields' => 'body']);

foreach ($doc->body->content as $content) {
  print_r($content);
}

但是列印出來的內容沒有任何有用的信息。

P粉449281068
P粉449281068

全部回覆(1)
P粉083785014

在您展示的腳本中,使用Docs API建立了一個新的文檔,並將圖片放入了已建立的新文檔中。在這種情況下,您可以將請求正文修改如下,使用UpdateTextStyleRequest。

範例:

$requests[] = new Google_Service_Docs_Request(array(
    'insertInlineImage' => array(
        'uri' => 'https://example.com/img.jpg',
        'location' => array(
            'index' => 1,
        ),
        'objectSize' => array(
            'height' => array(
                'magnitude' => 675,
                'unit' => 'PT',
            ),
            'width' => array(
                'magnitude' => 360,
                'unit' => 'PT',
            ),
        )
    )
));

範例:

$requests = [
    new Google_Service_Docs_Request(array(
        'insertInlineImage' => array(
            'uri' => 'https://example.com/img.jpg',
            'location' => array(
                'index' => 1,
            ),
            'objectSize' => array(
                'height' => array(
                    'magnitude' => 675,
                    'unit' => 'PT',
                ),
                'width' => array(
                    'magnitude' => 360,
                    'unit' => 'PT',
                ),
            )
        ),
    )),
    new Google_Service_Docs_Request(array(
        'updateTextStyle' => array(
            'range' => array(
                'startIndex' => 1,
                'endIndex' => 2,
            ),
            'textStyle' => array(
                'link' => array(
                    'url' => 'https://www.google.com', // Please set your URL.
                ),
            ),
            'fields' => 'link',
        ),
    )),
];
  • 使用這個修改後的請求正文時, https://www.google.com 的超連結將會設定在Google文件中插入的圖片上。

  • 例如,如果您想從文件中插入的圖片中檢索startIndex和endIndex,可以使用以下範例腳本:

    $doc = $docs_service->documents->get($document->getDocumentId(), ['fields' => 'body']);
      foreach ($doc->body->content as $content) {
          if (array_key_exists('paragraph', $content)) {
              foreach ($content->paragraph->elements as $element) {
                  if (array_key_exists('inlineObjectElement', $element)) {
                      $startIndex = $element->startIndex;
                      $endIndex = $element->endIndex;
                      print_r(array($startIndex, $endIndex));
                  }
              }
          }
      }
    • 在您的腳本中,$startIndex和$endIndex的值分別為1和2。請將這些值與updateTextStyle的範圍一起使用,如上述修改所示。
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!