テキストの書式設定

このページでは、Google Docs API を使用してテキストの書式を設定する方法について説明します。

書式設定について

ドキュメントのテキスト コンテンツに適用できる書式設定には、次の 2 種類があります。

  • フォント、色、下線などの文字書式を変更できます。
  • インデントや行間隔など、段落の書式を変更できます。

文字の書式設定の変更

文字の書式設定によって、ドキュメント内のテキスト文字のレンダリングが決まります。

適用した書式は、基になる段落の TextStyle から継承されたデフォルトの書式をオーバーライドします。逆に、書式を設定していない文字は、引き続き段落のスタイルから継承されます。

テキストの文字書式を変更するには、UpdateTextStyleRequestbatchUpdate を使用します。次の情報を含む Range オブジェクトを指定する必要があります。

  • テキストを含むヘッダー、フッター、脚注(指定しない場合は本文)を識別する segmentId
  • 書式設定するセグメント内のテキストの範囲を定義する startIndexendIndex

次の例では、ヘッダーに含まれるテキストに対してテキストのスタイル設定操作をいくつか実行します。

  • 1 ~ 5 文字のフォントを太字斜体に設定します。
  • 6 ~ 10 文字の色を blue 14 pt Times New Roman フォントに設定します。
  • 11 ~ 15 文字を www.example.com にハイパーリンクします。

リクエストのリストを作成してから、batchUpdate を 1 回呼び出すだけで簡単に更新できます。

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