テキストの編集とスタイル設定

TextRange タイプで表されるテキスト範囲を使用して、テキストの編集やスタイル設定を行うことができます。TextRange は、シェイプ内または表のセル内のテキスト セグメントを表します。シェイプまたは表のセルで getText() を呼び出すと、テキスト全体をカバーするテキスト範囲が返されます。

シェイプ内のテキストの配置を編集するメソッドを使用すると、シェイプに適用される自動調整設定はすべて無効になります。

テキスト範囲の使用

テキスト範囲には、テキスト範囲でカバーされるテキストのセグメントを区切る 2 つのインデックス(開始インデックスと終了インデックス)があります。これらのインデックスは、getStartIndex() 関数と getEndIndex() 関数を使用して決定できます。

テキスト範囲の内容を読み取るには、asString() 関数または asRenderedString() 関数を使用します。

テキスト範囲内から部分範囲を取得するには、getRange() 関数を使用します。

次のスクリプトは、最初のスライドにテキスト ボックスを作成し、そのテキスト コンテンツを「Hello world!」に設定します。次に、「Hello」のみを含むサブ範囲を取得します。

slides/style/style.gs
try {
  // Get the first slide of active presentation
  const slide = SlidesApp.getActivePresentation().getSlides()[0];
  // Insert shape in the slide with dimensions
  const shape = slide.insertShape(SlidesApp.ShapeType.TEXT_BOX, 100, 200, 300, 60);
  const textRange = shape.getText();
  // Set text in TEXT_BOX
  textRange.setText('Hello world!');
  console.log('Start: ' + textRange.getStartIndex() + '; End: ' +
    textRange.getEndIndex() + '; Content: ' + textRange.asString());
  const subRange = textRange.getRange(0, 5);
  console.log('Sub-range Start: ' + subRange.getStartIndex() + '; Sub-range End: ' +
    subRange.getEndIndex() + '; Sub-range Content: ' + subRange.asString());
} catch (err) {
  // TODO (developer) - Handle exception
  console.log('Failed with an error %s ', err.message);
}

図形や表のセルによって返されるテキスト範囲は、テキストを挿入したり削除したりしても、常にテキスト全体をカバーします。したがって、上記の例では次のログ ステートメントが生成されます。

Start: 0; End: 13; Content: Hello world!
Start: 0; End: 5; Content: Hello

テキストの挿入と削除

テキスト範囲を使用して、テキストの図形や表のセルを挿入したり、削除したりすることもできます。

  • insertText()appendText() を使用すると、テキストを挿入できます。
  • setText() は、テキスト範囲のテキストを指定されたテキストに置き換えます。
  • clear() は、テキスト範囲内のテキストを削除します。

次のスクリプトは、これらの関数の使用方法を示しています。

slides/style/style.gs
try {
  // Get the first slide of active presentation
  const slide = SlidesApp.getActivePresentation().getSlides()[0];
  // Insert shape in the slide with dimensions
  const shape = slide.insertShape(SlidesApp.ShapeType.TEXT_BOX, 100, 200, 300, 60);
  const textRange = shape.getText();
  textRange.setText('Hello world!');
  textRange.clear(6, 11);
  // Insert text in TEXT_BOX
  textRange.insertText(6, 'galaxy');
  console.log('Start: ' + textRange.getStartIndex() + '; End: ' +
    textRange.getEndIndex() + '; Content: ' + textRange.asString());
} catch (err) {
  // TODO (developer) - Handle exception
  console.log('Failed with an error %s ', err.message);
}

このスクリプトは、最初のスライドにテキスト ボックスを作成し、テキスト コンテンツを「Hello world!」に設定します。次に、文字 6 から 11(「world」)を削除し、代わりにインデックス 6 にテキスト「galaxy」を挿入します。上記の例では、次のログ ステートメントが生成されます。

Start: 0; End: 14; Content: Hello galaxy!

検索と置換

プレゼンテーションまたは特定のページで replaceAllText() 関数を使用して、グローバルな検索と置換を行います。

TextRange の find() 関数は、範囲内の文字列のインスタンスを返します。setText() とともに使用して、シェイプまたは表のセル内で検索と置換を行うことができます。

段落、リストアイテム、実行

TextRange は、テキスト エンティティの有用な集合を返す関数を提供します。たとえば、次のような関数です。

  • getParagraphs(),: テキスト範囲と重なるすべての段落を提供します。段落は改行文字「\n」で終わる一連のテキストです。
  • 現在のテキスト範囲内のリストアイテムを返す getListParagraphs(),
  • getRuns(),: 現在のテキスト範囲に重なるテキスト実行を提供します。テキストランは、すべての文字が同じテキスト スタイルであるテキストのセグメントです。

テキストのスタイル設定

テキスト スタイルは、プレゼンテーション内のテキスト文字(フォント、色、ハイパーリンクなど)のレンダリングを決定します。

テキスト範囲の getTextStyle() 関数には、テキストのスタイル設定に使用する TextStyle オブジェクトが用意されています。TextStyle オブジェクトは、親 TextRange と同じテキストをカバーします。

