Google 文件 API 支援將一般段落轉換為項目符號清單,以及從段落中移除項目符號。
將段落轉換為清單
常見的段落格式設定作業是將段落轉換為項目符號清單。
如要建立清單,請使用 documents.batchUpdate
方法,並提供 CreateParagraphBulletsRequest
。加入 Range
即可指定受影響的儲存格,並加入 BulletGlyphPreset
即可設定項目符號的模式。
所有與指定範圍重疊的段落都會加上項目符號。如果指定範圍與表格重疊,系統會在表格儲存格內套用項目符號。每個段落的巢狀層級,取決於各段落前方的開頭定位點數量。
您無法調整現有項目符號的巢狀層級。 你必須刪除項目符號、在段落前方設定前置定位點,然後再次建立項目符號。詳情請參閱從清單中移除項目符號。
您也可以使用 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).setTabId(TAB_ID)))); requests.add(new Request().setCreateParagraphBullets( new CreateParagraphBulletsRequest() .setRange(new Range() .setStartIndex(1) .setEndIndex(50) .setTabId(TAB_ID)) .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, 'tabId': TAB_ID }, 'text': 'Item One\n', }}, { 'createParagraphBullets': { 'range': { 'startIndex': 1, 'endIndex': 50, 'tabId': TAB_ID }, 'bulletPreset': 'BULLET_ARROW_DIAMOND_DISC', } } ] result = service.documents().batchUpdate( documentId=DOCUMENT_ID, body={'requests': requests}).execute()
移除清單中的項目符號
如要從段落清單中移除項目符號,請使用 documents.batchUpdate
方法,並提供 DeleteParagraphBulletsRequest
。加入 Range
,指定受影響的儲存格。
這個方法會刪除與指定範圍重疊的所有項目符號,無論巢狀層級為何。為保留巢狀結構層級,系統會在每個對應段落的開頭加入縮排。
下列程式碼範例顯示批次要求,可從段落清單中刪除項目符號。
Java
List<Request> requests = new ArrayList<>(); requests.add(new Request().setDeleteParagraphBullets( new DeleteParagraphBulletsRequest() .setRange(new Range() .setStartIndex(1) .setEndIndex(50) .setTabId(TAB_ID)))); BatchUpdateDocumentRequest body = new BatchUpdateDocumentRequest().setRequests(requests); BatchUpdateDocumentResponse response = docsService.documents() .batchUpdate(DOCUMENT_ID, body).execute();
Python
requests = [ { 'deleteParagraphBullets': { 'range': { 'startIndex': 1, 'endIndex': 50, 'tabId': TAB_ID }, } } ] result = service.documents().batchUpdate( documentId=DOCUMENT_ID, body={'requests': requests}).execute()