插入、刪除及移動文字

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

以反向寫入簡化重要事務。與插入作業一樣,刪除文字也會變更片段中「下方」所有文字的索引。同樣地,向後寫入也能簡化索引的處理。

移動文字

如要移動文字,請將文字從某個位置刪除,再插入其他位置。 刪除內容並不會取得副本的副本 (剪貼簿的概念不存在),因此您必須先擷取該範圍的內容,以便用於插入文字要求。