Class Selection

选择

用户在正在进行的演示中的选择。

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

方法

方法返回类型简介
getCurrentPage()Page|null返回当前活跃的 Page;如果没有活跃的页面,则返回 null
getPageElementRange()PageElementRange|null返回所选 PageElement 实例的 PageElementRange 集合;如果没有选择 PageElement 实例,则返回 null
getPageRange()PageRange|null返回轮播界面中选定的 PageRange(即 Page 实例的集合),如果选择不是 SelectionType.PAGE 类型,则返回 null
getSelectionType()SelectionType返回 SelectionType
getTableCellRange()TableCellRange|null返回所选 TableCell 实例的 TableCellRange 集合,如果没有选择 TableCell 实例,则返回 null
getTextRange()TextRange|null返回所选的 TextRange,如果所选内容不是 SelectionType.TEXT 类型,则返回 null

详细文档

getCurrentPage()

返回当前活跃的 Page;如果没有活跃的页面,则返回 null

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

返回轮播界面中选定的 Page 实例的集合 PageRange,如果选择不是 SelectionType.PAGE 类型,则返回 null

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