选择演示文稿中的内容

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

所做选择是脚本启动时所做内容的快照。如果用户在脚本运行期间执行点击操作,且所选内容发生变化,系统不会反映这些更改。

选择和选择类型

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

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

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

读取所选内容

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

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

读取当前页面

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

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

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

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

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

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

幻灯片/选择/选择.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() 方法,可以选择活动演示文稿中的页面作为当前页面。 此方法会移除之前的所有页面元素、页面或文本选择。因此,如果对当前页面使用此方法,您可以取消选择页面上当前的任何选定项目。例如:

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

例如:

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

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

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

幻灯片/选择/选择.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() 方法选择形状或表格单元格中包含的文本。如果文本包含在形状中,则也会选择该形状。如果文本包含在表格单元格中,则会同时选择该表格单元格及其外围表格。

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

形状中的范围选择

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

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

形状中的光标选择

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

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

表格单元格中的范围选择

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

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

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

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

使用文本修改进行选择转换

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

幻灯片/选择/选择.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() 方法实现此结果。

选择当前页面

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

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

选择页面元素

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

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