プレゼンテーション内のアイテムの選択

「選択」とは、ハイライト表示されたテキストや表など、開いているプレゼンテーション ページで現在選択されている要素です。このガイドでは、Apps Script を使用して、アクティブなプレゼンテーションで選択範囲を取得および設定する方法について説明します。

これで、スクリプトが開始されたときの状態を確認できます。スクリプトの実行中にユーザーがクリックして選択内容が変更された場合、それらの変更は反映されません。

選択範囲と選択タイプ

選択範囲は Selection クラスを使用して読み取ることができます。このクラスには、選択されたオブジェクトのタイプに基づいて選択されたオブジェクトを取得するためのさまざまなメソッドがあります。

SelectionType 列挙型は、選択されたオブジェクトの特定のタイプを表します。たとえば、ユーザーがシェイプ内のテキストを選択した場合、選択タイプは TEXT になります。この場合、selection.getTextRange() メソッドを使用して、選択したテキスト範囲を取得できます。

選択範囲を含むオブジェクトを取得することもできます。上記の例では、selection.getPageElementRange().getPageElements()[0] を使用して、選択したテキストを含むシェイプを取得できます。同様に、図形を囲んでいるページは現在アクティブなページです。そのページを取得するには、selection.getCurrentPage() を使用します。

選択内容の読み取り

選択範囲を読み取るには、次の例に示すように Presentation.getSelection() メソッドを使用します。

スライド/選択/選択.gs
const selection = SlidesApp.getActivePresentation().getSelection();

現在のページを読む

ユーザーが表示している現在の Page を取得するには、次のように getSelection() メソッドと getCurrentPage() メソッドを使用します。

スライド/選択/選択.gs
const currentPage = SlidesApp.getActivePresentation().getSelection().getCurrentPage();

現在のページは次のいずれかのタイプになります。

現在のページでは 1 つ以上のオブジェクトを選択できます。SelectionType によって選択の種類が決まります。

選択タイプに基づいて選択内容を読み取る

次の例は、選択タイプを使用して、タイプに適した方法で現在の選択を読み取る方法を示しています。

スライド/選択/選択.gs
const selection = SlidesApp.getActivePresentation().getSelection();
const selectionType = selection.getSelectionType();
let currentPage;
switch (selectionType) {
  case SlidesApp.SelectionType.NONE:
    console.log('Nothing selected');
    break;
  case SlidesApp.SelectionType.CURRENT_PAGE:
    currentPage = selection.getCurrentPage();
    console.log('Selection is a page with ID: ' + currentPage.getObjectId());
    break;
  case SlidesApp.SelectionType.PAGE_ELEMENT:
    const pageElements = selection.getPageElementRange().getPageElements();
    console.log('There are ' + pageElements.length + ' page elements selected.');
    break;
  case SlidesApp.SelectionType.TEXT:
    const tableCellRange = selection.getTableCellRange();
    if (tableCellRange !== null) {
      const tableCell = tableCellRange.getTableCells()[0];
      console.log('Selected text is in a table at row ' +
        tableCell.getRowIndex() + ', column ' +
        tableCell.getColumnIndex());
    }
    const textRange = selection.getTextRange();
    if (textRange.getStartIndex() === textRange.getEndIndex()) {
      console.log('Text cursor position: ' + textRange.getStartIndex());
    } else {
      console.log('Selection is a text range from: ' + textRange.getStartIndex() + ' to: ' +
        textRange.getEndIndex() + ' is selected');
    }
    break;
  case SlidesApp.SelectionType.TABLE_CELL:
    const tableCells = selection.getTableCellRange().getTableCells();
    const table = tableCells[0].getParentTable();
    console.log('There are ' + tableCells.length + ' table cells selected.');
    break;
  case SlidesApp.SelectionType.PAGE:
    const pages = selection.getPageRange().getPages();
    console.log('There are ' + pages.length + ' pages selected.');
    break;
  default:
    break;
}

選択したテキストを読み上げています

テキスト選択部分を読み取るには、Selection.getTextRange() メソッドを使用します。テキストの選択には次の 2 種類があります。

  • 範囲の選択: シェイプに「Hello」というテキストが含まれ、「He」が選択されている場合、返される範囲は startIndex=0、endIndex=2 となります。
  • カーソルの選択: シェイプに「Hello」というテキストが含まれ、カーソルが「H」(「H|ello」)の後にある場合、返される範囲は空の範囲(startIndex=1、endIndex=1)になります。

選択内容の変更

スクリプトでユーザーの選択を変更できます。 スクリプトがプレゼンテーションに対して行った選択変更は、スクリプトの実行中に後続の選択オペレーションに反映されます。

