Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Tài liệu này trình bày cách kết hợp các lệnh gọi API với nhau để giảm số lượng kết nối mà ứng dụng của bạn phải thực hiện. Việc xử lý hàng loạt có thể cải thiện hiệu quả của ứng dụng bằng cách giảm số lượt truyền dữ liệu qua lại trên mạng và tăng thông lượng.
Tổng quan
Mỗi kết nối mà ứng dụng của bạn thực hiện sẽ dẫn đến một lượng hao tổn nhất định.
Google Slides API hỗ trợ việc tạo yêu cầu hàng loạt để cho phép ứng dụng của bạn đặt nhiều đối tượng yêu cầu (mỗi đối tượng chỉ định một loại yêu cầu duy nhất cần thực hiện) vào một yêu cầu hàng loạt. Yêu cầu hàng loạt có thể tăng hiệu suất bằng cách kết hợp nhiều yêu cầu phụ thành một lệnh gọi duy nhất đến máy chủ, truy xuất một phản hồi duy nhất.
Người dùng nên luôn tạo nhiều yêu cầu cùng lúc. Sau đây là một số ví dụ về những trường hợp bạn có thể sử dụng tính năng xử lý hàng loạt:
Bạn mới bắt đầu sử dụng API và có nhiều dữ liệu cần tải lên.
Bạn cần cập nhật siêu dữ liệu hoặc các thuộc tính (chẳng hạn như định dạng) trên nhiều đối tượng.
Bạn cần xoá nhiều đối tượng.
Các giới hạn, uỷ quyền và những điều cần cân nhắc về phần phụ thuộc
Sau đây là danh sách những mục khác cần cân nhắc khi sử dụng tính năng cập nhật hàng loạt:
Mỗi yêu cầu hàng loạt (bao gồm cả tất cả các yêu cầu phụ) được tính là một yêu cầu API trong giới hạn sử dụng.
Yêu cầu hàng loạt được xác thực một lần. Quy trình xác thực duy nhất này áp dụng cho tất cả các đối tượng cập nhật hàng loạt trong yêu cầu.
Máy chủ xử lý các yêu cầu phụ theo thứ tự xuất hiện trong yêu cầu hàng loạt. Các yêu cầu phụ sau này có thể phụ thuộc vào các hành động được thực hiện trong các yêu cầu phụ trước đó. Ví dụ: trong cùng một yêu cầu hàng loạt, người dùng có thể chèn văn bản vào một tài liệu hiện có rồi định kiểu cho văn bản đó.
Chi tiết gói
Một yêu cầu hàng loạt bao gồm một lệnh gọi phương thức batchUpdate với nhiều yêu cầu phụ, chẳng hạn như thêm rồi định dạng một bản trình bày.
Mỗi yêu cầu đều được xác thực trước khi được áp dụng. Tất cả các yêu cầu phụ trong bản cập nhật hàng loạt đều được áp dụng một cách riêng lẻ. Tức là nếu có yêu cầu không hợp lệ thì toàn bộ quá trình cập nhật sẽ không thành công và không có thay đổi nào (có thể phụ thuộc) được áp dụng.
Một số yêu cầu cung cấp phản hồi kèm theo thông tin về các yêu cầu đã áp dụng.
Ví dụ: tất cả các yêu cầu cập nhật hàng loạt để thêm đối tượng đều trả về các phản hồi để bạn có thể truy cập vào siêu dữ liệu của đối tượng mới được thêm, chẳng hạn như mã nhận dạng hoặc tiêu đề.
Với phương pháp này, bạn có thể tạo toàn bộ tài liệu trên Google bằng một yêu cầu cập nhật theo lô của API có nhiều yêu cầu phụ.
Định dạng của yêu cầu theo lô
request là một yêu cầu JSON duy nhất chứa nhiều yêu cầu phụ lồng nhau với một thuộc tính bắt buộc: requests. Các yêu cầu được tạo trong một mảng các yêu cầu riêng lẻ. Mỗi yêu cầu đều sử dụng JSON để biểu thị đối tượng yêu cầu và chứa các thuộc tính của đối tượng đó.
Định dạng của phản hồi hàng loạt
Định dạng phản hồi cho một yêu cầu hàng loạt tương tự như định dạng yêu cầu. Phản hồi của máy chủ chứa một câu trả lời hoàn chỉnh của đối tượng phản hồi duy nhất.
Thuộc tính của đối tượng JSON chính có tên là replies. Các phản hồi được trả về trong một mảng, trong đó mỗi phản hồi cho một trong các yêu cầu chiếm cùng một thứ tự chỉ mục như yêu cầu tương ứng. Một số yêu cầu không có phản hồi và phản hồi tại chỉ mục mảng đó là trống.
Ví dụ:
Mã mẫu sau đây cho thấy cách sử dụng tính năng xử lý hàng loạt với Slides API.
Phản hồi hàng loạt mẫu này cho thấy thông tin về cách áp dụng từng yêu cầu phụ trong yêu cầu hàng loạt. Lưu ý rằng phương thức InsertTextRequest không chứa phản hồi nên giá trị chỉ mục của mảng tại [2] bao gồm các dấu ngoặc nhọn trống. Yêu cầu hàng loạt sẽ hiển thị thuộc tính WriteControl. Thuộc tính này cho biết cách thực hiện các yêu cầu ghi.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-08-29 UTC."],[],[],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```"]]