แทรกรูปภาพในบรรทัด
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
คุณแทรกรูปภาพลงในเอกสารได้โดยใช้เมธอด
InsertInlineImageRequest
คุณจะใช้ช่อง objectSize
เพื่อปรับขนาดรูปภาพก็ได้
รูปภาพต้องเข้าถึงได้แบบสาธารณะผ่าน URL ที่คุณระบุในวิธีนี้
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 Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-29 UTC
[null,null,["อัปเดตล่าสุด 2025-08-29 UTC"],[],[],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."]]