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

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

選択内容は、スクリプトの開始時のスナップショットです。スクリプトの実行中にユーザーがクリックして選択内容を変更しても、その変更は反映されません。

選択と選択タイプ

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

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

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

選択部分を読み上げる

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

slides/selection/selection.gs
const selection = SlidesApp.getActivePresentation().getSelection();

現在のページを読み上げる

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

slides/selection/selection.gs
const currentPage = SlidesApp.getActivePresentation().getSelection().getCurrentPage();

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

現在のページで 1 つ以上のオブジェクトを選択できます。SelectionType は選択のタイプを決定します。

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

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

slides/selection/selection.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() メソッドを呼び出すことで現在のページとして選択できます。このメソッドは、以前のページ要素、ページ、テキストの選択を削除します。そのため、現在のページでこのメソッドを使用すると、ページで現在選択されているものがすべて選択解除されます。次に例を示します。

slides/selection/selection.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() メソッドを使用します。また、以前に選択したページ要素の選択も解除されます。

次に例を示します。

slides/selection/selection.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) メソッドを使用します。すべてのページ要素は現在のページに存在する必要があります。

slides/selection/selection.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. 2 つの図形 A と B が選択されているとします。
  2. 次に、スクリプトは図形 A を削除します。
  3. その結果、選択範囲は編集に対して変換され、シェイプ B のみが選択されます。

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

slides/selection/selection.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() メソッドを使用して選択できます。テキストが図形に含まれている場合は、その図形も選択されます。テキストが表のセルに含まれている場合、その表のセルとそれを囲む表の両方が選択されます。

また、親ページが現在のページとして設定されます。

図形内の範囲選択

次の例は、図形に含まれるテキスト内で範囲選択を行う方法を示しています。

slides/selection/selection.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
//

図形内のカーソル選択

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

slides/selection/selection.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
//

表のセル内の範囲選択

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

slides/selection/selection.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 でのカーソル選択

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

slides/selection/selection.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
//

テキスト編集による選択変換

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

slides/selection/selection.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() メソッドを使用して実現できます。

現在のページを選択する

次の例は、ページを現在のページとして設定することで、ページ上の現在の選択をすべて選択解除する方法を示しています。

slides/selection/selection.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 つの要素を選択して、ページ上の現在の選択をすべて解除する方法を示しています。これにより、選択から他のすべてのアイテムが削除されます。

slides/selection/selection.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();