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

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).setTabId(TAB_ID))));

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

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

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,
            'tabId' => TAB_ID,
        ),
    ),
    'insertText' => array(
        'text' => $text2,
        'location' => array(
            'index' => 50,
            'tabId' => TAB_ID,
        ),
    ),
    'insertText' => array(
        'text' => $text3,
        'location' => array(
            'index' => 75,
            'tabId' => TAB_ID,
        ),
    ),
));

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

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

Python

requests = [
        {
        'insertText': {
            'location': {
                'index': 25,
                'tabId': TAB_ID
            },
            'text': text1
        }
    },
                {
        'insertText': {
            'location': {
                'index': 50,
                'tabId': TAB_ID
            },
            'text': text2
        }
    },
                {
        'insertText': {
            'location': {
                'index': 75,
                'tabId': TAB_ID
            },
            '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)
                        .setTabId(TAB_ID))
    ));

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,
            'tabId' => TAB_ID
        ),
    ),
));

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

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

Python

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

        }

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

उलटे क्रम में लिखकर, चीज़ों को आसान बनाएं. टेक्स्ट डालने की तरह ही, टेक्स्ट मिटाने से सेगमेंट में "नीचे" मौजूद सभी टेक्स्ट के इंडेक्स बदल जाते हैं. यहां भी, उल्टे क्रम में लिखने से इंडेक्स को मैनेज करना आसान हो जाता है.

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

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