插入內嵌圖片
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
您可以使用 InsertInlineImageRequest
方法,在文件中插入圖片。您也可以使用 objectSize
欄位調整圖片大小。
Java
List<Request> requests = new ArrayList<>();
requests.add(new Request().setInsertInlineImage(new InsertInlineImageRequest()
.setUri("https://fonts.gstatic.com/s/i/productlogos/docs_2020q4/v6/web-64dp/logo_docs_2020q4_color_1x_web_64dp.png")
.setLocation(new Location().setIndex(1).setTabId(TAB_ID))
.setObjectSize(new Size()
.setHeight(new Dimension()
.setMagnitude(50.0)
.setUnit("PT"))
.setWidth(new Dimension()
.setMagnitude(50.0)
.setUnit("PT")))));
BatchUpdateDocumentRequest body = new BatchUpdateDocumentRequest().setRequests(requests);
BatchUpdateDocumentResponse response = docsService.documents()
.batchUpdate(DOCUMENT_ID, body).execute();
PHP
$requests = array();
$requests[] = new Google_Service_Docs_Request(array(
'insertInlineImage' => array(
'uri' => 'https://fonts.gstatic.com/s/i/productlogos/docs_2020q4/v6/web-64dp/logo_docs_2020q4_color_1x_web_64dp.png',
'location' => array(
'index' => 1,
'tabId' => TAB_ID,
),
'objectSize' => array(
'height' => array(
'magnitude' => 50,
'unit' => 'PT',
),
'width' => array(
'magnitude' => 50,
'unit' => 'PT',
),
)
)
));
// Execute the requests.
$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(
'requests' => $requests
));
$response =
$docsService->documents->batchUpdate(DOCUMENT_ID, $batchUpdateRequest);
Python
requests = [{
'insertInlineImage': {
'location': {
'index': 1,
'tabId': TAB_ID
},
'uri':
'https://fonts.gstatic.com/s/i/productlogos/docs_2020q4/v6/web-64dp/logo_docs_2020q4_color_1x_web_64dp.png',
'objectSize': {
'height': {
'magnitude': 50,
'unit': 'PT'
},
'width': {
'magnitude': 50,
'unit': 'PT'
}
}
}
}]
# Execute the request.
body = {'requests': requests}
response = service.documents().batchUpdate(
documentId=document_id, body=body).execute()
insert_inline_image_response = response.get('replies')[0].get(
'insertInlineImage')
print('Inserted image with object ID: {0}'.format(
insert_inline_image_response.get('objectId')))
這個方法會將圖片插入為新的 ParagraphElement
,並使用長度為 1 的 InlineObjectElement
,其中 startIndex
是要求的所在位置。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-29 (世界標準時間)。
[null,null,["上次更新時間:2025-08-29 (世界標準時間)。"],[],[],null,["# Insert inline images\n\nYou can insert an image into a document using the\n[`InsertInlineImageRequest`](/workspace/docs/api/reference/rest/v1/documents/request#insertinlineimagerequest)\nmethod. You can optionally use the `objectSize` field to resize the image.\nThe image must be publicly accessible through the URL that you provide in this method. \n\n### Java\n\n```java\nList\u003cRequest\u003e requests = new ArrayList\u003c\u003e();\nrequests.add(new Request().setInsertInlineImage(new InsertInlineImageRequest()\n .setUri(\"https://fonts.gstatic.com/s/i/productlogos/docs_2020q4/v6/web-64dp/logo_docs_2020q4_color_1x_web_64dp.png\")\n .setLocation(new Location().setIndex(1).setTabId(TAB_ID))\n .setObjectSize(new Size()\n .setHeight(new Dimension()\n .setMagnitude(50.0)\n .setUnit(\"PT\"))\n .setWidth(new Dimension()\n .setMagnitude(50.0)\n .setUnit(\"PT\")))));\n\nBatchUpdateDocumentRequest body = new BatchUpdateDocumentRequest().setRequests(requests);\nBatchUpdateDocumentResponse response = docsService.documents()\n .batchUpdate(DOCUMENT_ID, body).execute();\n```\n\n### PHP\n\n```php\n$requests = array();\n$requests[] = new Google_Service_Docs_Request(array(\n 'insertInlineImage' =\u003e array(\n 'uri' =\u003e 'https://fonts.gstatic.com/s/i/productlogos/docs_2020q4/v6/web-64dp/logo_docs_2020q4_color_1x_web_64dp.png',\n 'location' =\u003e array(\n 'index' =\u003e 1,\n 'tabId' =\u003e TAB_ID,\n ),\n 'objectSize' =\u003e array(\n 'height' =\u003e array(\n 'magnitude' =\u003e 50,\n 'unit' =\u003e 'PT',\n ),\n 'width' =\u003e array(\n 'magnitude' =\u003e 50,\n 'unit' =\u003e 'PT',\n ),\n )\n )\n));\n\n// Execute the requests.\n$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(\n 'requests' =\u003e $requests\n));\n$response =\n $docsService-\u003edocuments-\u003ebatchUpdate(DOCUMENT_ID, $batchUpdateRequest);\n```\n\n### Python\n\n```python\nrequests = [{\n 'insertInlineImage': {\n 'location': {\n 'index': 1,\n 'tabId': TAB_ID\n },\n 'uri':\n 'https://fonts.gstatic.com/s/i/productlogos/docs_2020q4/v6/web-64dp/logo_docs_2020q4_color_1x_web_64dp.png',\n 'objectSize': {\n 'height': {\n 'magnitude': 50,\n 'unit': 'PT'\n },\n 'width': {\n 'magnitude': 50,\n 'unit': 'PT'\n }\n }\n }\n}]\n\n# Execute the request.\nbody = {'requests': requests}\nresponse = service.documents().batchUpdate(\n documentId=document_id, body=body).execute()\ninsert_inline_image_response = response.get('replies')[0].get(\n 'insertInlineImage')\nprint('Inserted image with object ID: {0}'.format(\n insert_inline_image_response.get('objectId')))\n```\n\nThe method inserts the image as a new\n[`ParagraphElement`](/workspace/docs/api/reference/rest/v1/documents#paragraphelement)\nwith an\n[`InlineObjectElement`](/workspace/docs/api/reference/rest/v1/documents#InlineObjectElement)\nof length 1, where the `startIndex` is the request's location."]]