forked from googleworkspace/java-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnippets.java
More file actions
450 lines (409 loc) · 22.3 KB
/
Snippets.java
File metadata and controls
450 lines (409 loc) · 22.3 KB
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
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.File;
import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.sheets.v4.model.ValueRange;
import com.google.api.services.slides.v1.Slides;
import com.google.api.services.slides.v1.model.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Snippets {
private Slides service;
private Drive driveService;
private Sheets sheetsService;
Snippets(Slides service, Drive driveService, Sheets sheetsService) {
this.service = service;
this.driveService = driveService;
this.sheetsService = sheetsService;
}
public String createPresentation(String title) throws IOException {
Slides slidesService = this.service;
// [START slides_create_presentation]
Presentation presentation = new Presentation()
.setTitle(title);
presentation = slidesService.presentations().create(presentation)
.setFields("presentationId")
.execute();
System.out.println("Created presentation with ID: " + presentation.getPresentationId());
// [END slides_create_presentation]
return presentation.getPresentationId();
}
public String copyPresentation(String presentationId, String copyTitle) throws IOException {
Drive driveService = this.driveService;
// [START slides_copy_presentation]
File copyMetadata = new File().setName(copyTitle);
File presentationCopyFile =
driveService.files().copy(presentationId, copyMetadata).execute();
String presentationCopyId = presentationCopyFile.getId();
// [END slides_copy_presentation]
return presentationCopyId;
}
public BatchUpdatePresentationResponse createSlide(String presentationId) throws IOException {
Slides slidesService = this.service;
// [START slides_create_slide]
// Add a slide at index 1 using the predefined "TITLE_AND_TWO_COLUMNS" layout
// and the ID "MyNewSlide_001".
List<Request> requests = new ArrayList<>();
String slideId = "MyNewSlide_001";
requests.add(new Request()
.setCreateSlide(new CreateSlideRequest()
.setObjectId(slideId)
.setInsertionIndex(1)
.setSlideLayoutReference(new LayoutReference()
.setPredefinedLayout("TITLE_AND_TWO_COLUMNS"))));
// If you wish to populate the slide with elements, add create requests here,
// using the slide ID specified above.
// Execute the request.
BatchUpdatePresentationRequest body =
new BatchUpdatePresentationRequest().setRequests(requests);
BatchUpdatePresentationResponse response =
slidesService.presentations().batchUpdate(presentationId, body).execute();
CreateSlideResponse createSlideResponse = response.getReplies().get(0).getCreateSlide();
System.out.println("Created slide with ID: " + createSlideResponse.getObjectId());
// [END slides_create_slide]
return response;
}
public BatchUpdatePresentationResponse createTextBoxWithText(
String presentationId, String slideId) throws IOException {
Slides slidesService = this.service;
// [START slides_create_textbox_with_text]
// Create a new square text box, using a supplied object ID.
List<Request> requests = new ArrayList<>();
String textBoxId = "MyTextBox_01";
Dimension pt350 = new Dimension().setMagnitude(350.0).setUnit("PT");
requests.add(new Request()
.setCreateShape(new CreateShapeRequest()
.setObjectId(textBoxId)
.setShapeType("TEXT_BOX")
.setElementProperties(new PageElementProperties()
.setPageObjectId(slideId)
.setSize(new Size()
.setHeight(pt350)
.setWidth(pt350))
.setTransform(new AffineTransform()
.setScaleX(1.0)
.setScaleY(1.0)
.setTranslateX(350.0)
.setTranslateY(100.0)
.setUnit("PT")))));
// Insert text into the box, using the object ID given to it.
requests.add(new Request()
.setInsertText(new InsertTextRequest()
.setObjectId(textBoxId)
.setInsertionIndex(0)
.setText("New Box Text Inserted")));
// Execute the requests.
BatchUpdatePresentationRequest body =
new BatchUpdatePresentationRequest().setRequests(requests);
BatchUpdatePresentationResponse response =
slidesService.presentations().batchUpdate(presentationId, body).execute();
CreateShapeResponse createShapeResponse = response.getReplies().get(0).getCreateShape();
System.out.println("Created textbox with ID: " + createShapeResponse.getObjectId());
// [END slides_create_textbox_with_text]
return response;
}
public BatchUpdatePresentationResponse createImage(String presentationId,
String slideId,
GoogleCredential credential)
throws IOException {
Slides slidesService = this.service;
// [START slides_create_image]
String imageUrl = "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png";
// Create a new image, using the supplied object ID, with content downloaded from imageUrl.
List<Request> requests = new ArrayList<>();
String imageId = "MyImageId_01";
Dimension emu4M = new Dimension().setMagnitude(4000000.0).setUnit("EMU");
requests.add(new Request()
.setCreateImage(new CreateImageRequest()
.setObjectId(imageId)
.setUrl(imageUrl)
.setElementProperties(new PageElementProperties()
.setPageObjectId(slideId)
.setSize(new Size()
.setHeight(emu4M)
.setWidth(emu4M))
.setTransform(new AffineTransform()
.setScaleX(1.0)
.setScaleY(1.0)
.setTranslateX(100000.0)
.setTranslateY(100000.0)
.setUnit("EMU")))));
// Execute the request.
BatchUpdatePresentationRequest body =
new BatchUpdatePresentationRequest().setRequests(requests);
BatchUpdatePresentationResponse response =
slidesService.presentations().batchUpdate(presentationId, body).execute();
CreateImageResponse createImageResponse = response.getReplies().get(0).getCreateImage();
System.out.println("Created image with ID: " + createImageResponse.getObjectId());
// [END slides_create_image]
return response;
}
public List<BatchUpdatePresentationResponse> textMerging(
String templatePresentationId, String dataSpreadsheetId) throws IOException {
Slides slidesService = this.service;
Drive driveService = this.driveService;
Sheets sheetsService = this.sheetsService;
List<BatchUpdatePresentationResponse> responses = new ArrayList<>(5);
// [START slides_text_merging]
// Use the Sheets API to load data, one record per row.
String dataRangeNotation = "Customers!A2:M6";
ValueRange sheetsResponse = sheetsService.spreadsheets().values()
.get(dataSpreadsheetId, dataRangeNotation).execute();
List<List<Object>> values = sheetsResponse.getValues();
// For each record, create a new merged presentation.
for (List<Object> row: values) {
String customerName = row.get(2).toString(); // name in column 3
String caseDescription = row.get(5).toString(); // case description in column 6
String totalPortfolio = row.get(11).toString(); // total portfolio in column 12
// Duplicate the template presentation using the Drive API.
String copyTitle = customerName + " presentation";
File content = new File().setName(copyTitle);
File presentationFile =
driveService.files().copy(templatePresentationId, content).execute();
String presentationId = presentationFile.getId();
// Create the text merge (replaceAllText) requests for this presentation.
List<Request> requests = new ArrayList<>();
requests.add(new Request()
.setReplaceAllText(new ReplaceAllTextRequest()
.setContainsText(new SubstringMatchCriteria()
.setText("{{customer-name}}")
.setMatchCase(true))
.setReplaceText(customerName)));
requests.add(new Request()
.setReplaceAllText(new ReplaceAllTextRequest()
.setContainsText(new SubstringMatchCriteria()
.setText("{{case-description}}")
.setMatchCase(true))
.setReplaceText(caseDescription)));
requests.add(new Request()
.setReplaceAllText(new ReplaceAllTextRequest()
.setContainsText(new SubstringMatchCriteria()
.setText("{{total-portfolio}}")
.setMatchCase(true))
.setReplaceText(totalPortfolio)));
// Execute the requests for this presentation.
BatchUpdatePresentationRequest body =
new BatchUpdatePresentationRequest().setRequests(requests);
BatchUpdatePresentationResponse response =
slidesService.presentations().batchUpdate(presentationId, body).execute();
// [START_EXCLUDE silent]
responses.add(response);
// [END_EXCLUDE]
// Count total number of replacements made.
int numReplacements = 0;
for (Response resp : response.getReplies()) {
numReplacements += resp.getReplaceAllText().getOccurrencesChanged();
}
System.out.println("Created merged presentation for " +
customerName + " with ID: " + presentationId);
System.out.println("Replaced " + numReplacements + " text instances.");
}
// [END slides_text_merging]
return responses;
}
public BatchUpdatePresentationResponse imageMerging(String templatePresentationId,
String imageUrl,
String customerName) throws IOException {
Slides slidesService = this.service;
Drive driveService = this.driveService;
String logoUrl = imageUrl;
String customerGraphicUrl = imageUrl;
// [START slides_image_merging]
// Duplicate the template presentation using the Drive API.
String copyTitle = customerName + " presentation";
File content = new File().setName(copyTitle);
File presentationFile =
driveService.files().copy(templatePresentationId, content).execute();
String presentationId = presentationFile.getId();
// Create the image merge (replaceAllShapesWithImage) requests.
List<Request> requests = new ArrayList<>();
requests.add(new Request()
.setReplaceAllShapesWithImage(new ReplaceAllShapesWithImageRequest()
.setImageUrl(logoUrl)
.setReplaceMethod("CENTER_INSIDE")
.setContainsText(new SubstringMatchCriteria()
.setText("{{company-logo}}")
.setMatchCase(true))));
requests.add(new Request()
.setReplaceAllShapesWithImage(new ReplaceAllShapesWithImageRequest()
.setImageUrl(customerGraphicUrl)
.setReplaceMethod("CENTER_INSIDE")
.setContainsText(new SubstringMatchCriteria()
.setText("{{customer-graphic}}")
.setMatchCase(true))));
// Execute the requests.
BatchUpdatePresentationRequest body =
new BatchUpdatePresentationRequest().setRequests(requests);
BatchUpdatePresentationResponse response =
slidesService.presentations().batchUpdate(presentationId, body).execute();
// Count total number of replacements made.
int numReplacements = 0;
for(Response resp: response.getReplies()) {
numReplacements += resp.getReplaceAllShapesWithImage().getOccurrencesChanged();
}
System.out.println("Created merged presentation with ID: " + presentationId);
System.out.println("Replaced " + numReplacements + " shapes instances with images.");
// [END slides_image_merging]
return response;
}
public BatchUpdatePresentationResponse simpleTextReplace(
String presentationId, String shapeId, String replacementText) throws IOException {
Slides slidesService = this.service;
// [START slides_simple_text_replace]
// Remove existing text in the shape, then insert the new text.
List<Request> requests = new ArrayList<>();
requests.add(new Request()
.setDeleteText(new DeleteTextRequest()
.setObjectId(shapeId)
.setTextRange(new Range()
.setType("ALL"))));
requests.add(new Request()
.setInsertText(new InsertTextRequest()
.setObjectId(shapeId)
.setInsertionIndex(0)
.setText(replacementText)));
// Execute the requests.
BatchUpdatePresentationRequest body =
new BatchUpdatePresentationRequest().setRequests(requests);
BatchUpdatePresentationResponse response =
slidesService.presentations().batchUpdate(presentationId, body).execute();
System.out.println("Replaced text in shape with ID: " + shapeId);
// [END slides_simple_text_replace]
return response;
}
public BatchUpdatePresentationResponse textStyleUpdate(String presentationId, String shapeId)
throws IOException {
Slides slidesService = this.service;
// [START slides_text_style_update]
// Update the text style so that the first 5 characters are bolded
// and italicized, and the next 5 are displayed in blue 14 pt Times
// New Roman font, and the next five are hyperlinked.
List<Request> requests = new ArrayList<>();
requests.add(new Request()
.setUpdateTextStyle(new UpdateTextStyleRequest()
.setObjectId(shapeId)
.setTextRange(new Range()
.setType("FIXED_RANGE")
.setStartIndex(0)
.setEndIndex(5))
.setStyle(new TextStyle()
.setBold(true)
.setItalic(true))
.setFields("bold,italic")));
requests.add(new Request()
.setUpdateTextStyle(new UpdateTextStyleRequest()
.setObjectId(shapeId)
.setTextRange(new Range()
.setType("FIXED_RANGE")
.setStartIndex(5)
.setEndIndex(10))
.setStyle(new TextStyle()
.setFontFamily("Times New Roman")
.setFontSize(new Dimension()
.setMagnitude(14.0)
.setUnit("PT"))
.setForegroundColor(new OptionalColor()
.setOpaqueColor(new OpaqueColor()
.setRgbColor(new RgbColor()
.setBlue(1.0F)
.setGreen(0.0F)
.setRed(0.0F)))))
.setFields("foregroundColor,fontFamily,fontSize")));
requests.add(new Request()
.setUpdateTextStyle(new UpdateTextStyleRequest()
.setObjectId(shapeId)
.setTextRange(new Range()
.setType("FIXED_RANGE")
.setStartIndex(10)
.setEndIndex(15))
.setStyle(new TextStyle()
.setLink(new Link()
.setUrl("www.example.com")))
.setFields("link")));
// Execute the requests.
BatchUpdatePresentationRequest body =
new BatchUpdatePresentationRequest().setRequests(requests);
BatchUpdatePresentationResponse response =
slidesService.presentations().batchUpdate(presentationId, body).execute();
System.out.println("Updated text style for shape with ID: " + shapeId);
// [END slides_text_style_update]
return response;
}
public BatchUpdatePresentationResponse createBulletedText(String presentationId,
String shapeId) throws IOException {
Slides slidesService = this.service;
// [START slides_create_bulleted_text]
// Add arrow-diamond-disc bullets to all text in the shape.
List<Request> requests = new ArrayList<>();
requests.add(new Request()
.setCreateParagraphBullets(new CreateParagraphBulletsRequest()
.setObjectId(shapeId)
.setTextRange(new Range()
.setType("ALL"))
.setBulletPreset("BULLET_ARROW_DIAMOND_DISC")));
// Execute the request.
BatchUpdatePresentationRequest body =
new BatchUpdatePresentationRequest().setRequests(requests);
BatchUpdatePresentationResponse response =
slidesService.presentations().batchUpdate(presentationId, body).execute();
System.out.println("Added bullets to text in shape with ID: " + shapeId);
// [END slides_create_bulleted_text]
return response;
}
public BatchUpdatePresentationResponse createSheetsChart(
String presentationId, String pageId, String spreadsheetId, Integer sheetChartId)
throws IOException {
Slides slidesService = this.service;
// [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.
List<Request> requests = new ArrayList<>();
Dimension emu4M = new Dimension().setMagnitude(4000000.0).setUnit("EMU");
String presentationChartId = "MyEmbeddedChart";
requests.add(new Request()
.setCreateSheetsChart(new CreateSheetsChartRequest()
.setObjectId(presentationChartId)
.setSpreadsheetId(spreadsheetId)
.setChartId(sheetChartId)
.setLinkingMode("LINKED")
.setElementProperties(new PageElementProperties()
.setPageObjectId(pageId)
.setSize(new Size()
.setHeight(emu4M)
.setWidth(emu4M))
.setTransform(new AffineTransform()
.setScaleX(1.0)
.setScaleY(1.0)
.setTranslateX(100000.0)
.setTranslateY(100000.0)
.setUnit("EMU")))));
// Execute the request.
BatchUpdatePresentationRequest body =
new BatchUpdatePresentationRequest().setRequests(requests);
BatchUpdatePresentationResponse response =
slidesService.presentations().batchUpdate(presentationId, body).execute();
System.out.println("Added a linked Sheets chart with ID " + presentationChartId);
// [END slides_create_sheets_chart]
return response;
}
public BatchUpdatePresentationResponse refreshSheetsChart(
String presentationId, String presentationChartId) throws IOException {
Slides slidesService = this.service;
// [START slides_refresh_sheets_chart]
List<Request> requests = new ArrayList<>();
// Refresh an existing linked Sheets chart embedded a presentation.
requests.add(new Request()
.setRefreshSheetsChart(new RefreshSheetsChartRequest()
.setObjectId(presentationChartId)));
// Execute the request.
BatchUpdatePresentationRequest body =
new BatchUpdatePresentationRequest().setRequests(requests);
BatchUpdatePresentationResponse response =
slidesService.presentations().batchUpdate(presentationId, body).execute();
System.out.println("Refreshed a linked Sheets chart with ID " + presentationChartId);
// [END slides_refresh_sheets_chart]
return response;
}
}