forked from googleworkspace/apps-script-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselection.gs
303 lines (299 loc) · 11.2 KB
/
selection.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/**
* Copyright Google LLC
*
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// [START apps_script_slides_get_selection]
const selection = SlidesApp.getActivePresentation().getSelection();
// [END apps_script_slides_get_selection]
// [START apps_script_slides_get_current_page]
const currentPage = SlidesApp.getActivePresentation().getSelection().getCurrentPage();
// [END apps_script_slides_get_current_page]
/**
* 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:
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;
}
// [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.
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]
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
//
// [END apps_script_slides_select_page_element]
}
/**
* 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
// selection.getCurrentPage() = slide
// 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]
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]
//
// [END apps_script_slides_transform_selection]
}
/**
* 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
// selection.getCurrentPage() = slide
// selection.getPageElementRange().getPageElements()[0] = shape
// selection.getTextRange().getStartIndex() = 0
// selection.getTextRange().getEndIndex() = 2
//
// [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
// selection.getCurrentPage() = slide
// selection.getPageElementRange().getPageElements()[0] = shape
// selection.getTextRange().getStartIndex() = 1
// selection.getTextRange().getEndIndex() = 1
//
// [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
// selection.getCurrentPage() = slide
// selection.getPageElementRange().getPageElements()[0] = table
// selection.getTableCellRange().getTableCells()[0] = tableCell
// selection.getTextRange().getStartIndex() = 0
// selection.getTextRange().getEndIndex() = 2
//
// [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
// selection.getCurrentPage() = slide
// selection.getPageElementRange().getPageElements()[0] = table
// selection.getTableCellRange().getTableCells()[0] = tableCell
// selection.getTextRange().getStartIndex() = 1
// selection.getTextRange().getEndIndex() = 1
//
// [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]
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
//
// [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.
//
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.
//
// 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();
// [END apps_script_slides_selecting]
}