-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathpresentations.go
475 lines (440 loc) · 14.6 KB
/
presentations.go
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
package snippets
import (
"fmt"
"log"
"google.golang.org/api/drive/v2"
"google.golang.org/api/slides/v1"
)
func createPresentation() string {
slidesService := getServices().Slides
// [START slides_create_presentation]
// Create a presentation and request a PresentationId.
p := &slides.Presentation{
Title: "Title",
}
presentation, err := slidesService.Presentations.Create(p).Fields(
"presentationId",
).Do()
if err != nil {
log.Fatalf("Unable to create presentation. %v", err)
}
fmt.Printf("Created presentation with ID: %s", presentation.PresentationId)
// [END slides_create_presentation]
return presentation.PresentationId
}
func copyPresentation(id string, title string) string {
driveService := getServices().Drive
// [START slides_copy_presentation]
// Copy a presentation.
file := drive.File{
Title: title,
}
presentationCopyFile, err := driveService.Files.Copy(id, &file).Do()
if err != nil {
log.Fatalf("Unable to copy presentation. %v", err)
}
presentationCopyId := presentationCopyFile.Id
// [END slides_copy_presentation]
return presentationCopyId
}
func createSlide(presentationId string) slides.BatchUpdatePresentationResponse {
slidesService := getServices().Slides
// [START slides_create_slide]
// Add a slide at index 1 using the predefined "TITLE_AND_TWO_COLUMNS" layout
// and the ID "MyNewSlide_001".
slideId := "MyNewSlide_001"
requests := []*slides.Request{{
CreateSlide: &slides.CreateSlideRequest{
ObjectId: slideId,
InsertionIndex: 1,
SlideLayoutReference: &slides.LayoutReference{
PredefinedLayout: "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.
body := &slides.BatchUpdatePresentationRequest{
Requests: requests,
}
response, err := slidesService.Presentations.BatchUpdate(presentationId, body).Do()
if err != nil {
log.Fatalf("Unable to create slide. %v", err)
}
fmt.Printf("Created slide with ID: %s", response.Replies[0].CreateSlide.ObjectId)
// [END slides_create_slide]
return *response
}
func createTextBoxWithText(presentationId string, slideId string) slides.BatchUpdatePresentationResponse {
slidesService := getServices().Slides
// [START slides_create_textbox_with_text]
// Create a new square text box, using a supplied object ID.
textBoxId := "MyTextBox_01"
pt350 := slides.Dimension{
Magnitude: 350,
Unit: "PT",
}
requests := []*slides.Request{{
// Create a new square text box, using a supplied object ID.
CreateShape: &slides.CreateShapeRequest{
ObjectId: textBoxId,
ShapeType: "TEXT_BOX",
ElementProperties: &slides.PageElementProperties{
PageObjectId: slideId,
Size: &slides.Size{
Height: &pt350,
Width: &pt350,
},
Transform: &slides.AffineTransform{
ScaleX: 1.0,
ScaleY: 1.0,
TranslateX: 350.0,
TranslateY: 100.0,
Unit: "PT",
},
},
},
}, {
// Insert text into the box, using the object ID given to it.
InsertText: &slides.InsertTextRequest{
ObjectId: textBoxId,
InsertionIndex: 0,
Text: "New Box Text Inserted",
},
}}
// Execute the requests.
body := &slides.BatchUpdatePresentationRequest{
Requests: requests,
}
response, err := slidesService.Presentations.BatchUpdate(presentationId, body).Do()
if err != nil {
log.Printf("Unable to create text box. %v", err)
}
fmt.Printf("Created text box with ID: %s", response.Replies[0].CreateShape.ObjectId)
// [END slides_create_textbox_with_text]
return *response
}
func createImage(presentationId string, slideId string) slides.BatchUpdatePresentationResponse {
slidesService := getServices().Slides
imageURL := "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"
// [START slides_create_image]
// Create a new image, using the supplied object ID, with content downloaded from imageURL.
imageId := "MyImageId_01"
emu4M := slides.Dimension{Magnitude: 4000000, Unit: "EMU"}
requests := []*slides.Request{{
CreateImage: &slides.CreateImageRequest{
ObjectId: imageId,
Url: imageURL,
ElementProperties: &slides.PageElementProperties{
PageObjectId: slideId,
Size: &slides.Size{
Height: &emu4M,
Width: &emu4M,
},
Transform: &slides.AffineTransform{
ScaleX: 1.0,
ScaleY: 1.0,
TranslateX: 100000.0,
TranslateY: 100000.0,
Unit: "EMU",
},
},
},
}}
// Execute the request.
body := &slides.BatchUpdatePresentationRequest{
Requests: requests,
}
response, err := slidesService.Presentations.BatchUpdate(presentationId, body).Do()
if err != nil {
log.Fatalf("Unable to create image. %v", err)
} else {
fmt.Printf("Created image with ID: %s", response.Replies[0].CreateImage.ObjectId)
}
// [END slides_create_image]
return *response
}
func textMerging(templatePresentationId string, dataSpreadsheetId string) []slides.BatchUpdatePresentationResponse {
slidesService := getServices().Slides
driveService := getServices().Drive
sheetsService := getServices().Sheets
responses := make([]slides.BatchUpdatePresentationResponse, 0)
// [START slides_text_merging]
// Use the Sheets API to load data, one record per row.
dataRangeNotation := "Customers!A2:M6"
sheetsResponse, _ := sheetsService.Spreadsheets.Values.Get(dataSpreadsheetId, dataRangeNotation).Do()
values := sheetsResponse.Values
// For each record, create a new merged presentation.
for _, row := range values {
customerName := row[2].(string)
caseDescription := row[5].(string)
totalPortfolio := row[11].(string)
// Duplicate the template presentation using the Drive API.
copyTitle := customerName + " presentation"
file := drive.File{
Title: copyTitle,
}
presentationFile, _ := driveService.Files.Copy(templatePresentationId, &file).Do()
presentationId := presentationFile.Id
// Create the text merge (replaceAllText) requests for this presentation.
requests := []*slides.Request{{
ReplaceAllText: &slides.ReplaceAllTextRequest{
ContainsText: &slides.SubstringMatchCriteria{
Text: "{{customer-name}}",
MatchCase: true,
},
ReplaceText: customerName,
},
}, {
ReplaceAllText: &slides.ReplaceAllTextRequest{
ContainsText: &slides.SubstringMatchCriteria{
Text: "{{case-description}}",
MatchCase: true,
},
ReplaceText: caseDescription,
},
}, {
ReplaceAllText: &slides.ReplaceAllTextRequest{
ContainsText: &slides.SubstringMatchCriteria{
Text: "{{total-portfolio}}",
MatchCase: true,
},
ReplaceText: totalPortfolio,
},
}}
// Execute the requests for this presentation.
body := &slides.BatchUpdatePresentationRequest{
Requests: requests,
}
response, _ := slidesService.Presentations.BatchUpdate(presentationId, body).Do()
// [START_EXCLUDE silent]
responses = append(responses, *response)
// [END_EXCLUDE silent]
// Count total number of replacements made.
var numReplacements int64 = 0
for _, resp := range response.Replies {
numReplacements += resp.ReplaceAllText.OccurrencesChanged
}
fmt.Printf("Created merged presentation for %s with ID %s\n", customerName, presentationId)
fmt.Printf("Replaced %d text instances.\n", numReplacements)
}
// [END slides_text_merging]
return responses
}
func imageMerging(templatePresentationId string, imageURL string, customerName string) slides.BatchUpdatePresentationResponse {
slidesService := getServices().Slides
driveService := getServices().Drive
logoURL := imageURL
customerGraphicURL := imageURL
// [START slides_image_merging]
// Duplicate the template presentation using the Drive API.
copyTitle := customerName + " presentation"
file := drive.File{
Title: copyTitle,
}
presentationFile, _ := driveService.Files.Copy(templatePresentationId, &file).Do()
presentationId := presentationFile.Id
// Create the image merge (replaceAllShapesWithImage) requests.
requests := []*slides.Request{{
ReplaceAllShapesWithImage: &slides.ReplaceAllShapesWithImageRequest{
ImageUrl: logoURL,
ReplaceMethod: "CENTER_INSIDE",
ContainsText: &slides.SubstringMatchCriteria{
Text: "{{company-logo}}",
MatchCase: true,
},
},
}, {
ReplaceAllShapesWithImage: &slides.ReplaceAllShapesWithImageRequest{
ImageUrl: customerGraphicURL,
ReplaceMethod: "CENTER_INSIDE",
ContainsText: &slides.SubstringMatchCriteria{
Text: "{{customer-graphic}}",
MatchCase: true,
},
},
}}
// Execute the requests for this presentation.
body := &slides.BatchUpdatePresentationRequest{Requests: requests}
response, _ := slidesService.Presentations.BatchUpdate(presentationId, body).Do()
// Count total number of replacements made.
var numReplacements int64 = 0
for _, resp := range response.Replies {
numReplacements += resp.ReplaceAllShapesWithImage.OccurrencesChanged
}
fmt.Printf("Created merged presentation with ID %s\n", presentationId)
fmt.Printf("Replaced %d shapes instances with images.\n", numReplacements)
// [END slides_image_merging]
return *response
}
func simpleTextReplace(presentationId string, shapeId string, replacementText string) slides.BatchUpdatePresentationResponse {
slidesService := getServices().Slides
// [START slides_simple_text_replace]
// Remove existing text in the shape, then insert the new text.
requests := []*slides.Request{{
DeleteText: &slides.DeleteTextRequest{
ObjectId: shapeId,
TextRange: &slides.Range{
Type: "All",
},
},
}, {
InsertText: &slides.InsertTextRequest{
ObjectId: shapeId,
InsertionIndex: 0,
Text: replacementText,
},
}}
// Execute the requests.
body := &slides.BatchUpdatePresentationRequest{Requests: requests}
response, _ := slidesService.Presentations.BatchUpdate(presentationId, body).Do()
fmt.Printf("Replaced text in shape with ID: %s", shapeId)
// [END slides_simple_text_replace]
return *response
}
func textStyleUpdate(presentationId string, shapeId string) slides.BatchUpdatePresentationResponse {
slidesService := getServices().Slides
// [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.
requests := []*slides.Request{{
UpdateTextStyle: &slides.UpdateTextStyleRequest{
ObjectId: shapeId,
TextRange: &slides.Range{
Type: "FIXED_RANGE",
StartIndex: ptrInt64(0),
EndIndex: ptrInt64(5),
ForceSendFields: []string{"StartIndex"},
},
Style: &slides.TextStyle{
Bold: true,
Italic: true,
},
Fields: "bold,italic",
},
}, {
UpdateTextStyle: &slides.UpdateTextStyleRequest{
ObjectId: shapeId,
TextRange: &slides.Range{
Type: "FIXED_RANGE",
StartIndex: ptrInt64(5),
EndIndex: ptrInt64(10),
},
Style: &slides.TextStyle{
FontFamily: "Times New Roman",
FontSize: &slides.Dimension{
Magnitude: 14.0,
Unit: "PT",
},
ForegroundColor: &slides.OptionalColor{
OpaqueColor: &slides.OpaqueColor{
RgbColor: &slides.RgbColor{
Blue: 1.0,
Green: 0.0,
Red: 0.0,
},
},
},
},
Fields: "foregroundColor,fontFamily,fontSize",
},
}, {
UpdateTextStyle: &slides.UpdateTextStyleRequest{
ObjectId: shapeId,
TextRange: &slides.Range{
Type: "FIXED_RANGE",
StartIndex: ptrInt64(10),
EndIndex: ptrInt64(15),
},
Style: &slides.TextStyle{
Link: &slides.Link{
Url: "www.example.com",
},
},
Fields: "link",
},
}}
// Execute the requests.
body := &slides.BatchUpdatePresentationRequest{Requests: requests}
response, _ := slidesService.Presentations.BatchUpdate(presentationId, body).Do()
fmt.Printf("Updated text style for shape with ID: %s", shapeId)
// [END slides_text_style_update]
return *response
}
func ptrInt64(i int64) *int64 {
return &i
}
func createBulletedText(presentationId string, shapeId string) slides.BatchUpdatePresentationResponse {
slidesService := getServices().Slides
// [START slides_create_bulleted_text]
// Add arrow-diamond-disc bullets to all text in the shape.
requests := []*slides.Request{{
CreateParagraphBullets: &slides.CreateParagraphBulletsRequest{
ObjectId: shapeId,
TextRange: &slides.Range{
Type: "ALL",
},
BulletPreset: "BULLET_ARROW_DIAMOND_DISC",
},
}}
// Execute the requests.
body := &slides.BatchUpdatePresentationRequest{Requests: requests}
response, _ := slidesService.Presentations.BatchUpdate(presentationId, body).Do()
fmt.Printf("Added a linked Sheets chart with ID %s", shapeId)
// [END slides_create_bulleted_text]
return *response
}
func createSheetsChart(presentationId string, pageId string, spreadsheetId string, sheetChartId int64) slides.BatchUpdatePresentationResponse {
slidesService := getServices().Slides
// [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.
emu4M := slides.Dimension{Magnitude: 4000000, Unit: "EMU"}
presentationChartId := "MyEmbeddedChart"
requests := []*slides.Request{{
CreateSheetsChart: &slides.CreateSheetsChartRequest{
ObjectId: presentationChartId,
SpreadsheetId: spreadsheetId,
ChartId: sheetChartId,
LinkingMode: "LINKED",
ElementProperties: &slides.PageElementProperties{
PageObjectId: pageId,
Size: &slides.Size{
Height: &emu4M,
Width: &emu4M,
},
Transform: &slides.AffineTransform{
ScaleX: 1.0,
ScaleY: 1.0,
TranslateX: 100000.0,
TranslateY: 100000.0,
Unit: "EMU",
},
},
},
}}
// Execute the requests.
body := &slides.BatchUpdatePresentationRequest{Requests: requests}
response, _ := slidesService.Presentations.BatchUpdate(presentationId, body).Do()
fmt.Printf("Added a linked Sheets chart with ID %s", presentationChartId)
// [END slides_create_sheets_chart]
return *response
}
func createRefreshSheetsChart(presentationId string, presentationChartId string) slides.BatchUpdatePresentationResponse {
slidesService := getServices().Slides
// [START slides_refresh_sheets_chart]
requests := []*slides.Request{{
RefreshSheetsChart: &slides.RefreshSheetsChartRequest{
ObjectId: presentationChartId,
},
}}
// Execute the requests.
body := &slides.BatchUpdatePresentationRequest{Requests: requests}
response, _ := slidesService.Presentations.BatchUpdate(presentationId, body).Do()
fmt.Printf("Refreshed a linked Sheets chart with ID %s", presentationChartId)
// [END slides_refresh_sheets_chart]
return *response
}