選択内容は、スクリプトの実行が完了した後、または Presentation.saveAndClose() が呼び出された場合にのみ、ユーザーのブラウザに反映されます。

現在のページを選択する

selectAsCurrentPage() メソッドを呼び出すと、アクティブなプレゼンテーション内のページを現在のページとして選択できます。このメソッドは、前のページ要素、ページ、テキスト選択を削除します。そのため、現在のページでこのメソッドを使用すると、ページ上の現在の選択を解除できます。例:

スライド/選択/選択.gs
// Select the first slide as the current page selection and remove any previous selection.
  const selection = SlidesApp.getActivePresentation().getSelection();
  const slide = SlidesApp.getActivePresentation().getSlides()[0];
  slide.selectAsCurrentPage();
// State of selection
//
// selection.getSelectionType() = SlidesApp.SelectionType.CURRENT_PAGE
// selection.getCurrentPage() = slide
//

ページ要素を選択する

ページのページ要素を選択するには、PageElement.select() メソッドを使用します。また、以前に選択したページ要素の選択も解除されます。

例:

スライド/選択/選択.gs
  const slide = SlidesApp.getActivePresentation().getSlides()[0];
  const pageElement = slide.getPageElements()[0];
  // Only select this page element and remove any previous selection.
  pageElement.select();
// State of selection
//
// selection.getSelectionType() = SlidesApp.SelectionType.PAGE_ELEMENT
// selection.getCurrentPage() = slide
// selection.getPageElementRange().getPageElements()[0] = pageElement
//

複数のページ要素を選択する

選択範囲にページ要素を追加するには、PageElement.select(false) メソッドを使用します。すべてのページ要素が現在のページ内に存在している必要があります。

スライド/選択/選択.gs
  const slide = SlidesApp.getActivePresentation().getSlides()[0];
  // First select the slide page, as the current page selection.
  slide.selectAsCurrentPage();
  // Then select all the page elements in the selected slide page.
  const pageElements = slide.getPageElements();
  for (let i = 0; i < pageElements.length; i++) {
    pageElements[i].select(false);
  }
// State of selection
//
// selection.getSelectionType() = SlidesApp.SelectionType.PAGE_ELEMENT
// selection.getCurrentPage() = slide
// selection.getPageElementRange().getPageElements() = pageElements
//

選択範囲の変換

スクリプトが実行する編集によって現在の選択が「変換」され、編集の結果として選択内容が変化します。例:

  1. たとえば、A と B の 2 つのシェイプを選択したとします。
  2. 次に、スクリプトでシェイプ A を削除します。
  3. その結果、選択部分が編集に対して変換され、シェイプ B のみが選択されるようになります。

次の例は、選択したページ要素を操作して選択内容を変更する方法を示しています。

スライド/選択/選択.gs
  const slide = SlidesApp.getActivePresentation().getSlides()[0];
  const shape1 = slide.getPageElements()[0].asShape();
  const shape2 = slide.getPageElements()[1].asShape();
  // Select both the shapes.
  shape1.select();
  shape2.select(false);
  // State of selection
  //
  // selection.getSelectionType() = SlidesApp.SelectionType.PAGE_ELEMENT
  // selection.getCurrentPage() = slide
  // selection.getPageElementRange().getPageElements() = [shape1, shape2]
  //
  // Remove one shape.
  shape2.remove();
// State of selection
//
// selection.getSelectionType() = SlidesApp.SelectionType.PAGE_ELEMENT
// selection.getCurrentPage() = slide
// selection.getPageElementRange().getPageElements() = [shape1]
//

テキストの選択

シェイプまたは表のセルに含まれているテキストを選択するには、TextRange.select() メソッドを使用します。テキストがシェイプに含まれている場合は、そのシェイプも選択されます。 テキストが表のセルに含まれている場合は、その表のセルと、それを囲む表の両方が選択されます。

これによって親ページも現在のページに設定されます。

シェイプでの範囲の選択

次の例は、シェイプに含まれるテキスト内で範囲を選択する方法を示しています。

スライド/選択/選択.gs
  const slide = SlidesApp.getActivePresentation().getSlides()[0];
  const shape = slide.getPageElements()[0].asShape();
  shape.getText().setText('Hello');
  // Range selection: Select the text range 'He'.
  shape.getText().getRange(0, 2).select();
// State of selection
//
// selection.getSelectionType() = SlidesApp.SelectionType.TEXT
// selection.getCurrentPage() = slide
// selection.getPageElementRange().getPageElements()[0] = shape
// selection.getTextRange().getStartIndex() = 0
// selection.getTextRange().getEndIndex() = 2
//

カーソルの選択範囲

