选择演示文稿中的内容

选定部分是指当前在打开的演示文稿页面中选择的内容,例如突出显示的文本或表格。本指南将向您介绍如何使用 Apps 脚本在活动演示文稿中获取和设置所选内容。

所选内容是脚本启动时情况的快照。如果用户在脚本运行期间点击并且选择发生了变化,这些更改不会反映出来。

选择和选择类型

您可以使用 Selection 类读取所选内容。该类具有多种方法来根据所选对象的类型获取所选对象。

SelectionType 枚举表示所选对象的具体类型。例如,如果用户选择了形状中的一些文本,则选择类型将为 TEXT。在这种情况下,您可以使用 selection.getTextRange() 方法检索所选文本范围。

您也可以检索包含所选文本的对象;继续上面的示例,您可以使用 selection.getPageElementRange().getPageElements()[0] 检索包含所选文本的形状。同样,包含封闭形状的页面是当前活动页面;要检索该页面,请使用 selection.getCurrentPage()

读取所选内容

如需读取所选内容,请使用 Presentation.getSelection() 方法,如以下示例所示:

幻灯片/selection/selection.gs
const selection = SlidesApp.getActivePresentation().getSelection();

正在朗读当前网页

如需检索用户正在查看的当前网页,请使用 getSelection()getCurrentPage() 方法,如下所示:

幻灯片/selection/selection.gs
const currentPage = SlidesApp.getActivePresentation().getSelection().getCurrentPage();

请注意,当前页面可以是以下任意一种:

您可以选择当前页面中的一个或多个对象,而 SelectionType 决定了选择的类型。

根据选择类型读取所选内容

以下示例展示了如何使用选择类型以适合类型的方式读取当前选择。

幻灯片/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() 方法,可将当前演示文稿中的某个页面选为当前页面。此方法会移除之前的所有页面元素、页面或文本选择。因此,通过在当前页面上使用此方法,您可以取消选择页面上当前的所有选择。例如:

幻灯片/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() 方法。 这也会取消选择之前选择的所有页面元素。

例如:

幻灯片/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) 方法。所有页面元素都必须位于当前页面中。

幻灯片/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。

以下示例展示了如何通过操纵所选页面元素来转换选择范围。

幻灯片/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() 方法选择形状或表格单元格中包含的文本。如果文本包含在形状中,则该形状也会被选中。 如果文本包含在表格单元格中,则该表格单元格及其封装表格均处于选中状态。

此操作也会将父页面设置为当前页面。

形状中的范围选择

以下示例展示了如何在形状中包含的文本中进行范围选择。

幻灯片/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
//

形状中的光标选择

以下示例展示了如何在形状中包含的文本中进行光标选择。

幻灯片/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
//

表格单元格中的范围选择

以下示例展示了如何在表格单元格中包含的文本中进行范围选择。

幻灯片/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 中选中光标

以下示例展示了如何在表格单元格中包含的文本中进行光标选择。

幻灯片/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
//

通过文本编辑实现选择转换

以下示例展示了如何通过修改所选文本来转换选择范围。

幻灯片/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() 方法实现。

选择当前网页

以下示例展示了如何通过将某个页面设置为当前页面来取消选择某个页面上的任何当前选择。

幻灯片/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();

选择页面元素

以下示例展示了如何通过选择一个页面元素来取消选择某个页面上的所有当前选定项,从而从选定项中移除所有其他项。

幻灯片/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();