با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
این سند نشان میدهد که چگونه میتوانید تماسهای API را با هم دستهبندی کنید تا تعداد اتصالهایی که مشتری شما باید برقرار کند را کاهش دهید. بچینگ می تواند با کاهش رفت و برگشت شبکه و افزایش توان عملیاتی، کارایی برنامه را بهبود بخشد.
نمای کلی
هر اتصالی که مشتری شما ایجاد می کند منجر به مقدار معینی سربار می شود. Google Docs API از دستهبندی پشتیبانی میکند تا به مشتری شما اجازه دهد چندین شیء درخواستی را که هر کدام یک نوع درخواست را برای انجام مشخص میکنند، در یک درخواست دستهای قرار دهد. یک درخواست دسته ای می تواند با ترکیب چندین درخواست فرعی در یک تماس با سرور، عملکرد را افزایش دهد و یک پاسخ را بازیابی کند.
ما کاربران را تشویق می کنیم که همیشه چندین درخواست را با هم دسته بندی کنند. در اینجا چند نمونه از موقعیت هایی وجود دارد که می توانید از بچینگ استفاده کنید:
شما به تازگی استفاده از API را شروع کرده اید و داده های زیادی برای آپلود دارید.
شما باید متادیتا یا ویژگیهایی مانند قالببندی را روی چندین شیء بهروزرسانی کنید.
شما باید بسیاری از اشیاء را حذف کنید.
محدودیت ها، مجوزها، و ملاحظات وابستگی
در اینجا لیستی از موارد دیگری وجود دارد که باید هنگام بهروزرسانی دستهای در نظر بگیرید:
هر درخواست دستهای، از جمله همه درخواستهای فرعی، به عنوان یک درخواست API برای محدودیت استفاده شما محاسبه میشود.
درخواست دسته ای یک بار احراز هویت می شود. این احراز هویت واحد برای همه اشیاء بهروزرسانی دستهای در درخواست اعمال میشود.
سرور درخواست های فرعی را به همان ترتیبی که در درخواست دسته ای ظاهر می شوند پردازش می کند. درخواستهای فرعی بعدی میتواند به اقدامات انجام شده در طول درخواستهای فرعی قبلی بستگی داشته باشد. به عنوان مثال، در همان درخواست دسته ای، کاربران می توانند متنی را در یک سند موجود وارد کنند و سپس به آن استایل دهند.
جزئیات دسته
یک درخواست دسته ای شامل یک فراخوانی متد batchUpdate با چندین درخواست فرعی برای اضافه کردن و سپس فرمت کردن یک سند است.
هر درخواست قبل از اعمال تایید می شود. همه درخواستهای فرعی در بهروزرسانی دستهای به صورت اتمی اعمال میشوند. یعنی اگر هر درخواستی معتبر نباشد، کل به روز رسانی ناموفق است و هیچ یک از تغییرات (بالقوه وابسته) اعمال نمی شود.
برخی از درخواستها پاسخهایی را با اطلاعات درخواستهای اعمال شده ارائه میدهند. برای مثال، تمام درخواستهای بهروزرسانی دستهای برای افزودن اشیا، پاسخها را برمیگردانند تا بتوانید به ابردادههای شی جدید اضافهشده، مانند شناسه یا عنوان دسترسی داشته باشید.
با این رویکرد، میتوانید کل سند Google را با استفاده از یک درخواست بهروزرسانی دستهای API با چندین درخواست فرعی بسازید.
فرمت درخواست دسته ای
درخواست یک درخواست JSON منفرد است که شامل چندین درخواست فرعی تودرتو با یک ویژگی مورد نیاز است: requests . درخواست ها در آرایه ای از درخواست های فردی ساخته می شوند. هر درخواست از JSON برای نمایش شی درخواست و حاوی ویژگی های آن استفاده می کند.
قالب یک پاسخ دسته ای
فرمت پاسخ برای درخواست دسته ای مشابه فرمت درخواست است. پاسخ سرور حاوی یک پاسخ کامل از شیء پاسخ واحد است.
ویژگی اصلی شیء JSON replies نام دارد. پاسخها در یک آرایه برگردانده میشوند و هر پاسخ به یکی از درخواستها همان ترتیب فهرست درخواست مربوطه را اشغال میکند. برخی از درخواست ها پاسخی ندارند و پاسخ در آن شاخص آرایه خالی است.
مثال
نمونه کد زیر استفاده از دستهبندی با Docs API را نشان میدهد.
درخواست کنید
این درخواست دستهای مثال نشان میدهد که چگونه:
متن "Hello World" را با استفاده از InsertTextRequest در ابتدای یک سند موجود، با location نمایه 1 وارد کنید.
کلمه "Hello" را با استفاده از UpdateTextStyleRequest به روز کنید. startIndex و endIndexrange متن قالببندی شده را در بخش تعریف میکنند.
با استفاده از textStyle ، سبک قلم را روی پررنگ و رنگ را روی آبی فقط برای کلمه "Hello" تنظیم کنید.
TAB_ID و REQUIRED_REVISION_ID را به ترتیب با شناسه برگه و شناسه بازبینی سندی که درخواست نوشتن روی آن اعمال می شود جایگزین کنید.
پاسخ
این نمونه پاسخ دسته ای اطلاعاتی را در مورد نحوه اعمال هر درخواست فرعی در درخواست دسته ای نمایش می دهد. نه InsertTextRequest و نه UpdateTextStyleRequest حاوی پاسخ نیستند، بنابراین مقادیر شاخص آرایه در [0] و [1] از پرانتزهای مجعد خالی تشکیل شده است. درخواست دسته ای شی WriteControl را نمایش می دهد که نحوه اجرای درخواست ها را نشان می دهد.
تاریخ آخرین بهروزرسانی 2025-08-29 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","easyToUnderstand","thumb-up"],["مشکلم را برطرف کرد","solvedMyProblem","thumb-up"],["غیره","otherUp","thumb-up"]],[["اطلاعاتی که نیاز دارم وجود ندارد","missingTheInformationINeed","thumb-down"],["بیشازحد پیچیده/ مراحل بسیار زیاد","tooComplicatedTooManySteps","thumb-down"],["قدیمی","outOfDate","thumb-down"],["مشکل ترجمه","translationIssue","thumb-down"],["مشکل کد / نمونهها","samplesCodeIssue","thumb-down"],["غیره","otherDown","thumb-down"]],["تاریخ آخرین بهروزرسانی 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 Docs 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/docs/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/docs/api/reference/rest/v1/documents/batchUpdate) method call\nwith multiple subrequests to, for example, add and then format a document.\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/docs/api/reference/rest/v1/documents/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/docs/api/reference/rest/v1/documents/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 Docs API.\n\n### Request\n\nThis example batch request demonstrates how to:\n\n- Insert \"Hello World\" text into the start of an existing document, with an\n index `location` of `1`, using the\n [`InsertTextRequest`](/workspace/docs/api/reference/rest/v1/documents/request#inserttextrequest).\n\n- Update the word \"Hello\" using the\n [`UpdateTextStyleRequest`](/workspace/docs/api/reference/rest/v1/documents/request#updatetextstylerequest).\n The `startIndex` and `endIndex` define the `range` of formatted text within\n the segment.\n\n- Using `textStyle`, set the font style to bold and the color to blue for just\n the word \"Hello\".\n\n- Using the [`WriteControl`](/workspace/docs/api/reference/rest/v1/documents/batchUpdate#writecontrol)\n field, you can control how write requests are executed. For more\n information, see [Establish state consistency with\n WriteControl](/workspace/docs/api/how-tos/best-practices#establish-state-consistency).\n\n```verilog\n{\n \"requests\":[\n {\n \"insertText\":{\n \"location\":{\n \"index\":1,\n \"tabId\":TAB_ID\n },\n \"text\":\"Hello World\"\n }\n },\n {\n \"updateTextStyle\":{\n \"range\":{\n \"startIndex\":1,\n \"endIndex\":6\n },\n \"textStyle\":{\n \"bold\":true,\n \"foregroundColor\":{\n \"color\":{\n \"rgbColor\":{\n \"blue\":1\n }\n }\n }\n },\n \"fields\":\"bold,foreground_color\"\n }\n }\n ],\n \"writeControl\": {\n \"requiredRevisionId\": \"\u003cvar translate=\"no\"\u003eREQUIRED_REVISION_ID\u003c/var\u003e\"\n }\n}\n```\n\nReplace \u003cvar translate=\"no\"\u003eTAB_ID\u003c/var\u003e and \u003cvar translate=\"no\"\u003eREQUIRED_REVISION_ID\u003c/var\u003e with\nthe tab ID and the revision ID, respectively, of the document the write request\nis applied to.\n\n### Response\n\nThis example batch response displays information on how each subrequest within\nthe batch request was applied. Neither the\n[`InsertTextRequest`](/workspace/docs/api/reference/rest/v1/documents/request#InsertTextRequest)\nor the\n[`UpdateTextStyleRequest`](/workspace/docs/api/reference/rest/v1/documents/request#updatetextstylerequest)\ncontain a response, so the index values of the array at \\[0\\] and \\[1\\] consist\nof empty curly braces. The batch request displays the `WriteControl` object,\nwhich shows how the requests were executed. \n\n```mysql\n{\n \"replies\":[\n {},\n {}\n ],\n \"writeControl\":{\n \"requiredRevisionId\":`\u003cvar translate=\"no\"\u003eREQUIRED_REVISION_ID\u003c/var\u003e`\n },\n \"documentId\":`\u003cvar translate=\"no\"\u003eDOCUMENT_ID\u003c/var\u003e`\n}\n```\n\nRelated topics\n--------------\n\n- [Best practices for best results](/workspace/docs/api/how-tos/best-practices)"]]