插入、刪除及移動文字

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()

往回寫出簡化案件。和插入的情況一樣,刪除文字也會變更區段「下方」所有文字的索引。同樣地,退回也可以簡化索引的處理。

移動文字

如要移動文字,請先刪除其中一個位置的文字,然後再將文字插入其他位置。 刪除內容並不會提供您複製內容的副本 (剪貼簿沒有相等的概念),因此您必須先擷取範圍的內容,才能在插入文字要求中使用。