批次處理要求
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本文說明如何批次處理 API 呼叫,以減少用戶端必須建立的連線數量。批次處理可減少網路往返次數並提高總處理量,進而提升應用程式效率。
總覽
用戶端建立的每個連線都會造成一定程度的負擔。
Google Slides API 支援批次處理,可讓用戶端將多個要求物件放入單一批次要求,每個物件都會指定要執行的單一要求類型。批次要求可將多個子要求合併為單一伺服器呼叫,並擷取單一回應,藉此提升效能。
建議使用者一律批次處理多個要求。以下列舉幾個可使用批次作業的狀況範例:
-
您剛開始使用 API,且有大量資料要上傳。
-
您需要更新多個物件的中繼資料或屬性,例如格式。
- 您需要刪除大量物件。
限制、授權和依附關係考量
以下是採用批次更新時應考量的其他項目:
-
每個批次要求 (包括所有子要求) 都會計為一次 API 要求,並計入用量限制。
-
批次要求只需驗證一次。這項單一驗證會套用至要求中的所有批次更新物件。
-
伺服器會按照子要求在批次要求中的順序處理這些要求。後續子要求可能會視先前子要求期間採取的動作而定。舉例來說,使用者可以在同一個批次要求中,將文字插入現有文件,然後設定樣式。
批次詳細資料
批次要求包含一項 batchUpdate
方法呼叫,以及多項子要求,例如新增簡報並設定格式。
系統會先驗證每項要求,再套用要求。系統會以不可分割的形式套用批次更新中的所有子要求。也就是說,如果任何要求無效,整個更新就會失敗,且不會套用任何 (可能相依的) 變更。
部分要求會提供回應,其中包含所套用要求的相關資訊。
舉例來說,所有新增物件的批次更新要求都會傳回回應,因此您可以存取新加入物件的中繼資料,例如 ID 或標題。
採用這種做法時,您可以使用一個 API 批次更新要求,搭配多個子要求,建構完整的 Google 文件。
要求是單一 JSON 要求,其中包含多個巢狀子要求,以及一個必要屬性:requests
。要求會建構在個別要求的陣列中。每個要求都會使用 JSON 代表要求物件,並包含其屬性。
批次要求的回應格式與要求格式類似。伺服器的回應包含單一回應物件的完整回覆。
主要 JSON 物件的屬性名為 replies
。回應會以陣列形式傳回,每個要求的回應會佔用與對應要求相同的索引順序。部分要求沒有回應,且該陣列索引處的回應為空白。
範例
下列程式碼範例顯示如何搭配使用 Slides API 與批次處理。
要求
這個批次要求範例說明如何:
{
"requests":[
{
"createSlide":{
"insertionIndex":1,
"objectId":"newSlide"
}
},
{
"createShape":{
"elementProperties":{
"pageObjectId":"newSlide",
"size":{
"height":{
"magnitude":50,
"unit":"PT"
},
"width":{
"magnitude":200,
"unit":"PT"
}
}
},
"shapeType":"TEXT_BOX",
"objectId":"newTextBox"
}
},
{
"insertText":{
"objectId":"newTextBox",
"text":"Hello World"
}
}
]
}
回應
這個批次回應範例會顯示批次要求中每個子要求的套用方式。請注意,InsertTextRequest
方法不包含回應,因此陣列在 [2] 的索引值包含空白的大括號。批次要求會顯示 WriteControl
屬性,說明寫入要求的執行方式。
{
"requiredRevisionId": ID
"presentationId": "",
"replies":[
{
"createSlide":{
"objectId":"newSlide"
}
},
{
"createShape":{
"objectId":"newTextBox"
}
},
{
}
],
"writeControl":{
"requiredRevisionId": REVISION_ID
}
}
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-29 (世界標準時間)。
[null,null,["上次更新時間:2025-08-29 (世界標準時間)。"],[],[],null,["# Batch requests\n\nThis document shows how to batch API calls together to reduce the number of\nconnections your client has to make. Batching can improve an application's\nefficiency by decreasing network round trips and increasing throughput.\n\nOverview\n--------\n\n\nEach connection your client makes results in a certain amount of overhead.\nThe Google Slides API supports batching to let your client place multiple\nrequest objects, each one specifying a single type of request to perform,\ninto a single batch request. A batch request can boost performance by\ncombining multiple subrequests into a single call to the server, retrieving\na single response back.\n\n\nWe encourage users to always batch multiple requests together. Here are some\nexamples of situations where you can use batching:\n\n- You've just started using the API and you have lots of data to upload.\n- You need to update metadata or properties, such as formatting, on multiple objects.\n- You need to delete many objects.\n\nLimits, authorization, \\& dependency considerations\n---------------------------------------------------\n\nHere's a list of other items to consider when employing batch updating:\n\n- Each batch request, including all subrequests, is counted as one API request toward your [usage limit](/workspace/slides/api/limits).\n- A batch request is authenticated once. This single authentication applies to all batch update objects in the request.\n- The server processes the subrequests in the same order they appear in the batch request. Latter subrequests can depend on actions taken during earlier subrequests. For example, in the same batch request, users can insert text into an existing document and then style it.\n\nBatch details\n-------------\n\n\nA batch request consists of one [batchUpdate](/workspace/slides/api/reference/rest/v1/presentations/batchUpdate) method call\nwith multiple subrequests to, for example, add and then format a presentation.\n\n\nEach request is validated before being applied. All subrequests in the batch\nupdate are applied atomically. That is, if any request is not valid then the\nentire update is unsuccessful and none of the (potentially dependent)\nchanges are applied.\n\n\nSome requests provide responses with information about the applied requests.\nFor example, all batch update requests to add objects return responses so\nyou can access the metadata of the newly added object, such as the ID or\ntitle.\n\n\nWith this approach, you can build an entire Google document using one API\nbatch update request with multiple subrequests.\n\n### Format of a batch request\n\n\nA [request](/workspace/slides/api/reference/rest/v1/presentations/request) is a single JSON request containing multiple,\nnested subrequests with one required property: `requests`. The\nrequests are constructed in an array of individual requests. Each request uses\nJSON to represent the request object and to contain its properties.\n\n### Format of a batch response\n\n\nThe [response](/workspace/slides/api/reference/rest/v1/presentations/response) format for a batch request is similar to the\nrequest format. The server's response contains a complete reply of the single\nresponse object.\n\n\nThe main JSON object's property is named `replies`. The responses\nare returned in an array, with each response to one of the requests occupying\nthe same index order as the corresponding request. Some requests don't have\nresponses and the response at that array index is empty.\n\nExample\n-------\n\nThe following code sample shows the use of batching with the\nSlides API.\n\n### Request\n\nThis example batch request demonstrates how to:\n\n- Add a\n [`presentations.pages`](/workspace/slides/api/reference/rest/v1/presentations.pages)\n resource to an existing presentation, with an `insertionIndex` of `1`, using\n the\n [`CreateSlideRequest`](/workspace/slides/api/reference/rest/v1/presentations/request#createsliderequest)\n method.\n\n- Add a `shapeType` of type `TEXT_BOX` to the new slide using the\n [`CreateShapeRequest`](/workspace/slides/api/reference/rest/v1/presentations/request#createshaperequest)\n method.\n\n- Insert \"Hello World\" text into the new field using the\n [`InsertTextRequest`](/workspace/slides/api/reference/rest/v1/presentations/request#inserttextrequest)\n method.\n\n```scdoc\n{\n \"requests\":[\n {\n \"createSlide\":{\n \"insertionIndex\":1,\n \"objectId\":\"newSlide\"\n }\n },\n {\n \"createShape\":{\n \"elementProperties\":{\n \"pageObjectId\":\"newSlide\",\n \"size\":{\n \"height\":{\n \"magnitude\":50,\n \"unit\":\"PT\"\n },\n \"width\":{\n \"magnitude\":200,\n \"unit\":\"PT\"\n }\n }\n },\n \"shapeType\":\"TEXT_BOX\",\n \"objectId\":\"newTextBox\"\n }\n },\n {\n \"insertText\":{\n \"objectId\":\"newTextBox\",\n \"text\":\"Hello World\"\n }\n }\n ]\n}\n```\n\n### Response\n\nThis example batch response displays information on how each subrequest within\nthe batch request was applied. Note the\n[`InsertTextRequest`](/workspace/slides/api/reference/rest/v1/presentations/request#inserttextrequest)\nmethod doesn't contain a response so the index value of the array at \\[2\\]\nconsists of empty curly braces. The batch request does display the\n[`WriteControl`](/workspace/slides/api/reference/rest/v1/presentations/batchUpdate#writecontrol)\nproperty, which shows how the write requests were executed. \n\n```scdoc\n{\n \"requiredRevisionId\": ID\n \"presentationId\": \"\",\n \"replies\":[\n {\n \"createSlide\":{\n \"objectId\":\"newSlide\"\n }\n },\n {\n \"createShape\":{\n \"objectId\":\"newTextBox\"\n }\n },\n {\n \n }\n ],\n \"writeControl\":{\n \"requiredRevisionId\": REVISION_ID\n }\n}\n```"]]