使用清單

Google Docs API 支援將純段落轉換成項目符號清單,以及將項目符號從段落移除。

將段落轉換為清單

段落格式作業的常見做法是將段落轉換為項目符號清單。

如要建立清單,請使用 documents.batchUpdate 方法並提供 CreateParagraphBulletsRequest。加入 Range 可指定受影響的儲存格,加入 BulletGlyphPreset 則可設定項目符號的模式。

與指定範圍重疊的所有段落都會加上項目符號。如果指定範圍與表格重疊,系統會在表格儲存格中套用項目符號。每個段落的巢狀層級都是在段落前計算前置 Tab 字元。

你無法調整現有項目符號的巢狀層級。 必須刪除項目符號,設定段落前開頭的分頁標籤,然後重新建立項目符號。詳情請參閱從清單中移除項目符號

您也可以使用 CreateParagraphBulletsRequest 變更現有清單的項目符號樣式。

以下程式碼範例顯示批次要求,這類要求會先在文件開頭插入文字,再從橫跨前 50 個字元的段落建立清單。BulletGlyphPreset 使用 BULLET_ARROW_DIAMOND_DISC,這表示項目符號清單的前三個巢狀層級以箭頭、菱形和光碟表示。

Java

      List<Request> requests = new ArrayList<>();
      requests.add(new Request().setInsertText(new InsertTextRequest()
              .setText("Item One\n")
              .setLocation(new Location().setIndex(1))));

      requests.add(new Request().setCreateParagraphBullets(
              new CreateParagraphBulletsRequest()
                      .setRange(new Range()
                              .setStartIndex(1)
                              .setEndIndex(50))
                      .setBulletPreset("BULLET_ARROW_DIAMOND_DISC")));

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

Python

    requests = [
         {
            'insertText': {
                'location': {
                    'index': 1
                },
                'text': 'Item One\n',
            }}, {
            'createParagraphBullets': {
                'range': {
                    'startIndex': 1,
                    'endIndex':  50
                },
                'bulletPreset': 'BULLET_ARROW_DIAMOND_DISC',
            }
        }
    ]

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

將段落轉換為清單。
圖 1. 將段落轉換為清單。

移除清單中的項目符號

如要從段落清單移除項目符號,請使用 documents.batchUpdate 方法並提供 DeleteParagraphBulletsRequest。加入 Range 來指定受影響的儲存格。

這個方法會刪除與指定範圍重疊的所有項目符號,無論其巢狀層級為何。為了視覺化保留巢狀層級,系統會在每個對應段落的開頭加入縮排。

以下程式碼範例說明批次要求,可從段落清單中刪除項目符號。

Java

      List<Request> requests = new ArrayList<>();
      requests.add(new Request().setDeleteParagraphBullets(
              new DeleteParagraphBulletsRequest()
                      .setRange(new Range()
                              .setStartIndex(1)
                              .setEndIndex(50))));

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

Python

    requests = [
         {
            'deleteParagraphBullets': {
                'range': {
                    'startIndex': 1,
                    'endIndex':  50
                },
            }
        }
    ]

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