插入内嵌图片
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
您可以使用 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
插入,其 InlineObjectElement
的长度为 1,其中 startIndex
是请求的位置。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-29。
[null,null,["最后更新时间 (UTC):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."]]