يوضّح نموذج الرمز البرمجي التالي كيفية إدراج سلسلة من سلاسل النصوص في مواضع الفهرس المحدّدة في نص المستند. يستخدم المثال ثلاثة إزاحات استهداف (25 و50 و75) ويُدخِل سلسلة من عشرة أحرف في كل موقع.
تزيد كل عملية إدراج جميع الفواصل ذات الأرقام الأعلى بمقدار حجم النص المُدرَج. يحسب المثال نتيجة تغييرات الفهرس هذه مسبقًا لكي تتم عمليات الإدراج اللاحقة في الموضع الجديد الذي تم تصحيحه. وبالتالي، لإدراج الإعلان في الموضع المستهدف الأصلي 25 و50 و75، تكون فهارس الإدراج الفعلية هي:
تُضيف عملية الإدراج الأولى 10 أحرف في الإزاحة 25.
تضيف عملية الإدراج الثانية 10 أحرف في الإزاحة 50+10=60.
تضيف عملية الإدراج الثالثة 10 أحرف في الإزاحة 75+10+10=95.
حذف النص
لحذف نص من مستند، عليك أولاً إنشاء Range يحدِّد النطاق الذي تريد حذفه. بعد ذلك، استخدِم طريقة documents.batchUpdate و أدرِج DeleteContentRangeRequest.
يعرض نموذج الرمز البرمجي التالي كيفية حذف النص بين الفهرس 10 والفهرس 24 في نص المستند.
تبسيط الأمور من خلال الكتابة من الخلف كما هو الحال مع عمليات الإدراج، يؤدي حذف النص إلى تغيير فهارس كل النصوص "أسفل" الجزء. مرة أخرى، يمكن أن يؤدي الكتابة بترتيب عكسي إلى تبسيط التعامل مع الفهارس.
نقل النص
لنقل نص، عليك حذفه من مكان ثم إدراجه في مكان آخر. لا يمنحك حذف المحتوى نسخة منه (لا يتوفّر مفهوم مكافئ لملف الحافظة)، لذا عليك استخراج محتوى النطاق أولاً حتى تتمكّن من استخدامه في طلب إدراج النص.
تاريخ التعديل الأخير: 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,["The Google Docs API lets you insert or delete text in a document. Moving text\ninvolves both operations, preceded by a `get` of the content.\n\nYou can insert or delete text in any of a document\n[tab](/workspace/docs/api/how-tos/tabs)'s segments (body, header, footer, or\nfootnote).\n\nInsert text\n\nTo insert text into a document, use the\n[`documents.batchUpdate`](/workspace/docs/api/reference/rest/v1/documents/batchUpdate)\nmethod and include an\n[`InsertTextRequest`](/workspace/docs/api/reference/rest/v1/documents/request#inserttextrequest)\nwith the text and location as the payload.\n\nThe following code sample shows how you might insert a series of text strings at\nspecified index locations in the body of a document. The example uses three\ntarget offsets (25, 50, and 75) and inserts a ten-character string at each\nlocation.\n**Note:** Indexes are measured in UTF-16 code units. For more information, see [Start and end indexes](/workspace/docs/api/concepts/structure#indexes). \n\nJava\n\n\n```java\nList\u003cRequest\u003e requests = new ArrayList\u003c\u003e();\nrequests.add(new Request().setInsertText(new InsertTextRequest()\n .setText(text1)\n .setLocation(new Location().setIndex(25).setTabId(TAB_ID))));\n\nrequests.add(new Request().setInsertText(new InsertTextRequest()\n .setText(text2)\n .setLocation(new Location().setIndex(50).setTabId(TAB_ID))));\n\nrequests.add(new Request().setInsertText(new InsertTextRequest()\n .setText(text3)\n .setLocation(new Location().setIndex(75).setTabId(TAB_ID))));\n\nBatchUpdateDocumentRequest body = new BatchUpdateDocumentRequest().setRequests(requests);\nBatchUpdateDocumentResponse response = docsService.documents()\n .batchUpdate(DOCUMENT_ID, body).execute();\n```\n\n\u003cbr /\u003e\n\nPHP\n\n\n```php\n$requests = array();\n$requests[] = new Google_Service_Docs_Request(array(\n 'insertText' =\u003e array(\n 'text' =\u003e $text1,\n 'location' =\u003e array(\n 'index' =\u003e 25,\n 'tabId' =\u003e TAB_ID,\n ),\n ),\n 'insertText' =\u003e array(\n 'text' =\u003e $text2,\n 'location' =\u003e array(\n 'index' =\u003e 50,\n 'tabId' =\u003e TAB_ID,\n ),\n ),\n 'insertText' =\u003e array(\n 'text' =\u003e $text3,\n 'location' =\u003e array(\n 'index' =\u003e 75,\n 'tabId' =\u003e TAB_ID,\n ),\n ),\n));\n\n$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(\n 'requests' =\u003e $requests\n));\n\n$response = $service-\u003edocuments-\u003ebatchUpdate($documentId, $batchUpdateRequest);\n```\n\n\u003cbr /\u003e\n\nPython\n\n\n```python\nrequests = [\n {\n 'insertText': {\n 'location': {\n 'index': 25,\n 'tabId': TAB_ID\n },\n 'text': text1\n }\n },\n {\n 'insertText': {\n 'location': {\n 'index': 50,\n 'tabId': TAB_ID\n },\n 'text': text2\n }\n },\n {\n 'insertText': {\n 'location': {\n 'index': 75,\n 'tabId': TAB_ID\n },\n 'text': text3\n }\n },\n]\n\nresult = service.documents().batchUpdate(\n documentId=DOCUMENT_ID, body={'requests': requests}).execute()\n```\n\n\u003cbr /\u003e\n\nEach insertion increments all the higher-numbered indexes by the size of the\ninserted text. The example precalculates the result of these index changes so\nthat subsequent insertions happen at the new, corrected offset. Thus to insert\nat the original target offsets of 25, 50, and 75, the actual insertion indexes\nare:\n\n- First insertion adds 10 characters at offset 25.\n- Second insertion adds 10 characters at offset 50+10=60.\n- Third insertion adds 10 characters at offset 75+10+10=95.\n\n| **Simplify matters by writing backwards.** To avoid having to precalculate these offset changes, order your insertions to \"write backwards\": do the insertion at the highest-numbered index first, working your way towards the beginning with each insertion. This ordering makes sure that each write's offsets are unaffected by the preceding ones.\n\nDelete text\n\nTo delete text from a document, first construct a\n[`Range`](/workspace/docs/api/reference/rest/v1/documents#range) that defines\nthe range of text to delete. Then use the `documents.batchUpdate` method and\ninclude a\n[`DeleteContentRangeRequest`](/workspace/docs/api/reference/rest/v1/documents/request#deletecontentrangerequest).\n| Be sure you understand the [rules and behavior](/workspace/docs/api/concepts/rules-behavior#delete-text) associated with deleting text.\n\nThe following code sample shows how you might delete the text between index 10\nand index 24 in the body of a document. \n\nJava\n\n\n```java\nList\u003cRequest\u003e requests = new ArrayList\u003c\u003e();\nrequests.add(new Request().setDeleteContentRange(\n new DeleteContentRangeRequest()\n .setRange(new Range()\n .setStartIndex(10)\n .setEndIndex(24)\n .setTabId(TAB_ID))\n ));\n\nBatchUpdateDocumentRequest body = new BatchUpdateDocumentRequest().setRequests(requests);\nBatchUpdateDocumentResponse response = docsService.documents()\n .batchUpdate(DOCUMENT_ID, body).execute();\n```\n\n\u003cbr /\u003e\n\nPHP\n\n\n```php\n$requests = array();\n$requests[] = new Google_Service_Docs_Request(array(\n 'deleteContentRange' =\u003e array(\n 'range' =\u003e array(\n 'startIndex' =\u003e 10,\n 'endIndex' =\u003e 24,\n 'tabId' =\u003e TAB_ID\n ),\n ),\n));\n\n$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(\n 'requests' =\u003e $requests\n));\n\n$response = $service-\u003edocuments-\u003ebatchUpdate($documentId, $batchUpdateRequest);\n```\n\n\u003cbr /\u003e\n\nPython\n\n\n```python\nrequests = [\n {\n 'deleteContentRange': {\n 'range': {\n 'startIndex': 10,\n 'endIndex': 24,\n 'tabId': TAB_ID\n }\n\n }\n\n },\n]\nresult = service.documents().batchUpdate(\n documentId=DOCUMENT_ID, body={'requests': requests}).execute()\n```\n\n\u003cbr /\u003e\n\n**Simplify matters by writing backwards.** As with insertions, deleting\ntext alters the indexes of all the text \"below\" in the segment. Again, [writing\nbackwards](#write_backwards) can simplify your handling of indexes.\n\nMove text\n\nTo move text, you delete it in one location and then insert it elsewhere.\nDeleting content doesn't give you a copy of it (there's no equivalent concept of\na clipboard) so you must extract the contents of the range first so you can use\nin your insert text request.\n\nRelated topics\n\n- [Create and manage documents](/workspace/docs/api/how-tos/documents)\n- [Merge text into a document](/workspace/docs/api/how-tos/merge)\n- [Structure of a Google Docs document](/workspace/docs/api/concepts/structure)"]]