選取簡報中的項目

選取項目是指目前開啟的簡報頁面中已選取的項目。 例如醒目顯示的文字或表格範圍本指南說明如何 然後使用 Apps Script 設定使用中簡報的選項。

選取範圍是指令碼開始當下的數據匯報。如果使用者 點擊、選取項目也會在指令碼執行期間變動 並不會反映

所選項目和類型

您可以使用 選取 類別此類別提供多種方法,可根據 物件類型。

SelectionType 列舉 代表所選物件的特定類型舉例來說,如果使用者 選擇了圖案中的文字後 為 TEXT。在這種情況下,您可以使用 selection.getTextRange() 方法。

您也可以擷取包含所選項目的物件。繼續進行 上述範例,您可以使用 擷取含有所選文字的形狀 selection.getPageElementRange().getPageElements()[0]。同樣地, 包含封閉圖形為目前使用中的頁面;到 請使用 selection.getCurrentPage() 擷取該網頁。

讀取所選項目

如要閱讀所選內容,請使用 Presentation.getSelection() 方法,如以下範例所示:

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

正在讀取目前頁面

若要擷取目前網頁, 使用者看到的畫面,請使用 getSelection()getCurrentPage() 方法,如下所示:

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

請注意,目前網頁可以是下列任一類型:

目前頁面可以選取一或多個物件,以及 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() 方法。 文字選取功能分為兩種類型:

  • 範圍選取:如果形狀包含「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. 假設您選取了兩個形狀 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();

選取網頁元素

以下範例說明如何取消選取網頁上任何目前選取的項目 只要選取一個網頁元素,即可移除選取範圍的所有其他項目。

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