选择演示文稿中的内容

selection 是指当前在打开的演示文稿页面中选择的内容, 例如突出显示的文本或表格本指南将为您介绍如何 然后使用 Apps 脚本在当前演示文稿中设置选定范围。

所选内容是脚本启动时情况的快照。如果用户 选择会在脚本运行期间发生改变, 不会反映。

选择和选择类型

您可以使用 选择 类。该类具有各种方法来根据 所选对象的类型。

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