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.
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:
Example:
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: