このページでは、Google ドキュメント API を使用してテキストの書式を設定する方法について説明します。
書式設定について
ドキュメントのテキスト コンテンツに適用できる書式設定には、次の 2 種類があります。
- フォント、色、下線などの文字の書式を変更できます。
- インデントや行間隔などの段落の書式設定を変更できます。
文字の書式設定を変更する
文字の書式設定は、ドキュメント内のテキスト文字のレンダリングを決定します。
適用した書式設定は、基になる段落の TextStyle から継承されたデフォルトの書式設定をオーバーライドします。逆に、書式設定を行わない文字は、段落のスタイルを継承し続けます。
テキストの文字形式を変更するには、UpdateTextStyleRequest で batchUpdate
を使用します。次の情報を含む Range オブジェクトを指定する必要があります。
- テキストを含むヘッダー、フッター、脚注(指定されていない場合は本文)を識別する
segmentId
。 - 書式設定するセグメント内のテキストの範囲を定義する
startIndex
とendIndex
。 - テキストを含むタブを識別する
tabId
。
次の例では、ヘッダーに含まれるテキストに対して、いくつかのテキスト スタイルの操作を行います。
- 1 ~ 5 文字目のフォントを太字斜体に設定します。
- 6 ~ 10 文字目の色を
blue
14 ポイントの Times New Roman フォントに設定します。 - ハイパーリンクの文字 11 ~ 15 を
www.example.com
にします。
リクエストのリストを作成し、1 回の batchUpdate 呼び出しを使用すると、簡単に実現できます。
Java
List<Request> requests = new ArrayList<>(); requests.add(new Request().setUpdateTextStyle(new UpdateTextStyleRequest() .setTextStyle(new TextStyle() .setBold(true) .setItalic(true)) .setRange(new Range() .setStartIndex(1) .setEndIndex(5) .setTabId(TAB_ID)) .setFields("bold"))); requests.add(new Request() .setUpdateTextStyle(new UpdateTextStyleRequest() .setRange(new Range() .setStartIndex(6) .setEndIndex(10) .setTabId(TAB_ID)) .setTextStyle(new TextStyle() .setWeightedFontFamily(new WeightedFontFamily() .setFontFamily("Times New Roman")) .setFontSize(new Dimension() .setMagnitude(14.0) .setUnit("PT")) .setForegroundColor(new OptionalColor() .setColor(new Color().setRgbColor(new RgbColor() .setBlue(1.0F) .setGreen(0.0F) .setRed(0.0F))))) .setFields("foregroundColor,weightedFontFamily,fontSize"))); requests.add(new Request() .setUpdateTextStyle(new UpdateTextStyleRequest() .setRange(new Range() .setStartIndex(11) .setEndIndex(15) .setTabId(TAB_ID)) .setTextStyle(new TextStyle() .setLink(new Link() .setUrl("www.example.com"))) .setFields("link"))); BatchUpdateDocumentRequest body = new BatchUpdateDocumentRequest().setRequests(requests); BatchUpdateDocumentResponse response = docsService.documents() .batchUpdate(DOCUMENT_ID, body).execute();
Python
requests = [ { 'updateTextStyle': { 'range': { 'startIndex': 1, 'endIndex': 5, 'tabId': TAB_ID }, 'textStyle': { 'bold': True, 'italic': True }, 'fields': 'bold,italic' } }, { 'updateTextStyle': { 'range': { 'startIndex': 6, 'endIndex': 10, 'tabId': TAB_ID }, 'textStyle': { 'weightedFontFamily': { 'fontFamily': 'Times New Roman' }, 'fontSize': { 'magnitude': 14, 'unit': 'PT' }, 'foregroundColor': { 'color': { 'rgbColor': { 'blue': 1.0, 'green': 0.0, 'red': 0.0 } } } }, 'fields': 'foregroundColor,weightedFontFamily,fontSize' } }, { 'updateTextStyle': { 'range': { 'startIndex': 11, 'endIndex': 15, 'tabId': TAB_ID }, 'textStyle': { 'link': { 'url': 'www.example.com' } }, 'fields': 'link' } } ] result = service.documents().batchUpdate( documentId=DOCUMENT_ID, body={'requests': requests}).execute()
段落の書式設定を変更する
Google Docs API を使用すると、段落の書式設定を更新できます。段落の書式設定では、配置やインデントなどの機能を含め、ドキュメント内のテキスト ブロックのレンダリング方法を指定します。
適用した書式設定は、基になる段落スタイルから継承されたデフォルトの書式設定をオーバーライドします。逆に、設定しなかった書式設定機能は、段落スタイルから引き続き継承されます。段落スタイルと継承の詳細については、ParagraphStyle をご覧ください。
次の例では、段落の書式設定を次のように指定しています。
- 名前付きスタイルとしての見出し
- 上の間隔をカスタム設定
- 下のカスタムの間隔
- カスタムの左枠線
段落の残りの書式設定機能は、引き続き基盤となる名前付きスタイルから継承されます。
Java
List<Request> requests = new ArrayList<>(); requests.add(new Request().setUpdateParagraphStyle(new UpdateParagraphStyleRequest() .setRange(new Range() .setStartIndex(1) .setEndIndex(10) .setTabId(TAB_ID)) .setParagraphStyle(new ParagraphStyle() .setNamedStyleType("HEADING_1") .setSpaceAbove(new Dimension() .setMagnitude(10.0) .setUnit("PT")) .setSpaceBelow(new Dimension() .setMagnitude(10.0) .setUnit("PT"))) .setFields("namedStyleType,spaceAbove,spaceBelow") )); requests.add(new Request().setUpdateParagraphStyle(new UpdateParagraphStyleRequest() .setRange(new Range() .setStartIndex(10) .setEndIndex(20) .setTabId(TAB_ID)) .setParagraphStyle(new ParagraphStyle() .setBorderLeft(new ParagraphBorder() .setColor(new OptionalColor() .setColor(new Color() .setRgbColor(new RgbColor() .setBlue(1.0F) .setGreen(0.0F) .setRed(0.0F) ) ) ) .setDashStyle("DASH") .setPadding(new Dimension() .setMagnitude(20.0) .setUnit("PT")) .setWidth(new Dimension() .setMagnitude(15.0) .setUnit("PT") ) ) ) .setFields("borderLeft") )); BatchUpdateDocumentRequest body = new BatchUpdateDocumentRequest().setRequests(requests); BatchUpdateDocumentResponse response = docsService.documents() .batchUpdate(DOCUMENT_ID, body).execute();
Python
requests = [ { 'updateParagraphStyle': { 'range': { 'startIndex': 1, 'endIndex': 10, 'tabId': TAB_ID }, 'paragraphStyle': { 'namedStyleType': 'HEADING_1', 'spaceAbove': { 'magnitude': 10.0, 'unit': 'PT' }, 'spaceBelow': { 'magnitude': 10.0, 'unit': 'PT' } }, 'fields': 'namedStyleType,spaceAbove,spaceBelow' } }, { 'updateParagraphStyle': { 'range': { 'startIndex': 10, 'endIndex': 20, 'tabId': TAB_ID }, 'paragraphStyle': { 'borderLeft': { 'color': { 'color': { 'rgbColor': { 'blue': 1.0, 'green': 0.0, 'red': 0.0 } } }, 'dashStyle': 'DASH', 'padding': { 'magnitude': 20.0, 'unit': 'PT' }, 'width': { 'magnitude': 15.0, 'unit': 'PT' }, } }, 'fields': 'borderLeft' } } ] result = service.documents().batchUpdate( documentId=DOCUMENT_ID, body={'requests': requests}).execute()