选择演示文稿中的项目

“选择”是指在打开的演示文稿页面上处于焦点的内容,例如一段突出显示的文本或一个表格。 本指南介绍了如何使用 Apps 脚本在活跃演示文稿中获取和设置选择。

脚本只能访问运行该脚本的用户的选择。

选择是脚本启动时的快照。如果用户在脚本运行时点击,并且选择发生更改,这些更改将不会反映出来。

选择和选择类型

使用 Selection 类读取选择。该类有多种方法,可根据所选对象类型获取所选对象。

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

您还可以检索包含选择的对象。例如,您可以使用 selection.getPageElementRange().getPageElements()[0] 检索包含所选文本的形状。同样,包含封闭形状的页面是当前活跃页面;如需检索该页面,请使用 selection.getCurrentPage()

读取选择

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

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

读取当前页面

如需检索用户当前正在查看的 Page,请使用 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() 时,选择更改才会反映在用户的浏览器中。

选择当前页面

通过调用 the 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() 方法。 此方法还会取消选择任何之前选择的页面元素。

select()select(true) 方法是等效的。

例如:

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