The selection is whatever is currently selected in an open presentation page, such as a span of highlighted text or a table. This guide tells you how to get and set the selection in an active presentation using Apps Script.
The selection is a snapshot of what it was when the script started. If the user clicks and the selection changes while the script is running, those changes won't be reflected.
Selections and selection type
You can read the selection using the Selection class. The class has various methods to get the selected objects based on the type of selected object(s).
The SelectionType enum
represents the specific type of selected objects. For example, if the user has
selected some text in a shape, the selection type will
be TEXT. In this case, you can retrieve the selected range of text using the
selection.getTextRange()
method.
You can also retrieve the object that contains the selection; continuing the
example above, you could retrieve the shape containing the selected text using
selection.getPageElementRange().getPageElements()[0]
. Similarly, the page that
contains the enclosing shape is the current active page; to
retrieve that page, use selection.getCurrentPage()
.
Reading the selection
To read the selection, use the Presentation.getSelection() method as shown in the following example:
Reading the current page
To retrieve the current Page that the user is viewing, use the getSelection() and getCurrentPage() methods as follows:
Note that the current page may be any one of the following types:
The current page can have one or more objects selected, and the SelectionType determines the type of selection.
Reading the selection based on the selection type
The following example shows how you can use the selection type to read the current selection in a type-appropriate way.
Reading text selections
You can read the text selection using the Selection.getTextRange() method. There are two types of text selection:
- Range selection: If a shape contains the text "Hello", and "He" is selected, the returned range has startIndex=0, and endIndex=2.
- Cursor selection: If a shape contains the text "Hello", and cursor is after "H" ("H|ello"), the returned range is empty range with startIndex=1 and endIndex=1.
Modifying the selection
The script can modify the user's selection. Any selection changes that the script makes to the presentation are reflected in subsequent selection operations for the duration of the script execution.
The selection changes are reflected on the user's browser only after the script
execution completes, or when Presentation.saveAndClose()
is called.
Selecting the current page
A page in the active presentation can be selected as the current page by calling the selectAsCurrentPage() method. This method removes any previous page element, page, or text selection. So using this method on the current page lets you unselect any current selections on the page. For example:
Selecting a page element
To select a page element in a page, use the PageElement.select() method. This also unselects any previously selected page elements.
For example:
Selecting multiple page elements
To append additional page elements to the selection, use the PageElement.select(false) method. All the page elements must be in the current page.
Transforming the selection
Edits that your script performs can transform the current selection, so that what's selected changes as a result of the edit. For example:
- Suppose you have two shapes A and B selected.
- Next your script removes shape A.
- As a result, the selection is transformed against the edit so that only shape B is selected.
The following example shows how the selection can be transformed by manipulating selected page elements.
Selecting text
Text contained in a shape or in a table cell can be selected using the TextRange.select() method. If the text is contained in a shape, then that shape is also selected. If the text is contained in a table cell, then that table cell and its enclosing table are both selected.
This also sets the parent page as the current page.
Range selection in a shape
The following example shows how to make a range selection within text contained in a shape.
Cursor selection in a shape
The following example shows how to make a cursor selection within text contained in a shape.
Range selection in a table cell
The following example shows how to make a range selection within text contained in a table cell.
Cursor selection in TableCell
The following example shows how to make a cursor selection within text contained in a table cell.
Selection transformation with textual edits
The following example shows how the selection can be transformed by editing the selected text.
Unselecting
There are no explicit methods to unselect text or page elements. However, this
result can be achieved using the Page.selectAsCurrentPage()
or
pageElement.select()
methods.
Select a current page
The following example shows how to unselect any current selections on a page by setting that page as the current page.
Select a page element
The following example shows how to unselect any current selections on a page by selecting one page element, thus removing all other items from the selection.