forked from googleworkspace/browser-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnippets.js
495 lines (475 loc) · 14.2 KB
/
snippets.js
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
// Copyright 2018 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
//
// http://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.
function createPresentation(title, callback) {
// [START slides_create_presentation]
gapi.client.slides.presentations.create({
title: title
}).then((response) => {
console.log(`Created presentation with ID: ${response.result.presentationId}`);
// [START_EXCLUDE silent]
callback(response);
// [END_EXCLUDE]
});
// [END slides_create_presentation]
}
function copyPresentation(presentationId, copyTitle, callback) {
// [START slides_copy_presentation]
var request = {
name: copyTitle
};
gapi.client.drive.files.copy({
fileId: presentationId,
resource: request
}).then((driveResponse) => {
var presentationCopyId = driveResponse.result.id;
// [START_EXCLUDE silent]
callback(presentationCopyId);
// [END_EXCLUDE]
});
// [END slides_copy_presentation]
}
function createSlide(presentationId, pageId, callback) {
// [START slides_create_slide]
var 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.
gapi.client.slides.presentations.batchUpdate({
presentationId: presentationId,
requests: requests
}).then((createSlideResponse) => {
console.log(`Created slide with ID: ${createSlideResponse.result.replies[0].createSlide.objectId}`);
// [START_EXCLUDE silent]
callback(createSlideResponse);
// [END_EXCLUDE]
});
// [END slides_create_slide]
}
function createTextboxWithText(presentationId, pageId, callback) {
// [START slides_create_textbox_with_text]
// Create a new square textbox, using the supplied element ID.
var elementId = 'MyTextBox_01';
var pt350 = {
magnitude: 350,
unit: 'PT'
};
var 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.
gapi.client.slides.presentations.batchUpdate({
presentationId: presentationId,
requests: requests
}).then((createTextboxWithTextResponse) => {
var createShapeResponse = createTextboxWithTextResponse.result.replies[0].createShape;
console.log(`Created textbox with ID: ${createShapeResponse.objectId}`);
// [START_EXCLUDE silent]
callback(createTextboxWithTextResponse.result);
// [END_EXCLUDE]
});
// [END slides_create_textbox_with_text]
}
function createImage(presentationId, pageId, callback) {
var imageUrl = IMAGE_URL;
// [START slides_create_image]
// Create a new image, using the supplied object ID, with content downloaded from imageUrl.
var requests = [];
var imageId = 'MyImage_01';
var 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.
gapi.client.slides.presentations.batchUpdate({
presentationId: presentationId,
requests: requests
}).then((response) => {
var createImageResponse = response.result.replies;
console.log(`Created image with ID: ${createImageResponse[0].createImage.objectId}`);
// [START_EXCLUDE silent]
callback(createImageResponse);
// [END_EXCLUDE]
});
// [END slides_create_image]
}
function textMerging(templatePresentationId, dataSpreadsheetId, callback) {
// [START slides_text_merging]
// Use the Sheets API to load data, one record per row.
var responses = [];
var dataRangeNotation = 'Customers!A2:M6';
gapi.client.sheets.spreadsheets.values.get({
spreadsheetId: dataSpreadsheetId,
range: dataRangeNotation
}).then((sheetsResponse) => {
var values = sheetsResponse.result.values;
// For each record, create a new merged presentation.
for (var i = 0; i < values.length; ++i) {
var row = values[i];
var customerName = row[2]; // name in column 3
var caseDescription = row[5]; // case description in column 6
var totalPortfolio = row[11]; // total portfolio in column 12
// Duplicate the template presentation using the Drive API.
var copyTitle = customerName + ' presentation';
var request = {
name: copyTitle
};
gapi.client.drive.files.copy({
fileId: templatePresentationId,
requests: request
}).then((driveResponse) => {
var presentationCopyId = driveResponse.result.id;
// Create the text merge (replaceAllText) requests for this presentation.
var 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.
gapi.client.slides.presentations.batchUpdate({
presentationId: presentationCopyId,
requests: requests
}).then((batchUpdateResponse) => {
var result = batchUpdateResponse.result;
// [START_EXCLUDE silent]
responses.push(result.replies);
// [END_EXCLUDE]
// Count the total number of replacements made.
var numReplacements = 0;
for (var i = 0; i < result.replies.length; ++i) {
numReplacements += result.replies[i].replaceAllText.occurrencesChanged;
}
console.log(`Created presentation for ${customerName} with ID: ${presentationCopyId}`);
console.log(`Replaced ${numReplacements} text instances`);
// [START_EXCLUDE silent]
if (responses.length === values.length) { // callback for the last value
callback(responses);
}
// [END_EXCLUDE]
});
});
}
});
// [END slides_text_merging]
}
function imageMerging(templatePresentationId, imageUrl, customerName, callback) {
var logoUrl = imageUrl;
var customerGraphicUrl = imageUrl;
// [START slides_image_merging]
// Duplicate the template presentation using the Drive API.
var copyTitle = customerName + ' presentation';
gapi.client.drive.files.copy({
fileId: templatePresentationId,
resource: {
name: copyTitle
}
}).then((driveResponse) => {
var presentationCopyId = driveResponse.result.id;
// Create the image merge (replaceAllShapesWithImage) requests.
var requests = [{
replaceAllShapesWithImage: {
imageUrl: logoUrl,
replaceMethod: 'CENTER_INSIDE',
containsText: {
text: '{{company-logo}}',
matchCase: true
}
}
}, {
replaceAllShapesWithImage: {
imageUrl: customerGraphicUrl,
replaceMethod: 'CENTER_INSIDE',
containsText: {
text: '{{customer-graphic}}',
matchCase: true
}
}
}];
// Execute the requests for this presentation.
gapi.client.slides.presentations.batchUpdate({
presentationId: presentationCopyId,
requests: requests
}).then((batchUpdateResponse) => {
var numReplacements = 0;
for (var i = 0; i < batchUpdateResponse.result.replies.length; ++i) {
numReplacements += batchUpdateResponse.result.replies[i].replaceAllShapesWithImage.occurrencesChanged;
}
console.log(`Created merged presentation with ID: ${presentationCopyId}`);
console.log(`Replaced ${numReplacements} shapes with images.`);
// [START_EXCLUDE silent]
callback(batchUpdateResponse.result);
// [END_EXCLUDE]
});
});
// [END slides_image_merging]
}
function simpleTextReplace(presentationId, shapeId, replacementText, callback) {
// [START slides_simple_text_replace]
// Remove existing text in the shape, then insert new text.
var requests = [{
deleteText: {
objectId: shapeId,
textRange: {
type: 'ALL'
}
}
}, {
insertText: {
objectId: shapeId,
insertionIndex: 0,
text: replacementText
}
}];
// Execute the requests.
gapi.client.slides.presentations.batchUpdate({
presentationId: presentationId,
requests: requests
}).then((batchUpdateResponse) => {
console.log(`Replaced text in shape with ID: ${shapeId}`)
// [START_EXCLUDE silent]
callback(batchUpdateResponse.result);
// [END_EXCLUDE]
});
// [END slides_simple_text_replace]
}
function textStyleUpdate(presentationId, shapeId, callback) {
// [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.
var 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.
gapi.client.slides.presentations.batchUpdate({
presentationId: presentationId,
requests: requests
}).then((batchUpdateResponse) => {
console.log(`Updated the text style for shape with ID: ${shapeId}`);
// [START_EXCLUDE silent]
callback(batchUpdateResponse.result);
// [END_EXCLUDE]
});
// [END slides_text_style_update]
}
function createBulletedText(presentationId, shapeId, callback) {
// [START slides_create_bulleted_text]
// Add arrow-diamond-disc bullets to all text in the shape.
var requests = [{
createParagraphBullets: {
objectId: shapeId,
textRange: {
type: 'ALL'
},
bulletPreset: 'BULLET_ARROW_DIAMOND_DISC'
}
}];
// Execute the requests.
gapi.client.slides.presentations.batchUpdate({
presentationId: presentationId,
requests: requests
}).then((batchUpdateResponse) => {
console.log(`Added bullets to text in shape with ID: ${shapeId}`);
// [START_EXCLUDE silent]
callback(batchUpdateResponse.result);
// [END_EXCLUDE]
});
// [END slides_create_bulleted_text]
}
function createSheetsChart(presentationId, pageId, shapeId, sheetChartId, callback) {
// [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.
var emu4M = {
magnitude: 4000000,
unit: 'EMU'
};
var presentationChartId = 'MyEmbeddedChart';
var 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.
gapi.client.slides.presentations.batchUpdate({
presentationId: presentationId,
requests: requests
}).then((batchUpdateResponse) => {
console.log(`Added a linked Sheets chart with ID: ${presentationChartId}`);
// [END slides_create_sheets_chart]
callback(batchUpdateResponse.result);
});
}
function refreshSheetsChart(presentationId, presentationChartId, callback) {
// [START slides_refresh_sheets_chart]
var requests = [{
refreshSheetsChart: {
objectId: presentationChartId
}
}];
// Execute the request.
gapi.client.slides.presentations.batchUpdate({
presentationId: presentationId,
requests: requests
}).then((batchUpdateResponse) => {
console.log(`Refreshed a linked Sheets chart with ID: ${presentationChartId}`);
// [START_EXCLUDE silent]
callback(batchUpdateResponse.result);
// [END_EXCLUDE]
});
// [END slides_refresh_sheets_chart]
}