टेक्स्ट डालें, मिटाएं, और एक जगह से दूसरी जगह ले जाएं

Google Docs API की मदद से, किसी दस्तावेज़ में टेक्स्ट शामिल किया जा सकता है या मिटाया जा सकता है. टेक्स्ट को एक जगह से दूसरी जगह ले जाने की प्रक्रिया में, कॉन्टेंट के get से पहले की दोनों कार्रवाइयां शामिल होती हैं.

आपके पास दस्तावेज़ के किसी भी सेगमेंट (मुख्य हिस्से, हेडर, फ़ुटर या फ़ुटनोट) में टेक्स्ट जोड़ने या मिटाने का विकल्प होता है.

टेक्स्ट डालें

किसी दस्तावेज़ में टेक्स्ट डालने के लिए, documents.batchUpdate मेथड का इस्तेमाल करें और पेलोड के तौर पर टेक्स्ट और जगह की जानकारी के साथ InsertTextRequest जोड़ें.

नीचे दिया गया कोड सैंपल दिखाता है कि किसी दस्तावेज़ के मुख्य हिस्से में, तय की गई इंडेक्स जगहों पर टेक्स्ट स्ट्रिंग की सीरीज़ कैसे शामिल की जा सकती है. उदाहरण में तीन टारगेट ऑफ़सेट (25, 50, और 75) का इस्तेमाल किया गया है और हर जगह पर दस वर्णों की स्ट्रिंग डाली गई है.

Java

        List<Request> requests = new ArrayList<>();
        requests.add(new Request().setInsertText(new InsertTextRequest()
                .setText(text1)
                .setLocation(new Location().setIndex(25))));

        requests.add(new Request().setInsertText(new InsertTextRequest()
                .setText(text2)
                .setLocation(new Location().setIndex(50))));

        requests.add(new Request().setInsertText(new InsertTextRequest()
                .setText(text3)
                .setLocation(new Location().setIndex(75))));

        BatchUpdateDocumentRequest body = new BatchUpdateDocumentRequest().setRequests(requests);
        BatchUpdateDocumentResponse response = docsService.documents()
                .batchUpdate(DOCUMENT_ID, body).execute();

PHP

$requests = array();
$requests[] = new Google_Service_Docs_Request(array(
    'insertText' => array(
        'text' => $text1,
        'location' => array(
            'index' => 25,
        ),
    ),
    'insertText' => array(
        'text' => $text2,
        'location' => array(
            'index' => 50,
        ),
    ),
    'insertText' => array(
        'text' => $text3,
        'location' => array(
            'index' => 75,
        ),
    ),
));

$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(
    'requests' => $requests
));

$response = $service->documents->batchUpdate($documentId, $batchUpdateRequest);

Python

    requests = [
         {
            'insertText': {
                'location': {
                    'index': 25,
                },
                'text': text1
            }
        },
                 {
            'insertText': {
                'location': {
                    'index': 50,
                },
                'text': text2
            }
        },
                 {
            'insertText': {
                'location': {
                    'index': 75,
                },
                'text': text3
            }
        },
    ]

    result = service.documents().batchUpdate(
        documentId=DOCUMENT_ID, body={'requests': requests}).execute()

हर इंसर्शन, डाले गए टेक्स्ट के साइज़ के हिसाब से सभी बड़ी संख्या वाले इंडेक्स को बढ़ा देता है. यह उदाहरण, इंडेक्स में होने वाले इन बदलावों के नतीजे को पहले से ही कैलकुलेट करता है, ताकि नए और सही ऑफ़सेट पर, बाद में जोड़ी जा सके. इस तरह 25, 50, और 75 के मूल टारगेट ऑफ़सेट को डालने के लिए, असल इंसर्शन इंडेक्स ये हैं:

  • पहली बार जोड़े जाने के बाद, ऑफ़सेट 25 पर 10 वर्ण जोड़े जाते हैं.
  • सेकंड इंसर्शन, 50+10=60 ऑफ़सेट पर 10 वर्ण जोड़ता है.
  • तीसरा इंसर्शन, ऑफ़सेट 75+10+10=95 पर 10 वर्ण जोड़ता है.

टेक्स्ट मिटाना

किसी दस्तावेज़ से टेक्स्ट मिटाने के लिए, सबसे पहले एक Range बनाएं. इससे पता चलता है कि कितने टेक्स्ट को मिटाना है. इसके बाद, documents.batchUpdate तरीके का इस्तेमाल करें और DeleteContentRangeRequest को शामिल करें.

नीचे दिया गया कोड सैंपल दिखाता है कि दस्तावेज़ के मुख्य हिस्से में, इंडेक्स 10 और इंडेक्स 24 के बीच के टेक्स्ट को कैसे मिटाया जा सकता है.

Java

        List<Request> requests = new ArrayList<>();
        requests.add(new Request().setDeleteContentRange(
                new DeleteContentRangeRequest()
                        .setRange(new Range()
                                .setStartIndex(10)
                                .setEndIndex(24))
            ));

        BatchUpdateDocumentRequest body = new BatchUpdateDocumentRequest().setRequests(requests);
        BatchUpdateDocumentResponse response = docsService.documents()
                .batchUpdate(DOCUMENT_ID, body).execute();

PHP

$requests = array();
$requests[] = new Google_Service_Docs_Request(array(
    'deleteContentRange' => array(
        'range' => array(
            'startIndex' => 10,
            'endIndex' => 24
        ),
    ),
));

$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(
    'requests' => $requests
));

$response = $service->documents->batchUpdate($documentId, $batchUpdateRequest);

Python

    requests = [
        {
            'deleteContentRange': {
                'range': {
                    'startIndex': 10,
                    'endIndex': 24,
                }

            }

        },
    ]
    result = service.documents().batchUpdate(
        documentId=DOCUMENT_ID, body={'requests': requests}).execute()

पीछे की ओर लिखकर मामलों को आसान बनाएं. इंसर्शन की तरह ही, टेक्स्ट मिटाने से सेगमेंट के "नीचे" के सभी टेक्स्ट के इंडेक्स बदल जाते हैं. फिर से, पीछे लिखने से आपके इंडेक्स को मैनेज करना आसान हो सकता है.

टेक्स्ट को दूसरी जगह ले जाएं

टेक्स्ट को एक जगह से दूसरी जगह पर ले जाने के लिए, उसे किसी एक जगह मिटाएं और फिर उसे कहीं और डालें. कॉन्टेंट मिटाने से आपको उसकी कॉपी नहीं मिलती (क्लिपबोर्ड जैसा कोई कॉन्सेप्ट नहीं है). इसलिए, आपको पहले रेंज का कॉन्टेंट एक्सट्रैक्ट करना होगा, ताकि आप उसका इस्तेमाल, टेक्स्ट शामिल करने के अनुरोध में कर सकें.