Selecting items within a presentation Stay organized with collections Save and categorize content based on your preferences.
Page Summary
The selection is what is currently selected in an open presentation page, such as text or a table.
Scripts can only access the selection of the user running the script, and the selection is a snapshot from when the script started.
You can read the selection using the
Selectionclass and theSelectionTypeenum helps identify the type of selected objects.The script can modify the user's selection, and these changes are reflected in subsequent script operations and appear in the user's browser after execution or calling
saveAndClose().Text within shapes or table cells can be selected, and unselecting can be achieved by selecting a page as the current page or selecting a specific page element.
Theselection 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 getand set the selection in an active presentation using Apps Script.
A script can only access the selection of the user runningthe script.The selection is a snapshot of what it was when the script started. If the userclicks and the selection changes while the script is running, those changeswon't be reflected.
Selections and selection type
You can read the selection using theSelectionclass. The class has various methods to get the selected objects based on thetype of selected object(s).
TheSelectionType enumrepresents the specific type of selected objects. For example, if the user hasselected some text in a shape, the selection type willbeTEXT. In this case, you can retrieve the selected range of text using theselection.getTextRange() method.
You can also retrieve the object that contains the selection; continuing theexample above, you could retrieve the shape containing the selected text usingselection.getPageElementRange().getPageElements()[0]. Similarly, the page thatcontains the enclosing shape is the current active page; toretrieve that page, useselection.getCurrentPage().
Reading the selection
To read the selection, use thePresentation.getSelection()method as shown in the following example:
constselection=SlidesApp.getActivePresentation().getSelection();
Reading the current page
To retrieve the currentPage that theuser is viewing, use thegetSelection()andgetCurrentPage()methods as follows:
constcurrentPage=SlidesApp.getActivePresentation().getSelection().getCurrentPage();
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 SelectionTypedetermines 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 thecurrent selection in a type-appropriate way.
constselection=SlidesApp.getActivePresentation().getSelection();constselectionType=selection.getSelectionType();letcurrentPage;switch(selectionType){caseSlidesApp.SelectionType.NONE:console.log("Nothing selected");break;caseSlidesApp.SelectionType.CURRENT_PAGE:currentPage=selection.getCurrentPage();console.log(`Selection is a page with ID:${currentPage.getObjectId()}`);break;caseSlidesApp.SelectionType.PAGE_ELEMENT:{constpageElements=selection.getPageElementRange().getPageElements();console.log(`There are${pageElements.length} page elements selected.`);break;}caseSlidesApp.SelectionType.TEXT:{consttableCellRange=selection.getTableCellRange();if(tableCellRange!==null){consttableCell=tableCellRange.getTableCells()[0];console.log(`Selected text is in a table at row${tableCell.getRowIndex()}, column${tableCell.getColumnIndex()}`,);}consttextRange=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;}caseSlidesApp.SelectionType.TABLE_CELL:{consttableCells=selection.getTableCellRange().getTableCells();consttable=tableCells[0].getParentTable();console.log(`There are${tableCells.length} table cells selected.`);break;}caseSlidesApp.SelectionType.PAGE:{constpages=selection.getPageRange().getPages();console.log(`There are${pages.length} pages selected.`);break;}default:break;}
Reading text selections
You can read the text selection using theSelection.getTextRange() method.There are two types of text selection:
- Range selection: If a shape contains the text "Hello", and "He" isselected, the returned range has startIndex=0, and endIndex=2.
- Cursor selection: If a shape contains the text "Hello", and cursor isafter "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 reflectedin subsequent selection operations for the duration of the script execution.
The selection changes are reflected on the user's browser only after the scriptexecution completes, or whenPresentation.saveAndClose() is called.
Selecting the current page
A page in the active presentation can be selected as the current page by callingtheselectAsCurrentPage() method.This method removes any previous page element, page, or text selection. So usingthis method on the current page lets you unselect any current selections on thepage. For example:
// Select the first slide as the current page selection and remove any previous selection.constselection=SlidesApp.getActivePresentation().getSelection();constslide=SlidesApp.getActivePresentation().getSlides()[0];slide.selectAsCurrentPage();// State of selection//// selection.getSelectionType() = SlidesApp.SelectionType.CURRENT_PAGE// selection.getCurrentPage() = slide//
Selecting a page element
To select a page element in a page, use thePageElement.select() method.This also unselects any previously selected page elements.
Theselect() andselect(true)methods are equivalent.For example:
constslide=SlidesApp.getActivePresentation().getSlides()[0];constpageElement=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//
Selecting multiple page elements
To append additional page elements to the selection, use thePageElement.select(false) method.All the page elements must be in the current page.
constslide=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.constpageElements=slide.getPageElements();for(leti=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//
Transforming the selection
Edits that your script performs cantransform the current selection, so thatwhat'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 onlyshape B is selected.
The following example shows how the selection can be transformed by manipulatingselected page elements.
constslide=SlidesApp.getActivePresentation().getSlides()[0];constshape1=slide.getPageElements()[0].asShape();constshape2=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]//
Selecting text
Text contained in a shape or in a table cell can be selected using theTextRange.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 enclosingtable 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 containedin a shape.
constslide=SlidesApp.getActivePresentation().getSlides()[0];constshape=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//
Cursor selection in a shape
The following example shows how to make a cursor selection within text containedin a shape.
constslide=SlidesApp.getActivePresentation().getSlides()[0];constshape=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//
Range selection in a table cell
The following example shows how to make a range selection within text containedin a table cell.
constslide=SlidesApp.getActivePresentation().getSlides()[0];consttable=slide.getPageElements()[0].asTable();consttableCell=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//
Cursor selection in TableCell
The following example shows how to make a cursor selection within text containedin a table cell.
constslide=SlidesApp.getActivePresentation().getSlides()[0];consttable=slide.getPageElements()[0].asTable();consttableCell=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//
Selection transformation with textual edits
The following example shows how the selection can be transformed by editing theselected text.
constslide=SlidesApp.getActivePresentation().getSlides()[0];constshape=slide.getPageElements()[0].asShape();consttextRange=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//
Unselecting
There are no explicit methods to unselect text or page elements. However, thisresult can be achieved using thePage.selectAsCurrentPage() orpageElement.select() methods.
Select a current page
The following example shows how to unselect any current selections on a pageby setting that page as the current page.
// 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.//constslide=SlidesApp.getActivePresentation().getSlides()[0];slide.selectAsCurrentPage();
Select a page element
The following example shows how to unselect any current selections on a pageby selecting one page element, thus removing all other items from the selection.
// 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.//constslide=SlidesApp.getActivePresentation().getSlides()[0];slide.getPageElements()[0].select();
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-11 UTC.