Class Selection

Selection

The user's selection in the active presentation.

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

Methods

MethodReturn typeBrief description
getCurrentPage()PageReturns the currently active Page or null if there is no active page.
getPageElementRange()PageElementRangeReturns the PageElementRange collection of PageElement instances that are selected or null if there are no PageElement instances selected.
getPageRange()PageRangeReturns the PageRange a collection of Page instances in the flimstrip that are selected or null if the selection is not of type SelectionType.PAGE.
getSelectionType()SelectionTypeReturns the SelectionType.
getTableCellRange()TableCellRangeReturns the TableCellRange collection of TableCell instances that are selected or null if there are no TableCell instances selected.
getTextRange()TextRangeReturns the TextRange that is selected or null if the selection is not of type SelectionType.TEXT.

Detailed documentation

getCurrentPage()

Returns the currently active Page or null if there is no active page.

 var selection = SlidesApp.getActivePresentation().getSelection();
 var currentPage = selection.getCurrentPage();
 if (currentPage != null) {
   Logger.log('Selected current active page ID: ' + currentPage.getObjectId());
 }
 

Return

Page


getPageElementRange()

Returns the PageElementRange collection of PageElement instances that are selected or null if there are no PageElement instances selected.

 var selection = SlidesApp.getActivePresentation().getSelection();
 var selectionType = selection.getSelectionType();
 if (selectionType == SlidesApp.SelectionType.PAGE_ELEMENT) {
   var currentPage = selection.getCurrentPage();
   var pageElements = selection.getPageElementRange().getPageElements();
   Logger.log('Number of page elements selected: ' + pageElements.length);
 }
 

Return

PageElementRange


getPageRange()

Returns the PageRange a collection of Page instances in the flimstrip that are selected or null if the selection is not of type SelectionType.PAGE.

 var selection = SlidesApp.getActivePresentation().getSelection();
 var selectionType = selection.getSelectionType();
 if (selectionType == SlidesApp.SelectionType.PAGE) {
   var pageRange = selection.getPageRange();
   Logger.log('Number of pages in the flimstrip selected: ' + pageRange.getPages().length);
 }
 }

Return

PageRange


getSelectionType()

Returns the SelectionType.

 var selection = SlidesApp.getActivePresentation().getSelection();
 var selectionType = selection.getSelectionType();
 if (selectionType == SlidesApp.SelectionType.CURRENT_PAGE) {
   var currentPage = selection.getCurrentPage();
   Logger.log('Selected current active page ID: ' + currentPage.getObjectId());
 }
 

Return

SelectionType


getTableCellRange()

Returns the TableCellRange collection of TableCell instances that are selected or null if there are no TableCell instances selected.

 var selection = SlidesApp.getActivePresentation().getSelection();
 var selectionType = selection.getSelectionType();
 if (selectionType == SlidesApp.SelectionType.TABLE_CELL) {
   var currentPage = selection.getCurrentPage();
   var tableCells = selection.getTableCellRange().getTableCells();
   var table = tableCells[0].getParentTable();
   Logger.log('Number of table cells selected: ' + tableCells.length);
 }
 

Return

TableCellRange


getTextRange()

Returns the TextRange that is selected or null if the selection is not of type SelectionType.TEXT.

The TextRange represents two scenarios:

1. Range of text selected. For example if a shape has text "Hello", and "He" is selected, the returned range has TextRange.getStartIndex() = 0, and TextRange.getEndIndex() = 2.

2. Cursor position. For example if a shape has text "Hello", and cursor is after "H", ("H|ello"), the returned range has TextRange.getStartIndex() = 1 and TextRange.getEndIndex() = 1.

 var selection = SlidesApp.getActivePresentation().getSelection();
 var selectionType = selection.getSelectionType();
 if (selectionType == SlidesApp.SelectionType.TEXT) {
   var currentPage = selection.getCurrentPage();
   var pageElement = selection.getPageElementRange().getPageElements()[0];
   var textRange = selection.getTextRange();
   Logger.log('Text selected: ' + textRange.asString());
 }
 

Return

TextRange

发送以下问题的反馈:

此网页
Apps Script
Apps Script