-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathSnippets.gs
591 lines (550 loc) · 15.7 KB
/
Snippets.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/**
* 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.
*/
const title = 'my title';
// [START slides_create_presentation]
/**
* Creates a presentation
* @returns {*} the created presentation
*/
function createPresentation() {
try {
const presentation = Slides.Presentations.create({
title: title
});
console.log('Created presentation with ID: %s', presentation.presentationId);
return presentation;
} catch (err) {
// TODO (Developer) - Handle exception
console.log('Failed with error: %s', err.error);
}
};
// [END slides_create_presentation]
// [START slides_copy_presentation]
/**
* create a presentation and copy it
* @param {string} presentationId - ID of presentation to copy
* @returns {*} the copy's presentation id
*/
function copyPresentation(presentationId) {
const copyTitle = 'Copy Title';
let copyFile = {
title: copyTitle,
parents: [{id: 'root'}]
};
try {
copyFile = Drive.Files.copy(copyFile, presentationId);
// (optional) copyFile.id can be returned directly
const presentationCopyId = copyFile.id;
return presentationCopyId;
} catch (err) {
// TODO (Developer) - Handle exception
console.log('Failed with error: %s', err.error);
}
};
// [END slides_copy_presentation]
// [START slides_create_slide]
/**
* Creates a slide
* @param {string} presentationId
* @param {string} pageId
* @returns {*}
*/
function createSlide(presentationId, pageId) {
// See Presentation.insertSlide(...) to learn how to add a slide using SlidesApp.
// http://developers.google.com/apps-script/reference/slides/presentation#appendslidelayout
const requests = [{
createSlide: {
objectId: pageId,
insertionIndex: '1',
slideLayoutReference: {
predefinedLayout: 'TITLE_AND_TWO_COLUMNS'
}
}
}];
// If you wish to populate the slide with elements, add element create requests here,
// using the pageId.
// Execute the request.
try {
const createSlideResponse = Slides.Presentations.batchUpdate({
requests: requests
}, presentationId);
console.log('Created slide with ID: %s', createSlideResponse.replies[0].createSlide.objectId);
return createSlideResponse;
} catch (err) {
// TODO (Developer) - Handle exception
console.log('Failed with error: %s', err.error);
}
};
// [END slides_create_slide]
// [START slides_create_textbox_with_text]
/**
* Create a new square textbox, using the supplied element ID.
* @param {string} presentationId
* @param {string} pageId
* @returns {*}
*/
function createTextboxWithText(presentationId, pageId) {
const elementId = 'MyTextBox_01';
const pt350 = {
magnitude: 350,
unit: 'PT'
};
const requests = [
{
createShape: {
objectId: elementId,
shapeType: 'TEXT_BOX',
elementProperties: {
pageObjectId: pageId,
size: {
height: pt350,
width: pt350
},
transform: {
scaleX: 1,
scaleY: 1,
translateX: 350,
translateY: 100,
unit: 'PT'
}
}
}
},
// Insert text into the box, using the supplied element ID.
{
insertText: {
objectId: elementId,
insertionIndex: 0,
text: 'New Box Text Inserted!'
}
}
];
// Execute the request.
try {
const createTextboxWithTextResponse = Slides.Presentations.batchUpdate({
requests: requests
}, presentationId);
const createShapeResponse = createTextboxWithTextResponse.replies[0].createShape;
console.log('Created textbox with ID: %s', createShapeResponse.objectId);
return createTextboxWithTextResponse;
} catch (err) {
// TODO (Developer) - Handle exception
console.log('Failed with error: %s', err.error);
}
};
// [END slides_create_textbox_with_text]
// [START slides_create_image]
/**
* Create a new image, using the supplied object ID, with content downloaded from imageUrl.
* @param {string} presentationId
* @param {string} pageId
* @returns {*}
*/
function createImage(presentationId, pageId) {
let requests = [];
const imageId = 'MyImage_01';
const imageUrl = 'https://www.google.com/images/branding/googlelogo/2x/' +
'googlelogo_color_272x92dp.png';
const emu4M = {
magnitude: 4000000,
unit: 'EMU'
};
requests.push({
createImage: {
objectId: imageId,
url: imageUrl,
elementProperties: {
pageObjectId: pageId,
size: {
height: emu4M,
width: emu4M
},
transform: {
scaleX: 1,
scaleY: 1,
translateX: 100000,
translateY: 100000,
unit: 'EMU'
}
}
}
});
// Execute the request.
try {
const response = Slides.Presentations.batchUpdate({
requests: requests
}, presentationId);
const createImageResponse = response.replies;
console.log('Created image with ID: %s', createImageResponse[0].createImage.objectId);
return createImageResponse;
} catch (err) {
// TODO (Developer) - Handle exception
console.log('Failed with error: %s', err.error);
}
};
// [END slides_create_image]
// [START slides_text_merging]
/**
* Use the Sheets API to load data, one record per row.
* @param {string} templatePresentationId
* @param {string} dataSpreadsheetId
* @returns {*[]}
*/
function textMerging(templatePresentationId, dataSpreadsheetId) {
let responses = [];
const dataRangeNotation = 'Customers!A2:M6';
try {
let values = SpreadsheetApp.openById(dataSpreadsheetId).getRange(dataRangeNotation).getValues();
// For each record, create a new merged presentation.
for (let i = 0; i < values.length; ++i) {
const row = values[i];
const customerName = row[2]; // name in column 3
const caseDescription = row[5]; // case description in column 6
const totalPortfolio = row[11]; // total portfolio in column 12
// Duplicate the template presentation using the Drive API.
const copyTitle = customerName + ' presentation';
let copyFile = {
title: copyTitle,
parents: [{id: 'root'}]
};
copyFile = Drive.Files.copy(copyFile, templatePresentationId);
const presentationCopyId = copyFile.id;
// Create the text merge (replaceAllText) requests for this presentation.
const requests = [{
replaceAllText: {
containsText: {
text: '{{customer-name}}',
matchCase: true
},
replaceText: customerName
}
}, {
replaceAllText: {
containsText: {
text: '{{case-description}}',
matchCase: true
},
replaceText: caseDescription
}
}, {
replaceAllText: {
containsText: {
text: '{{total-portfolio}}',
matchCase: true
},
replaceText: totalPortfolio + ''
}
}];
// Execute the requests for this presentation.
const result = Slides.Presentations.batchUpdate({
requests: requests
}, presentationCopyId);
// Count the total number of replacements made.
let numReplacements = 0;
result.replies.forEach(function(reply) {
numReplacements += reply.replaceAllText.occurrencesChanged;
});
console.log('Created presentation for %s with ID: %s', customerName, presentationCopyId);
console.log('Replaced %s text instances', numReplacements);
// [START_EXCLUDE silent]
responses.push(result.replies);
if (responses.length === values.length) { // return for the last value
return responses;
}
// [END_EXCLUDE]
}
} catch (err) {
// TODO (Developer) - Handle exception
console.log('Failed with error: %s', err.error);
}
};
// [END slides_text_merging]
// [START slides_image_merging]
/**
* Duplicate the template presentation using the Drive API.
* @param {string} templatePresentationId
* @param {string} imageUrl
* @param {string} customerName
* @returns {*}
*/
function imageMerging(templatePresentationId, imageUrl, customerName) {
const logoUrl = imageUrl;
const customerGraphicUrl = imageUrl;
const copyTitle = customerName + ' presentation';
let copyFile = {
title: copyTitle,
parents: [{id: 'root'}]
};
try {
copyFile = Drive.Files.copy(copyFile, templatePresentationId);
const presentationCopyId = copyFile.id;
// Create the image merge (replaceAllShapesWithImage) requests.
const requests = [{
replaceAllShapesWithImage: {
imageUrl: logoUrl,
imageReplaceMethod: 'CENTER_INSIDE',
containsText: {
text: '{{company-logo}}',
matchCase: true
}
}
}, {
replaceAllShapesWithImage: {
imageUrl: customerGraphicUrl,
imageReplaceMethod: 'CENTER_INSIDE',
containsText: {
text: '{{customer-graphic}}',
matchCase: true
}
}
}];
// Execute the requests for this presentation.
let batchUpdateResponse = Slides.Presentations.batchUpdate({
requests: requests
}, presentationCopyId);
let numReplacements = 0;
batchUpdateResponse.replies.forEach(function(reply) {
numReplacements += reply.replaceAllShapesWithImage.occurrencesChanged;
});
console.log('Created merged presentation with ID: %s', presentationCopyId);
console.log('Replaced %s shapes with images.', numReplacements);
return batchUpdateResponse;
} catch (err) {
// TODO (Developer) - Handle exception
console.log('Failed with error: %s', err.error);
}
};
// [END slides_image_merging]
// [START slides_simple_text_replace]
/**
* Remove existing text in the shape, then insert new text.
* @param {string} presentationId
* @param {string?} shapeId
* @param {string} replacementText
* @returns {*}
*/
function simpleTextReplace(presentationId, shapeId, replacementText) {
const requests = [{
deleteText: {
objectId: shapeId,
textRange: {
type: 'ALL'
}
}
}, {
insertText: {
objectId: shapeId,
insertionIndex: 0,
text: replacementText
}
}];
// Execute the requests.
try {
const batchUpdateResponse = Slides.Presentations.batchUpdate({
requests: requests
}, presentationId);
console.log('Replaced text in shape with ID: %s', shapeId);
return batchUpdateResponse;
} catch (err) {
// TODO (Developer) - Handle exception
console.log('Failed with error: %s', err.error);
}
};
// [END slides_simple_text_replace]
// [START slides_text_style_update]
/**
* Update the text style so that the first 5 characters are bolded
* and italicized, the next 5 are displayed in blue 14 pt Times
* New Roman font, and the next 5 are hyperlinked.
* @param {string} presentationId
* @param {string} shapeId
* @returns {*}
*/
function textStyleUpdate(presentationId, shapeId) {
const requests = [{
updateTextStyle: {
objectId: shapeId,
textRange: {
type: 'FIXED_RANGE',
startIndex: 0,
endIndex: 5
},
style: {
bold: true,
italic: true
},
fields: 'bold,italic'
}
}, {
updateTextStyle: {
objectId: shapeId,
textRange: {
type: 'FIXED_RANGE',
startIndex: 5,
endIndex: 10
},
style: {
fontFamily: 'Times New Roman',
fontSize: {
magnitude: 14,
unit: 'PT'
},
foregroundColor: {
opaqueColor: {
rgbColor: {
blue: 1.0,
green: 0.0,
red: 0.0
}
}
}
},
fields: 'foregroundColor,fontFamily,fontSize'
}
}, {
updateTextStyle: {
objectId: shapeId,
textRange: {
type: 'FIXED_RANGE',
startIndex: 10,
endIndex: 15
},
style: {
link: {
url: 'www.example.com'
}
},
fields: 'link'
}
}];
// Execute the requests.
try {
const batchUpdateResponse = Slides.Presentations.batchUpdate({
requests: requests
}, presentationId);
console.log('Updated the text style for shape with ID: %s', shapeId);
return batchUpdateResponse;
} catch (err) {
// TODO (Developer) - Handle exception
console.log('Failed with error: %s', err.error);
}
};
// [END slides_text_style_update]
// [START slides_create_bulleted_text]
/**
* Add arrow-diamond-disc bullets to all text in the shape.
*/
function createBulletedText(presentationId, shapeId) {
const requests = [{
createParagraphBullets: {
objectId: shapeId,
textRange: {
type: 'ALL'
},
bulletPreset: 'BULLET_ARROW_DIAMOND_DISC'
}
}];
// Execute the requests.
try {
const batchUpdateResponse = Slides.Presentations.batchUpdate({
requests: requests
}, presentationId);
console.log('Added bullets to text in shape with ID: %s', shapeId);
return batchUpdateResponse;
} catch (err) {
// TODO (Developer) - Handle exception
console.log('Failed with error: %s', err.error);
}
};
// [END slides_create_bulleted_text]
// [START slides_create_sheets_chart]
/**
* Embed a Sheets chart (indicated by the spreadsheetId and sheetChartId) onto
* a page in the presentation. Setting the linking mode as 'LINKED' allows the
* chart to be refreshed if the Sheets version is updated.
* @param {string} presentationId
* @param {string} pageId
* @param {string} shapeId
* @param {string} sheetChartId
* @returns {*}
*/
function createSheetsChart(presentationId, pageId, shapeId, sheetChartId) {
const emu4M = {
magnitude: 4000000,
unit: 'EMU'
};
const presentationChartId = 'MyEmbeddedChart';
const requests = [{
createSheetsChart: {
objectId: presentationChartId,
spreadsheetId: shapeId,
chartId: sheetChartId,
linkingMode: 'LINKED',
elementProperties: {
pageObjectId: pageId,
size: {
height: emu4M,
width: emu4M
},
transform: {
scaleX: 1,
scaleY: 1,
translateX: 100000,
translateY: 100000,
unit: 'EMU'
}
}
}
}];
// Execute the request.
try {
const batchUpdateResponse = Slides.Presentations.batchUpdate({
requests: requests
}, presentationId);
console.log('Added a linked Sheets chart with ID: %s', presentationChartId);
return batchUpdateResponse;
} catch (err) {
// TODO (Developer) - Handle exception
console.log('Failed with error: %s', err.error);
}
};
// [END slides_create_sheets_chart]
// [START slides_refresh_sheets_chart]
/**
* Refresh the sheets charts
* @param {string} presentationId
* @param {string} presentationChartId
* @returns {*}
*/
function refreshSheetsChart(presentationId, presentationChartId) {
const requests = [{
refreshSheetsChart: {
objectId: presentationChartId
}
}];
// Execute the request.
try {
const batchUpdateResponse = Slides.Presentations.batchUpdate({
requests: requests
}, presentationId);
console.log('Refreshed a linked Sheets chart with ID: %s', presentationChartId);
return batchUpdateResponse;
} catch (err) {
// TODO (Developer) - Handle exception
console.log('Failed with error: %s', err.error);
}
};
// [END slides_refresh_sheets_chart]