تنظيم صفحاتك في مجموعات يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
يوضّح هذا المستند كيفية تجميع طلبات واجهة برمجة التطبيقات معًا لتقليل عدد الاتصالات التي يجب أن يجريها العميل. يمكن أن تؤدي معالجة البيانات مجمّعةً إلى تحسين فعالية التطبيق عن طريق تقليل عدد عمليات التنقّل بين التطبيقات والشبكة وزيادة معدل نقل البيانات.
نظرة عامة
يؤدي كل اتصال يجريه برنامجك إلى زيادة في وقت الاستجابة. تتيح Google Docs API التجميع للسماح لعميلك بوضع عدة عناصر طلب، يحدّد كلّ منها نوعًا واحدًا من الطلبات المطلوب تنفيذه، في طلب مجمّع واحد. يمكن أن يؤدي طلب الحزمة إلى تحسين الأداء من خلال دمج طلبات فرعية متعددة في طلب واحد إلى الخادم، واسترداد ردّ واحد.
ونشجّع المستخدمين على تجميع طلبات متعددة معًا دائمًا. في ما يلي بعض الأمثلة على الحالات التي يمكنك فيها استخدام ميزة "تجميع الطلبات":
إذا كنت قد بدأت للتو في استخدام واجهة برمجة التطبيقات ولديك الكثير من البيانات المطلوب تحميلها
تحتاج إلى تعديل البيانات الوصفية أو الخصائص، مثل التنسيق، في عناصر متعدّدة.
عليك حذف العديد من العناصر.
اعتبارات الحدود والتفويض والتبعية
في ما يلي قائمة بالعناصر الأخرى التي يجب مراعاتها عند استخدام ميزة "التحديث المجمّع":
يتم احتساب كل طلب مجمّع، بما في ذلك جميع الطلبات الفرعية، كطلب واحد من واجهة برمجة التطبيقات ضمن الحد الأقصى للاستخدام.
تتم مصادقة طلب الحزمة مرة واحدة. تنطبق عملية المصادقة هذه على جميع عناصر التعديل المجمّع في الطلب.
يعالج الخادم الطلبات الفرعية بالترتيب نفسه الذي تظهر به في طلب الدفعة. يمكن أن تعتمد الطلبات الفرعية اللاحقة على الإجراءات التي تم اتّخاذها أثناء الطلبات الفرعية السابقة. على سبيل المثال، في طلب الحزمة نفسه، يمكن للمستخدمين إدراج نص في مستند حالي ثم تنسيقه.
تفاصيل الدفعة
يتألّف الطلب المجمّع من طلب واحد لاستدعاء طريقة batchUpdate مع طلبات فرعية متعددة، مثل إضافة مستند ثم تنسيقه.
يتم التحقّق من كل طلب قبل تطبيقه. يتم تطبيق جميع الطلبات الفرعية في التحديث الدفعي بشكل موحّد. وهذا يعني أنّه إذا لم يكن أي طلب صالحًا، لن يتم إكمال التعديل بالكامل ولن يتم تطبيق أي من التغييرات (التي قد تكون متعلّقة).
تقدّم بعض الطلبات ردودًا تتضمّن معلومات عن الطلبات التي تمّ تطبيقها. على سبيل المثال، تُرسِل جميع طلبات التعديل المجمّع لإضافة عناصر ردود حتى تتمكّن من الوصول إلى البيانات الوصفية للعنصر الذي تمت إضافته حديثًا، مثل المعرّف أو العنوان.
باستخدام هذا النهج، يمكنك إنشاء مستند Google بالكامل باستخدام طلب تحديث مجمّع واحد لواجهة برمجة التطبيقات يتضمّن طلبات فرعية متعددة.
تنسيق طلب الحزمة
الطلب هو طلب JSON واحد يحتوي على عدة طلبات فرعية مضمّنة تتضمّن سمة واحدة مطلوبة: requests. يتم إنشاء طلبات في صفيف من الطلبات الفردية. يستخدم كل طلب تنسيق JSON لتمثيل عنصر الطلب وتضمين خصائصه.
تنسيق ردّ الدفعة
يشبه تنسيق الردّ لطلب الحزمة تنسيق الطلب. يحتوي ردّ الخادم على ردّ كامل لعنصر الاستجابة الفردي.
سمة عنصر JSON الرئيسي هي replies. يتم عرض الاستجابات في صفيف، مع احتلال كل استجابة لأحد الطلبات ترتيب الفهرس نفسه للطلب المقابل. لا تتضمّن بعض الطلبات ردودًا، ويكون الردّ في فهرس الصفيف هذا فارغًا.
مثال
يوضِّح نموذج الرمز البرمجي التالي استخدام ميزة تجميع الطلبات مع Docs API.
الطلب
يوضّح مثال طلب الحِزم هذا كيفية إجراء ما يلي:
أدخِل النص "مرحبًا بك" في بداية مستند حالي، مع locationفهرس 1، باستخدام InsertTextRequest.
عدِّل كلمة "مرحبًا" باستخدام UpdateTextStyleRequest. تحدِّد startIndex وendIndexrange للنص المنسَّق ضمن القطعة.
باستخدام textStyle، اضبط نمط الخط على غامق واللون على أزرق لكلمة "مرحبًا" فقط.
استبدِل TAB_ID وREQUIRED_REVISION_ID بقيمة معرّف علامة التبويب ومعرّف المراجعة، على التوالي، للمستند الذي يتم تطبيق طلب الكتابة عليه.
الردّ
يعرض مثال استجابة الحزمة هذه معلومات عن كيفية تطبيق كل طلب فرعي ضمن طلب الحزمة. لا يحتوي كلّ من InsertTextRequest أو UpdateTextStyleRequest على استجابة، لذا تتألّف قيم الفهرس للصفيف في [0] و[1] من قوسين معقوفين فارغين. يعرض الطلب المجمّع العنصر WriteControl، الذي يوضّح كيفية تنفيذ الطلبات.
تاريخ التعديل الأخير: 2025-08-01 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","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-01 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],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)"]]