-
Notifications
You must be signed in to change notification settings - Fork 5
/
integrationTypeImageLibrary.gs
399 lines (325 loc) · 10.5 KB
/
integrationTypeImageLibrary.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
/**
* @fileoverview Code related to information that the Odo Add-on
* may show in various contexts if the chosen integration type is
* INTEGRATION_TYPE.IMG_LIBRARY
*/
const PROP_SELECTED_IMG_LIBRARY_REPO = 'PROP_SELECTED_IMG_LIBRARY_REPO';
/**
* Function that returns default configuration fields and values for
* an integration type of INTEGRATION_TYPE.IMG_LIBRARY, to be stored
* as the 'integrationData' field in 'config'. Called when setting
* up default configuration.
*
* @return {Object}
*/
function imgLibraryGetDefaultConfig() {
let integrationData = {
repos: [
{
name: 'Finance',
driveFolderUrl: 'https://drive.google.com/drive/folders/'
+ '1XmcEiWzyiYNIOQuMr0WuuqLonDXeL6mb'
},
{
name: 'HR',
driveFolderUrl: 'https://drive.google.com/drive/folders/'
+ '1g4VlalVo3W32wSeYp5u0-3Jy1WOp5dPe'
},
{
name: '',
driveFolderUrl: ''
},
]
};
return integrationData;
}
/**
* Creates and returns the card that gives the user options to configure
* the Image Library integration. Called from integrationTypeAll.gs based
* on the value of the 'buildConfigureIntegrationCard' parameter.
*
* @return {CardService.Card}
*/
function buildImgLibraryConfigureCard() {
let config = getConfig();
let integrationData;
integrationData = getConfigIntegrationData(INTEGRATION_TYPE.IMAGE_LIBRARY);
let repos = integrationData.repos;
let repoName1 = integrationData.repos[0].name;
let driveFolderUrl1 = repos[0].driveFolderUrl;
let repoName2 = integrationData.repos[1].name;
let driveFolderUrl2 = repos[1].driveFolderUrl;
let repoName3 = integrationData.repos[2].name;
let driveFolderUrl3 = repos[2].driveFolderUrl;
let card = CardService.newCardBuilder();
let section = CardService.newCardSection();
let input;
section.addWidget(CardService.newTextParagraph().setText('Repo #1:'));
input = CardService.newTextInput()
.setFieldName('repoName1')
.setTitle('Repo Name')
.setValue(repoName1);
section.addWidget(input);
input = CardService.newTextInput()
.setFieldName('driveFolderUrl1')
.setTitle('Drive Folder Url')
.setValue(driveFolderUrl1);
section.addWidget(input);
section.addWidget(
CardService.newTextParagraph().setText('<br><br>Repo #2:')
);
input = CardService.newTextInput()
.setFieldName('repoName2')
.setTitle('Repo Name')
.setValue(repoName2);
section.addWidget(input);
input = CardService.newTextInput()
.setFieldName('driveFolderUrl2')
.setTitle('Drive Folder Url')
.setValue(driveFolderUrl2);
section.addWidget(input);
section.addWidget(
CardService.newTextParagraph().setText('<br><br>Repo #3:')
);
input = CardService.newTextInput()
.setFieldName('repoName3')
.setTitle('Repo Name')
.setValue(repoName3);
section.addWidget(input);
input = CardService.newTextInput()
.setFieldName('driveFolderUrl3')
.setTitle('Drive Folder Url')
.setValue(driveFolderUrl3);
section.addWidget(input);
card.addSection(section);
return card;
}
/**
* Function that gets called for this particular integration when user
* clicks '← Done' button in integration configuration card. Saves the
* selections and returns them as an object to be stored in the
* 'integrationData' field of the config object if/when the user saves their
* configurations.
*
* This is the handler that's defined as
* 'saveConfigureIntegrationSelections' in integrationTypeAll.gs.
*
* @param {object} formInputs - Contains user selections
*
* @return {object}
*/
function saveImgLibraryConfigureSelections(formInputs) {
let repoName1 = '';
let driveFolderUrl1 = '';
let repoName2 = '';
let driveFolderUrl2 = '';
let repoName3 = '';
let driveFolderUrl3 = '';
if (formInputs.hasOwnProperty('repoName1')
&& formInputs.hasOwnProperty('driveFolderUrl1')) {
repoName1 = formInputs['repoName1'].stringInputs.value[0];
driveFolderUrl1 = formInputs['driveFolderUrl1'].stringInputs.value[0];
}
if (formInputs.hasOwnProperty('repoName2')
&& formInputs.hasOwnProperty('driveFolderUrl2')) {
repoName2 = formInputs['repoName2'].stringInputs.value[0];
driveFolderUrl2 = formInputs['driveFolderUrl2'].stringInputs.value[0];
}
if (formInputs.hasOwnProperty('repoName3')
&& formInputs.hasOwnProperty('driveFolderUrl3')) {
repoName3 = formInputs['repoName3'].stringInputs.value[0];
driveFolderUrl3 = formInputs['driveFolderUrl3'].stringInputs.value[0];
}
console.log(repoName1);
let integrationData = {
repos: [
{
name: repoName1,
driveFolderUrl: driveFolderUrl1
},
{
name: repoName2,
driveFolderUrl: driveFolderUrl2
},
{
name: repoName3,
driveFolderUrl: driveFolderUrl3
},
]
};
return integrationData;
}
/**
* Function used to return the image library data as a formatted Card to be
* displayed. Called from integrationTypeAll.gs as a context specific
* handler for this integration.
*
* @param {string} Calling context (i.e. CALL_CONTEXT.SLIDES)
*
* @return {Card}
*/
function buildImgLibraryCard(context) {
let up = PropertiesService.getUserProperties();
let config = getConfig();
let card = CardService.newCardBuilder();
let brandedHeader = buildCustomerBrandedHeader();
card.setHeader(brandedHeader);
let repos = config.integrationData.repos;
if (repos.length === 0) {
let section = CardService.newCardSection();
section.addWidget(CardService.newTextParagraph()
.setText("No image library repositories have been configured"));
card.addSection(section);
return card.build;
}
// gather info on the currently selected (or default) image repo
let selectedRepoUrl = up.getProperty(PROP_SELECTED_IMG_LIBRARY_REPO);
let resourceKey;
if (!selectedRepoUrl) {
selectedRepoId = _extractFolderIdFromUrl(repos[0].driveFolderUrl);
resourceKey = _extractResourceKeyFromUrl(repos[0].driveFolderUrl);
name = repos[0].name;
} else {
// get all the info on the previously selected repo
for (let i=0; i < repos.length; i++) {
if (repos[i].driveFolderUrl
&& repos[i].driveFolderUrl === selectedRepoUrl) {
selectedRepoId = _extractFolderIdFromUrl(repos[i].driveFolderUrl);
resourceKey = _extractResourceKeyFromUrl(repos[i].driveFolderUrl);
name = repos[i].name;
break;
}
}
}
// show repo drop-down selector
let repoSection = CardService.newCardSection();
let onChangeAction = CardService.newAction()
.setFunctionName('_refreshImageRepoCard');
let selectRepoWidget = CardService.newSelectionInput()
.setFieldName('selectedRepo')
.setType(CardService.SelectionInputType.DROPDOWN)
.setTitle('Image Repository')
.setOnChangeAction(onChangeAction);
for (let i=0; i < repos.length; i++) {
if (repos[i].driveFolderUrl) {
selectRepoWidget.addItem(repos[i].name, repos[i].driveFolderUrl,
(repos[i].driveFolderUrl === selectedRepoUrl));
}
}
repoSection.addWidget(selectRepoWidget);
// show images in repo folder
let imgSection = CardService.newCardSection();
imgSection.addWidget(CardService.newTextParagraph()
.setText("Click on an image to insert it:"));
let repoFolder;
if (resourceKey) {
repoFolder = DriveApp.getFolderByIdAndResourceKey(
selectedRepoId,
resourceKey);
} else {
repoFolder = DriveApp.getFolderById(selectedRepoId);
}
let imgFileIter = repoFolder.getFiles();
while (imgFileIter.hasNext()) {
let imgFile = imgFileIter.next();
let imgFileId = imgFile.getId();
let imgFileResourceKey = imgFile.getResourceKey();
if (imgFileResourceKey === null) {
// can't pass a null parameter
imgFileResourceKey = '';
}
let imgUrl = 'https://docs.google.com/uc?id=' + imgFileId;
if (resourceKey) {
imgUrl += '&resourcekey=' + resourceKey;
}
let params = {
imgFileId: imgFileId,
imgFileResourceKey: imgFileResourceKey,
};
let clickAction = CardService.newAction()
.setParameters(params)
.setFunctionName('_onImageClick');
let imgWidget = CardService.newImage()
.setImageUrl(imgUrl)
.setOnClickAction(clickAction);
imgSection.addWidget(imgWidget);
}
card.addSection(repoSection);
card.addSection(imgSection);
return card.build();
}
/**
* Internal function to fresh the images when a new repository is selected.
*
*/
function _refreshImageRepoCard(event) {
let formInputs = event.commonEventObject.formInputs;
let up = PropertiesService.getUserProperties();
let selectedRepo = formInputs.selectedRepo.stringInputs.value[0];
up.setProperty(PROP_SELECTED_IMG_LIBRARY_REPO, selectedRepo);;
return buildImgLibraryCard(CALL_CONTEXT.SLIDES);
}
/**
* Internal function to insert the selected image into the selected/active
* slide.
*
* @param {Object} event - Includes parameters to identify the selected image.
*/
function _onImageClick(event) {
let imgFileId = event.parameters.imgFileId;
let imgFileResourceKey = event.parameters.imgFileResourceKey;
let slide = SlidesApp.getActivePresentation()
.getSelection()
.getCurrentPage();
let file;
if (imgFileResourceKey) {
file = DriveApp.getFileByIdAndResourceKey(imgFileId, imgFileResourceKey);
} else {
file = DriveApp.getFileById(imgFileId);
}
slide.insertImage(file.getBlob());
}
/**
* Internal function to extract the Drive Folder ID from a folder's URL.
* If not present (not all folders will have one), returns empty string.
*
* @param {String} folderUrl
*
* @return {String}
*/
function _extractFolderIdFromUrl(folderUrl) {
let folderId = '';
let regex = /https:\/\/drive\.google\.com\/drive\/folders\/(.+)/;
// get rid of any params (i.e. '?resourcekey=')
folderUrlSplit = folderUrl.split('?')[0];
let found = folderUrlSplit.match(regex);
if (!found) {
return '';
}
folderId = found[1];
return folderId;
}
/**
* Internal function to extract the Drive Folder Resource Key from a folder's
* URL. If not present (not all folders will have one), returns empty string.
*
* @param {String} folderUrl
*
* @return {String}
*/
function _extractResourceKeyFromUrl(folderUrl) {
let resourceKey = '';
let regex = /resourcekey=([^&]+)/;
// get rid of any params (i.e. '?resourcekey=')
let folderUrlSplit = folderUrl.split('?');
if (folderUrlSplit.length < 2) {
return '';
}
let splitPart = folderUrlSplit[1];
let found = splitPart.match(regex);
if (!found) {
return '';
}
resourceKey = found[1];
return resourceKey;
}