-
Notifications
You must be signed in to change notification settings - Fork 5
/
integrationTypeFileRepo.gs
361 lines (287 loc) · 9.94 KB
/
integrationTypeFileRepo.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
/**
* @fileoverview Code related to information that the Odo Add-on
* may show in various contexts if the chosen integration type is
* INTEGRATION_TYPE.FILE_REPOSITORY
*/
/**
* Function that returns default configuration fields and values for
* an integration type of INTEGRATION_TYPE.FILE_REPOSITORY, to be stored
* as the 'integrationData' field in 'config'. Called when setting
* up default configuration.
*
* @return {Object}
*/
function fileRepoGetDefaultConfig() {
let integrationData = {
maxFiles: 5,
};
return integrationData;
}
/**
* Creates and returns the card that gives the user options to configure
* the File Repository integration. Called from integrationTypeAll.gs based
* on the value of the 'buildConfigureIntegrationCard' parameter.
*
* @return {CardService.Card}
*/
function buildFileRepoConfigureCard() {
let config = getConfig();
const fileLimits = [5, 10, 25, 50];
let card = CardService.newCardBuilder();
integrationData = getConfigIntegrationData(INTEGRATION_TYPE.FILE_REPOSITORY);
let selectedMaxFiles = integrationData.maxFiles;
/*
if (config.saved &&
config.integrationType === INTEGRATION_TYPE.FILE_REPOSITORY
) {
selectedMaxFiles = config.integrationData.maxFiles;
} else {
selectedMaxFiles = fileLimits[0]; // default
}
*/
let section = CardService.newCardSection();
let selectMaxFilesWidget = CardService.newSelectionInput()
.setFieldName('maxFiles')
.setType(CardService.SelectionInputType.DROPDOWN)
.setTitle('Max Files Shown')
for (let i=0; i < fileLimits.length; i++) {
let limit = fileLimits[i];
selectMaxFilesWidget.addItem(limit.toString(), limit,
(limit === Number(selectedMaxFiles)));
}
section.addWidget(selectMaxFilesWidget);
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 saveFileRepoConfigureSelections(formInputs) {
let integrationData = {};
integrationData.maxFiles = formInputs.maxFiles.stringInputs.value[0];
return integrationData;
}
/**
* Function used to return the file repository 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.DRIVE)
*
* @return {Card}
*/
function buildFileRepoCard(context) {
// if file selected, giver user chance to check it in
let fileName = getMergeKeyValue('{{fileName}}');
let mimeType = getMergeKeyValue('{{fileMimeType}}');
let fileId = getMergeKeyValue('{{fileId}}');
let card = CardService.newCardBuilder();
let brandedHeader = buildCustomerBrandedHeader();
card.setHeader(brandedHeader);
if (fileName) {
// file is selected
let section = CardService.newCardSection();
// check mimeType to ensure it's DOCX
if (!_isOfficeFile(mimeType)) {
console.log(mimeType)
let message = 'Only MS Office documents can be checked-in.';
section.addWidget(CardService.newTextParagraph().setText(message));
// inform user that only office files are allowed
} else {
// give user chance to check in file
let url = _getOfficeFileIconUrl(mimeType)
let icon = CardService.newIconImage().setIconUrl(url);
let textField = CardService.newDecoratedText()
.setStartIcon(icon)
.setText(fileName);
section.addWidget(textField);
let params = {
fileId: fileId,
fileName: fileName,
};
let buttonAction = CardService.newAction()
.setFunctionName('_checkInFile')
.setParameters(params);
let button = CardService.newTextButton()
.setText("Check-in File")
.setOnClickAction(buttonAction);
section.addWidget(button);
}
card.addSection(section);
} else {
// no file is selected
let section = CardService.newCardSection();
section.addWidget(CardService.newImage().setImageUrl(SELECT_FILE_ICON));
section.addWidget(
CardService.newTextParagraph().setText(
' ' +
' ' +
'Select a file to check it in'
));
card.addSection(section);
// show user list of files they can "check out"
let ciFiles = _getCheckedInFiles();
if (ciFiles.length) {
let section2 = CardService.newCardSection();
let message = '<br>Or select a file listed below to check-out:';
section2.addWidget(CardService.newTextParagraph().setText(message));
for (let i=0; i < ciFiles.length; i++) {
let file = ciFiles[i];
let fileListEntry = CardService.newDecoratedText();
fileListEntry.setText(file.getName())
.setBottomLabel('Modified: ' + file.getLastUpdated())
let iconImage = CardService.newIconImage()
.setIconUrl(_getOfficeFileIconUrl(file.getMimeType()));
fileListEntry.setStartIcon(iconImage);
let action = CardService.newAction()
.setFunctionName('_checkOutFile')
.setParameters({fileId: file.getId(), fileName: file.getName()});
let button = CardService.newImageButton()
.setIconUrl(CHECKOUT_FILE_ICON)
.setAltText('Checkout File')
.setOnClickAction(action);
fileListEntry.setButton(button);
section2.addWidget(fileListEntry);
}
card.addSection(section2);
}
}
return card.build();
}
/**
* Private function that handles the user's request to check out
* a selected file when the button next to it is clicked.
* @param {Object} event
*/
function _checkOutFile(event) {
let up = PropertiesService.getUserProperties();
let fileId = event.parameters.fileId;
let fileName = event.parameters.fileName;
let destFolder;
let destFolderId = up.getProperty(fileId);
if (!destFolderId) {
destFolder = DriveApp.getRootFolder();
} else {
destFolder = DriveApp.getFolderById(destFolderId);
}
let f = DriveApp.getFileById(fileId);
f.moveTo(destFolder);
let card = CardService.newCardBuilder();
let brandedHeader = buildCustomerBrandedHeader();
card.setHeader(brandedHeader);
let section = CardService.newCardSection();
let message = `File ${fileName}"" has been checked-out`;
let url = f.getUrl();
section.addWidget(CardService.newTextParagraph().setText(message));
card.addSection(section);
let updateCard = card.build();
let actionResponse = CardService.newActionResponseBuilder()
.setNavigation(CardService.newNavigation().updateCard(updateCard))
.setStateChanged(true)
.build();
return actionResponse;
}
/**
* Private function that handles the user's request to check in
* a selected file when the "Check-In File" button is clicked.
*
* @param {Object} event
*
*/
function _checkInFile(event) {
let up = PropertiesService.getUserProperties();
let fileId = event.parameters.fileId;
let fileName = event.parameters.fileName;
let f = DriveApp.getFileById(fileId);
let parentFolderIter = f.getParents();
if (parentFolderIter.hasNext()) {
let parentFolderId = parentFolderIter.next().getId();
// record where this file lived so we can put it back there
// upon next check-in
up.setProperty(fileId, parentFolderId);
}
// "check-in" the file. Here we simulate this by just moving
// the file to a special folder so we can hide it from view.
let odf = createOrGetOdoDataFolder();
let file = DriveApp.getFileById(fileId);
file.moveTo(odf);
let card = CardService.newCardBuilder();
let brandedHeader = buildCustomerBrandedHeader();
card.setHeader(brandedHeader);
let section = CardService.newCardSection();
let message = `File "${fileName}" has been checked-in`;
section.addWidget(CardService.newTextParagraph().setText(message));
card.addSection(section);
let updateCard = card.build();
let actionResponse = CardService.newActionResponseBuilder()
.setNavigation(CardService.newNavigation().updateCard(updateCard))
.setStateChanged(true)
.build();
return actionResponse;
}
/**
* Private function that returns a list of Files that have been checked in
* (sorted by last modified date). Number returned is limited to
* config.integrationData.maxFiles
*
* @return {Object} Array of Files
*/
function _getCheckedInFiles() {
let config = getConfig();
let ciFiles = [];
let odf = createOrGetOdoDataFolder();
let count = 0;
let filesIter = odf.getFiles();
while (filesIter.hasNext()) {
let file = filesIter.next();
ciFiles.push(file);
count++;
if (count === config.integrationData.maxFiles) {
break;
}
}
return ciFiles;
}
/**
* Private function that returns true if given mimeType corresponds to
* an MS Office file (Word, Excel, Powerpoint), false otherwise.
*
* @return {Boolean}
*/
function _isOfficeFile(mimeType) {
if (mimeType.includes('officedocument')) {
return true;
}
return false;
}
/**
* Private function that returns a URL for an icon for Word, Excel, or
* Powerpoint files (based on the passed mimeType). Call _isOfficeFile
* first to verify if mimeType corresponds to Office file.
*
* @return {String}
*/
function _getOfficeFileIconUrl(mimeType) {
let url;
if (mimeType.includes('word')) {
url = 'https://img.icons8.com/color/512/ms-word.png';
} else if (mimeType.includes('presentation')) {
url = 'https://img.icons8.com/color/512/ms-powerpoint.png';
} else if (mimeType.includes('spreadsheet')) {
url = 'https://img.icons8.com/color/512/ms-excel.png';
} else {
url = 'https://img.icons8.com/color/512/office-365.png';
}
return url;
}