diff --git a/slides/selection/selection.gs b/slides/selection/selection.gs index ce055104e..9880b285e 100644 --- a/slides/selection/selection.gs +++ b/slides/selection/selection.gs @@ -15,76 +15,88 @@ */ // [START apps_script_slides_get_selection] -var selection = SlidesApp.getActivePresentation().getSelection(); +const selection = SlidesApp.getActivePresentation().getSelection(); // [END apps_script_slides_get_selection] // [START apps_script_slides_get_current_page] -var currentPage = SlidesApp.getActivePresentation().getSelection().getCurrentPage(); +const currentPage = SlidesApp.getActivePresentation().getSelection().getCurrentPage(); // [END apps_script_slides_get_current_page] -// [START apps_script_slides_selection_type] -var selection = SlidesApp.getActivePresentation().getSelection(); -var selectionType = selection.getSelectionType(); -var currentPage; -switch (selectionType) { - case SlidesApp.SelectionType.NONE: - Logger.log('Nothing selected'); - break; - case SlidesApp.SelectionType.CURRENT_PAGE: - currentPage = selection.getCurrentPage(); - Logger.log('Selection is a page with ID: ' + currentPage.getObjectId()); - break; - case SlidesApp.SelectionType.PAGE_ELEMENT: - var pageElements = selection.getPageElementRange().getPageElements(); - Logger.log('There are ' + pageElements.length + ' page elements selected.'); - break; - case SlidesApp.SelectionType.TEXT: - var tableCellRange = selection.getTableCellRange(); - if (tableCellRange != null) { - var tableCell = tableCellRange.getTableCells()[0]; - Logger.log('Selected text is in a table at row ' + - tableCell.getRowIndex() + ', column ' + - tableCell.getColumnIndex()); - } - var textRange = selection.getTextRange(); - if (textRange.getStartIndex() == textRange.getEndIndex()) { - Logger.log('Text cursor position: ' + textRange.getStartIndex()); - } else { - Logger.log('Selection is a text range from: ' + textRange.getStartIndex() + ' to: ' + - textRange.getEndIndex() + ' is selected'); - } - break; - case SlidesApp.SelectionType.TABLE_CELL: - var tableCells = selection.getTableCellRange().getTableCells(); - var table = tableCells[0].getParentTable(); - Logger.log('There are ' + tableCells.length + ' table cells selected.'); - break; - case SlidesApp.SelectionType.PAGE: - var pages = selection.getPageRange().getPages(); - Logger.log('There are ' + pages.length + ' pages selected.'); - break; - default: - break; -} +/** + * Selection type to read the current selection in a type-appropriate way. + */ +function slidesSelectionTypes() { + // [START apps_script_slides_selection_type] + const selection = SlidesApp.getActivePresentation().getSelection(); + const selectionType = selection.getSelectionType(); + let currentPage; + switch (selectionType) { + case SlidesApp.SelectionType.NONE: + Logger.log('Nothing selected'); + break; + case SlidesApp.SelectionType.CURRENT_PAGE: + currentPage = selection.getCurrentPage(); + Logger.log('Selection is a page with ID: ' + currentPage.getObjectId()); + break; + case SlidesApp.SelectionType.PAGE_ELEMENT: + const pageElements = selection.getPageElementRange().getPageElements(); + Logger.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]; + Logger.log('Selected text is in a table at row ' + + tableCell.getRowIndex() + ', column ' + + tableCell.getColumnIndex()); + } + const textRange = selection.getTextRange(); + if (textRange.getStartIndex() === textRange.getEndIndex()) { + Logger.log('Text cursor position: ' + textRange.getStartIndex()); + } else { + Logger.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(); + Logger.log('There are ' + tableCells.length + ' table cells selected.'); + break; + case SlidesApp.SelectionType.PAGE: + const pages = selection.getPageRange().getPages(); + Logger.log('There are ' + pages.length + ' pages selected.'); + break; + default: + break; + } // [END apps_script_slides_selection_type] - +} +/** + * Selecting the current page + */ +function slideSelect() { // [START apps_script_slides_select] // Select the first slide as the current page selection and remove any previous selection. -var selection = SlidesApp.getActivePresentation().getSelection(); -var slide = SlidesApp.getActivePresentation().getSlides()[0]; -slide.selectAsCurrentPage(); + 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 // // [END apps_script_slides_select] - +} +/** + * Selecting a page element. + */ +function selectPageElement() { // [START apps_script_slides_select_page_element] -var slide = SlidesApp.getActivePresentation().getSlides()[0]; -var pageElement = slide.getPageElements()[0]; -// Only select this page element and remove any previous selection. -pageElement.select(); + 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 @@ -92,16 +104,20 @@ pageElement.select(); // selection.getPageElementRange().getPageElements()[0] = pageElement // // [END apps_script_slides_select_page_element] - -// [START apps_script_slides_select_multiple_page_elements] -var 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. -var pageElements = slide.getPageElements(); -for (var i = 0; i < pageElements.length; i++) { - pageElements[i].select(false); } +/** + * Selecting multiple page elements + */ +function selectMultiplePageElement() { +// [START apps_script_slides_select_multiple_page_elements] + 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 @@ -109,22 +125,27 @@ for (var i = 0; i < pageElements.length; i++) { // selection.getPageElementRange().getPageElements() = pageElements // // [END apps_script_slides_select_multiple_page_elements] - +} +/** + *This shows how selection can be transformed by manipulating + * selected page elements. + */ +function slideTransformSelection() { // [START apps_script_slides_transform_selection] -var slide = SlidesApp.getActivePresentation().getSlides()[0]; -var shape1 = slide.getPageElements()[0].asShape(); -var 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(); + 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 @@ -132,13 +153,17 @@ shape2.remove(); // selection.getPageElementRange().getPageElements() = [shape1] // // [END apps_script_slides_transform_selection] - -// [START apps_script_slides_range_selection] -var slide = SlidesApp.getActivePresentation().getSlides()[0]; -var shape = slide.getPageElements()[0].asShape(); -shape.getText().setText('Hello'); -// Range selection: Select the text range 'He'. -shape.getText().getRange(0, 2).select(); +} +/** + * Range selection within text contained in a shape. + */ +function slidesRangeSelection() { +// [START apps_script_slides_range_selection_in_shape] + 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 @@ -147,14 +172,18 @@ shape.getText().getRange(0, 2).select(); // selection.getTextRange().getStartIndex() = 0 // selection.getTextRange().getEndIndex() = 2 // -// [END apps_script_slides_range_selection] - -// [START apps_script_slides_cursor_selection] -var slide = SlidesApp.getActivePresentation().getSlides()[0]; -var 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(); +// [END apps_script_slides_range_selection_in_shape] +} +/** + * Cursor selection within text contained in a shape. + */ +function slidesCursorSelection() { +// [START apps_script_slides_cursor_selection_in_shape] + 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 @@ -163,15 +192,19 @@ shape.getText().getRange(1, 1).select(); // selection.getTextRange().getStartIndex() = 1 // selection.getTextRange().getEndIndex() = 1 // -// [END apps_script_slides_cursor_selection] - -// [START apps_script_slides_range_selection] -var slide = SlidesApp.getActivePresentation().getSlides()[0]; -var table = slide.getPageElements()[0].asTable(); -var tableCell = table.getCell(0, 1); -tableCell.getText().setText('Hello'); -// Range selection: Select the text range 'He'. -tableCell.getText().getRange(0, 2).select(); +// [END apps_script_slides_cursor_selection_in_shape] +} +/** + * Range selection in table cell. + */ +function slideRangeSelection() { +// [START apps_script_slides_range_selection_in_table] + 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 @@ -181,15 +214,19 @@ tableCell.getText().getRange(0, 2).select(); // selection.getTextRange().getStartIndex() = 0 // selection.getTextRange().getEndIndex() = 2 // -// [END apps_script_slides_range_selection] - -// [START apps_script_slides_cursor_selection] -var slide = SlidesApp.getActivePresentation().getSlides()[0]; -var table = slide.getPageElements()[0].asTable(); -var 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(); +// [END apps_script_slides_range_selection_in_table] +} +/** + * Cursor selection in table cell. + */ +function cursorSelection() { +// [START apps_script_slides_cursor_selection_in_table] + 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 @@ -199,25 +236,29 @@ tableCell.getText().getRange(1, 1).select(); // selection.getTextRange().getStartIndex() = 1 // selection.getTextRange().getEndIndex() = 1 // -// [END apps_script_slides_cursor_selection] - +// [END apps_script_slides_cursor_selection_in_table] +} +/** + * This shows how the selection can be transformed by editing the selected text. + */ +function selectTransformation() { // [START apps_script_slides_selection_transformation] -var slide = SlidesApp.getActivePresentation().getSlides()[0]; -var shape = slide.getPageElements()[0].asShape(); -var 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 '); + 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 // @@ -228,17 +269,27 @@ textRange.insertText(0, 'Hello '); // selection.getTextRange().getEndIndex() = 12 // // [END apps_script_slides_selection_transformation] - +} +/** + * The following example shows how to unselect any current selections on a page + * by setting that page as the current page. + */ +function slidesUnselectingCurrentPage() { // [START apps_script_slides_unselecting] // 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. // -var slide = SlidesApp.getActivePresentation().getSlides()[0]; -slide.selectAsCurrentPage(); + const slide = SlidesApp.getActivePresentation().getSlides()[0]; + slide.selectAsCurrentPage(); // [END apps_script_slides_unselecting] - +} +/** + * 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. + */ +function slideUnselectingPageElements() { // [START apps_script_slides_selecting] // Unselect one or more page elements already selected. // @@ -246,6 +297,7 @@ slide.selectAsCurrentPage(); // selecting any pageElement in the first slide (or any other pageElement) would // do the unselect and select that pageElement. // -var slide = SlidesApp.getActivePresentation().getSlides()[0]; -slide.getPageElements()[0].select(); + const slide = SlidesApp.getActivePresentation().getSlides()[0]; + slide.getPageElements()[0].select(); // [END apps_script_slides_selecting] +}