次の例は、シェイプに含まれるテキスト内でカーソルを選択する方法を示しています。

スライド/選択/選択.gs
  const slide = SlidesApp.getActivePresentation().getSlides()[0];
  const shape = slide.getPageElements()[0].asShape();
  shape.getText().setText('Hello');
  // Cursor selection: Place the cursor after 'H' like 'H|ello'.
  shape.getText().getRange(1, 1).select();
// State of selection
//
// selection.getSelectionType() = SlidesApp.SelectionType.TEXT
// selection.getCurrentPage() = slide
// selection.getPageElementRange().getPageElements()[0] = shape
// selection.getTextRange().getStartIndex() = 1
// selection.getTextRange().getEndIndex() = 1
//

表のセルでの範囲の選択

次の例は、表のセルに含まれるテキストで範囲を選択する方法を示しています。

スライド/選択/選択.gs
  const slide = SlidesApp.getActivePresentation().getSlides()[0];
  const table = slide.getPageElements()[0].asTable();
  const tableCell = table.getCell(0, 1);
  tableCell.getText().setText('Hello');
  // Range selection: Select the text range 'He'.
  tableCell.getText().getRange(0, 2).select();
// State of selection
//
// selection.getSelectionType() = SlidesApp.SelectionType.TEXT
// selection.getCurrentPage() = slide
// selection.getPageElementRange().getPageElements()[0] = table
// selection.getTableCellRange().getTableCells()[0] = tableCell
// selection.getTextRange().getStartIndex() = 0
// selection.getTextRange().getEndIndex() = 2
//

TableCell でのカーソルの選択

次の例は、表のセルに含まれるテキスト内でカーソルを選択する方法を示しています。

スライド/選択/選択.gs
  const slide = SlidesApp.getActivePresentation().getSlides()[0];
  const table = slide.getPageElements()[0].asTable();
  const tableCell = table.getCell(0, 1);
  tableCell.getText().setText('Hello');
  // Cursor selection: Place the cursor after 'H' like 'H|ello'.
  tableCell.getText().getRange(1, 1).select();
// State of selection
//
// selection.getSelectionType() = SlidesApp.SelectionType.TEXT
// selection.getCurrentPage() = slide
// selection.getPageElementRange().getPageElements()[0] = table
// selection.getTableCellRange().getTableCells()[0] = tableCell
// selection.getTextRange().getStartIndex() = 1
// selection.getTextRange().getEndIndex() = 1
//

テキスト編集を伴う選択変換

次の例は、選択したテキストを編集して選択範囲を変換する方法を示しています。

スライド/選択/選択.gs
  const slide = SlidesApp.getActivePresentation().getSlides()[0];
  const shape = slide.getPageElements()[0].asShape();
  const textRange = shape.getText();
  textRange.setText('World');
  // Select all the text 'World'.
  textRange.select();
  // State of selection
  //
  // selection.getSelectionType() = SlidesApp.SelectionType.TEXT
  // selection.getCurrentPage() = slide
  // selection.getPageElementRange().getPageElements()[0] = shape
  // selection.getTextRange().getStartIndex() = 0
  // selection.getTextRange().getEndIndex() = 6
  //
  // Add some text to the shape, and the selection will be transformed.
  textRange.insertText(0, 'Hello ');

// State of selection
//
// selection.getSelectionType() = SlidesApp.SelectionType.TEXT
// selection.getCurrentPage() = slide
// selection.getPageElementRange().getPageElements()[0] = shape
// selection.getTextRange().getStartIndex() = 0
// selection.getTextRange().getEndIndex() = 12
//

選択を解除しています

テキストまたはページ要素の選択を解除するための明示的なメソッドはありません。ただし、この結果を得るには Page.selectAsCurrentPage() メソッドまたは pageElement.select() メソッドを使用します。

現在のページを選択

次の例は、ページ上の現在の選択を解除するために、ページを現在のページとして設定しています。

スライド/選択/選択.gs
// Unselect one or more page elements already selected.
//
// In case one or more page elements in the first slide are selected, setting the
// same (or any other) slide page as the current page would do the unselect.
//
  const slide = SlidesApp.getActivePresentation().getSlides()[0];
  slide.selectAsCurrentPage();

ページ要素を選択

次の例では、1 つのページ要素を選択して、ページ上の現在の選択を解除し、他のすべてのアイテムを選択から削除する方法を示します。

スライド/選択/選択.gs
// Unselect one or more page elements already selected.
//
// In case one or more page elements in the first slide are selected,
// selecting any pageElement in the first slide (or any other pageElement) would
// do the unselect and select that pageElement.
//
  const slide = SlidesApp.getActivePresentation().getSlides()[0];
  slide.getPageElements()[0].select();