Class Selection

選取

使用者在進行中的簡報中選取的項目。

const selection = SlidesApp.getActivePresentation().getSelection();
const currentPage = selection.getCurrentPage();
const selectionType = selection.getSelectionType();

方法

方法傳回類型簡短說明
getCurrentPage()Page|null如果沒有有效頁面,則傳回目前有效的 Pagenull
getPageElementRange()PageElementRange|null傳回所選 PageElement 執行個體的 PageElementRange 集合,如果未選取任何 PageElement 執行個體,則傳回 null
getPageRange()PageRange|null傳回影格中選取的 Page 執行個體集合,或如果選取項目不是 SelectionType.PAGE 類型,則傳回 nullPageRange
getSelectionType()SelectionType傳回 SelectionType
getTableCellRange()TableCellRange|null傳回所選 TableCell 執行個體的 TableCellRange 集合,如果沒有選取任何 TableCell 執行個體,則傳回 null
getTextRange()TextRange|null傳回所選的 TextRange,或如果選取項目不是 SelectionType.TEXT 類型,則傳回 null

內容詳盡的說明文件

getCurrentPage()

如果沒有有效頁面,則傳回目前有效的 Pagenull

const selection = SlidesApp.getActivePresentation().getSelection();
const currentPage = selection.getCurrentPage();
if (currentPage != null) {
  Logger.log(`Selected current active page ID: ${currentPage.getObjectId()}`);
}

回攻員

Page|null

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

  • https://www.googleapis.com/auth/presentations.currentonly
  • https://www.googleapis.com/auth/presentations

getPageElementRange()

傳回所選 PageElement 執行個體的 PageElementRange 集合,如果沒有選取任何 PageElement 執行個體,則傳回 null

const selection = SlidesApp.getActivePresentation().getSelection();
const selectionType = selection.getSelectionType();
if (selectionType === SlidesApp.SelectionType.PAGE_ELEMENT) {
  const currentPage = selection.getCurrentPage();
  const pageElements = selection.getPageElementRange().getPageElements();
  Logger.log(`Number of page elements selected: ${pageElements.length}`);
}

回攻員

PageElementRange|null

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

  • https://www.googleapis.com/auth/presentations.currentonly
  • https://www.googleapis.com/auth/presentations

getPageRange()

傳回選取或 null 的 flimstrip 中 Page 執行個體的集合,如果選取項目不是 SelectionType.PAGE 類型,則傳回 PageRange

const selection = SlidesApp.getActivePresentation().getSelection();
const selectionType = selection.getSelectionType();
if (selectionType === SlidesApp.SelectionType.PAGE) {
  const pageRange = selection.getPageRange();
  Logger.log(
      `Number of pages in the flimstrip selected: ${
          pageRange.getPages().length}`,
  );
}

回攻員

PageRange|null

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

  • https://www.googleapis.com/auth/presentations.currentonly
  • https://www.googleapis.com/auth/presentations

getSelectionType()

傳回 SelectionType

const selection = SlidesApp.getActivePresentation().getSelection();
const selectionType = selection.getSelectionType();
if (selectionType === SlidesApp.SelectionType.CURRENT_PAGE) {
  const currentPage = selection.getCurrentPage();
  Logger.log(`Selected current active page ID: ${currentPage.getObjectId()}`);
}

回攻員

SelectionType

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

  • https://www.googleapis.com/auth/presentations.currentonly
  • https://www.googleapis.com/auth/presentations

getTableCellRange()

傳回所選 TableCell 執行個體的 TableCellRange 集合,如果沒有選取任何 TableCell 執行個體,則傳回 null

const selection = SlidesApp.getActivePresentation().getSelection();
const selectionType = selection.getSelectionType();
if (selectionType === SlidesApp.SelectionType.TABLE_CELL) {
  const currentPage = selection.getCurrentPage();
  const tableCells = selection.getTableCellRange().getTableCells();
  const table = tableCells[0].getParentTable();
  Logger.log(`Number of table cells selected: ${tableCells.length}`);
}

回攻員

TableCellRange|null

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

  • https://www.googleapis.com/auth/presentations.currentonly
  • https://www.googleapis.com/auth/presentations

getTextRange()

傳回所選的 TextRange,或如果選取項目不是 SelectionType.TEXT 類型,則傳回 null

TextRange 代表兩種情況:

1. 選取的文字範圍。舉例來說,如果形狀含有「Hello」文字,且選取「He」,則傳回的範圍會是 TextRange.getStartIndex() = 0,且 TextRange.getEndIndex() = 2。

2. 游標位置。舉例來說,如果形狀含有「Hello」文字,且游標位於「H」之後 (「H|ello」),則傳回的範圍會是 TextRange.getStartIndex() = 1 和 TextRange.getEndIndex() = 1。

const selection = SlidesApp.getActivePresentation().getSelection();
const selectionType = selection.getSelectionType();
if (selectionType === SlidesApp.SelectionType.TEXT) {
  const currentPage = selection.getCurrentPage();
  const pageElement = selection.getPageElementRange().getPageElements()[0];
  const textRange = selection.getTextRange();
  Logger.log(`Text selected: ${textRange.asString()}`);
}

回攻員

TextRange|null

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

  • https://www.googleapis.com/auth/presentations.currentonly
  • https://www.googleapis.com/auth/presentations