How to hotlink images in DOC file using Google API?
P粉449281068
P粉449281068 2023-07-21 13:47:02
0
1
641

I created a new document file on Google Docs, inserted an image, and (there's no option to insert a linked image, right?) needed to assign it a URL so that the image could be clicked.

$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']);

But I can't find the correct API function. There is a setLinkUrl method of class InlineImage, but how to get an instance of InlineImage?

Another way is to iterate over the document

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

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

But the printed content does not contain any useful information.

P粉449281068
P粉449281068

reply all(1)
P粉083785014

In the script you showed, a new document is created using the Docs API and the image is placed into the new document created. In this case, you can modify the request body as follows, using UpdateTextStyleRequest.

Example:

$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',
            ),
        )
    )
));

Example:

$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',
        ),
    )),
];
  • When using this modified request body, the hyperlink of https://www.google.com will be set to the image inserted in the Google document.

  • For example, if you want to retrieve startIndex and endIndex from a picture inserted in the document, you can use the following sample script:

    $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));
                  }
              }
          }
      }
    • In your script, the values ​​of $startIndex and $endIndex are 1 and 2 respectively. Please use these values ​​with the scope of updateTextStyle as shown in the modification above.
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template