このページでは、Google Docs API を使用してテキストを書式設定する方法について説明します。
形式について
ドキュメントのテキスト コンテンツに適用できる書式設定には、次の 2 種類があります。
- フォント、色、下線などの文字書式を変更できます。
- インデントや行間隔などの段落書式を変更できます。
文字書式の変更
文字書式によって、ドキュメント内のテキスト文字のレンダリングが決まります。
書式を適用すると、基になる段落の TextStyle から継承されたデフォルトの書式がオーバーライドされます。逆に、書式設定が設定されていない文字は、引き続きその段落のスタイルを継承します。
テキストの文字書式を変更するには、batchUpdate
と UpdateTextStyleRequest を使用します。次の情報を含む Range オブジェクトを指定する必要があります。
- テキストが含まれるヘッダー、フッター、脚注(指定されていない場合は本文)を識別する
segmentId
。 - 書式設定するセグメント内のテキストの範囲を定義する
startIndex
とendIndex
。
次の例では、ヘッダーに含まれるテキストに対していくつかのテキストのスタイル設定操作を行います。
- 1 ~ 5 文字のフォントを太字斜体に設定します。
- 6 ~ 10 文字の色を
blue
14 pt 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)) .setFields("bold"))); requests.add(new Request() .setUpdateTextStyle(new UpdateTextStyleRequest() .setRange(new Range() .setStartIndex(6) .setEndIndex(10)) .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)) .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 }, 'textStyle': { 'bold': True, 'italic': True }, 'fields': 'bold,italic' } }, { 'updateTextStyle': { 'range': { 'startIndex': 6, 'endIndex': 10 }, '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 }, '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)) .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)) .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 }, '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 }, '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()