-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslides.js
80 lines (71 loc) · 2.64 KB
/
slides.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
// Utility functions for google slides documents
function getLinkFromGoogleId(googleId) {
return 'https://drive.google.com/open?id=' + googleId;
}
/**
* Given an array of shapes, substitute any occurrence of {{match}} by another String value defined in data object as match: value
*
* @param shapes {shape[]}
* @param data {Object} Object of { match : 'value' } properties
*/
function replaceShapeMatches(shapes, data) {
shapes.forEach(function(shape) {
Object.keys(data).forEach(function(key) {
shape.getText().replaceAllText('{{' + key + '}}', data[key]);
});
});
}
/**
* Given a slide and a data object with { match: value } properties, substitute any occurrence of {{match}} by value
*
* @param slide {slide}
* @param data {Object} Object of { match : 'value' } properties
*/
function replaceMatchesInSlide(slide, data) {
const shapes = slide.getShapes();
replaceShapeMatches(shapes, data);
}
/**
* Given an array of images, substitute any image whose title matches given string by image provided by matching URL
*
* @param images {images[]}
* @param data {Object} Object of { match : 'value' } properties
* @param imageFolderId { String } Driver folder id where images are stored
*/
function replaceImageMatches(images, data, imageFolderId) {
var keys = Object.keys(data);
var imageFolder = DriveApp.getFolderById(imageFolderId);
for (var i = 0; i < images.length; i++) {
for (var j = 0; j < keys.length; j++) {
if (images[i].getTitle() === keys[j]) {
var driveImageBlob = imageFolder
.getFilesByName(data[keys[j]])
.next()
.getBlob();
images[i].replace(driveImageBlob);
}
}
}
}
/**
* Given a slide, substitute the source of any of its images whose title matches given string, by image provided by name.
* E.g. Given slide with image with alt title "myImage" and data object { myImage: imageName }, image will be substituted by any image named imageName accessible in the Driver folder defined by imageFolderId
*
* @param slide {slide}
* @param data {Object} Object of { title : URL } properties
* @param imageFolderId { String } Driver folder id where images are stored
*/
function replaceImagesInSlide(slide, data, imageFolderId) {
// const keys = Object.keys(data);
const images = slide.getImages();
const imageFolder = DriveApp.getFolderById(imageFolderId);
images.forEach(function(image) {
const replacementValue = data[image.getTitle()];
if (replacementValue) {
const driveImages = imageFolder.getFilesByName(replacementValue);
if (driveImages.hasNext()) {
image.replace(driveImages.next().getBlob());
}
}
});
}