slides/style/style.gs
try {
  // Get the first slide of active presentation
  const slide = SlidesApp.getActivePresentation().getSlides()[0];
  // Insert shape in the slide with dimensions
  const shape = slide.insertShape(SlidesApp.ShapeType.TEXT_BOX, 100, 200, 300, 60);
  const textRange = shape.getText();
  // Set text in TEXT_BOX
  textRange.setText('Hello ');
  // Append text in TEXT_BOX
  const insertedText = textRange.appendText('world!');
  // Style the text with url,bold
  insertedText.getTextStyle()
      .setBold(true)
      .setLinkUrl('www.example.com')
      .setForegroundColor('#ff0000');
  const helloRange = textRange.getRange(0, 5);
  console.log('Text: ' + helloRange.asString() + '; Bold: ' + helloRange.getTextStyle().isBold());
  console.log('Text: ' + insertedText.asString() + '; Bold: ' +
    insertedText.getTextStyle().isBold());
  console.log('Text: ' + textRange.asString() + '; Bold: ' + textRange.getTextStyle().isBold());
} catch (err) {
  // TODO (developer) - Handle exception
  console.log('Failed with an error %s ', err.message);
}

上記の例では、最初に最初のスライドにテキスト ボックスを作成し、コンテンツを「Hello」に設定し、「world!」というテキストを追加しています。新しく追加されたテキストは太字になり、www.example.com にリンクされて、色が赤に設定されます。

スタイルを読み取る際に、範囲に複数の値がある場合、この関数は null を返します。したがって、上記のサンプルでは、次のログ ステートメントが生成されます。

Text: Hello; Bold: false
Text: world!; Bold: true
Text: Hello world!; Bold: null

テキストに適用できるスタイルは他にも多数あります。詳しくは、TextStyle リファレンス ドキュメントをご覧ください。

段落のスタイル設定

段落スタイルは段落全体に適用され、テキストの配置や行間隔などを設定できます。TextRange の getParagraphStyle() 関数には、親テキスト範囲に重なるすべての段落のスタイルを設定するための ParagraphStyle オブジェクトが用意されています。

次の例では、最初のスライドに 4 つの段落からなるテキスト ボックスを作成し、最初の 3 つの段落を中央揃えにしています。

slides/style/style.gs
try {
  // Get the first slide of active presentation
  const slide = SlidesApp.getActivePresentation().getSlides()[0];
  // Insert shape in the slide with dimensions
  const shape = slide.insertShape(SlidesApp.ShapeType.TEXT_BOX, 50, 50, 300, 300);
  const textRange = shape.getText();
  // Set the text in the shape/TEXT_BOX
  textRange.setText('Paragraph 1\nParagraph2\nParagraph 3\nParagraph 4');
  const paragraphs = textRange.getParagraphs();
  // Style the paragraph alignment center.
  for (let i = 0; i <= 3; i++) {
    const paragraphStyle = paragraphs[i].getRange().getParagraphStyle();
    paragraphStyle.setParagraphAlignment(SlidesApp.ParagraphAlignment.CENTER);
  }
} catch (err) {
  // TODO (developer) - Handle exception
  console.log('Failed with an error %s ', err.message);
}

リストのスタイル設定

ParagraphStyle と同様に、ListStyle を使用すると、親テキスト範囲と重なるすべての段落のスタイルを設定できます。

slides/style/style.gs
try {
  // Get the first slide of active presentation
  const slide = SlidesApp.getActivePresentation().getSlides()[0];
  // Insert shape in the slide with dimensions
  const shape = slide.insertShape(SlidesApp.ShapeType.TEXT_BOX, 50, 50, 300, 300);
  // Add and style the list
  const textRange = shape.getText();
  textRange.appendText('Item 1\n')
      .appendText('\tItem 2\n')
      .appendText('\t\tItem 3\n')
      .appendText('Item 4');
  // Preset patterns of glyphs for lists in text.
  textRange.getListStyle().applyListPreset(SlidesApp.ListPreset.DIGIT_ALPHA_ROMAN);
  const paragraphs = textRange.getParagraphs();
  for (let i = 0; i < paragraphs.length; i++) {
    const listStyle = paragraphs[i].getRange().getListStyle();
    console.log('Paragraph ' + (i + 1) + '\'s nesting level: ' + listStyle.getNestingLevel());
  }
} catch (err) {
  // TODO (developer) - Handle exception
  console.log('Failed with an error %s ', err.message);
}

上記の例では、4 つの段落を含むテキスト ボックスが最初のスライドに作成されます。2 番目の段落は 1 回インデントされ、3 番目の段落は 2 回インデントされます。次に、すべての段落にリスト プリセットを適用します。最後に、各段落のネストレベルがログに記録されます。(段落のネストレベルは、段落テキストの前にあるタブの数によって決まります)。したがって、上記のスクリプトは次のログ ステートメントを生成します。

Paragraph 1's nesting level: 0
Paragraph 2's nesting level: 1
Paragraph 3's nesting level: 2
Paragraph 4's nesting level: 0