-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode.js
295 lines (269 loc) · 8.65 KB
/
Code.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
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
/* eslint no-var: 0 */
/* exported onOpen */
/**
*
*
*/
function onOpen() {
addStrimanMenu();
}
/* exported onEdit */
/**
*
*
*/
function onEdit() {
insertInTodo();
}
/**
* Generate unique identifiers (UID) in the sheet.
*
*/
function generateUID() {
try {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName('Data');
var columnHeaderValues = sheet.getRange(1, 1, 1, sheet.getLastColumn())
.getValues();
var UID_COLUMN = columnHeaderValues[0].indexOf('UID') + 1;
var lastRow = sheet.getLastRow();
var uidRange = sheet.getRange(2, UID_COLUMN, lastRow - 1);
var uidValues = uidRange.getValues();
var uidCounterRange = spreadsheet.getRangeByName('UID_Counter');
var nextCount = uidCounterRange.getValue();
for (var i = 0; i < uidValues; i++) {
if (uidValues[i][0] == 0) {
uidValues[i][0] = nextCount++;
}
}
uidCounterRange.setValue(nextCount);
uidRange.setValues(uidValues).setHorizontalAlignment('center');
} catch (error) {
Logger.log('%s : %s', error.name, error.message);
}
}
/**
* Inserts comment from this spreadsheet into the
* corresponding TODO spreadsheet.
*
*
*/
function insertInTodo() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var activeSheet = SpreadsheetApp.getActiveSheet();
var columnHeaderValues = activeSheet.getRange(1, 1, 1, activeSheet
.getLastColumn()).getValues();
var ADD_TO_TODO2019_COLUMN = columnHeaderValues[0].indexOf('TODO 2019') + 1;
var currentCell = activeSheet.getCurrentCell();
var firstRow = currentCell.getRow();
if ((firstRow == 1) ||
(currentCell.getColumn() !== ADD_TO_TODO2019_COLUMN) ||
(activeSheet.getName() !== 'Data') || (currentCell.getValue() == false)) {
return;
}
var UID_COLUMN = columnHeaderValues[0].indexOf('UID') + 1;
var ACKNOWLEDGE_COLUMN = columnHeaderValues[0].indexOf('Vu') + 1;
var COMMENT_COLUMN = columnHeaderValues[0].indexOf('Commentaire') + 1;
var CATEGORY_COLUMN = columnHeaderValues[0].indexOf('Catégorie') + 1;
var AUTHOR_COLUMN = columnHeaderValues[0].indexOf('Auteur') + 1;
var todoSpreadsheet = SpreadsheetApp
.openById('1J1oFUXOXRSxIpiyLbbA2Nwq31mnSzkFXr6zJ5nOduNE');
var todoDataSheet = todoSpreadsheet.getSheetByName('Data');
var todoLastColumn = todoDataSheet.getLastColumn();
var todoColumnHeaderValues = todoDataSheet
.getRange(1, 1, 1, todoLastColumn).getValues();
var TODO_TASK_COLUMN = todoColumnHeaderValues[0].indexOf('Tâche') + 1;
var TODO_CATEGORY_COLUMN = todoColumnHeaderValues[0]
.indexOf('Catégorie') + 1;
var TODO_AUTHOR_COLUMN = todoColumnHeaderValues[0].indexOf('Auteur') + 1;
var TODO_SOURCE_COLUMN = todoColumnHeaderValues[0].indexOf('Source') + 1;
var valuesToAdd = [];
var uidValue = activeSheet.getRange(currentCell.getRow(), UID_COLUMN)
.getValue();
var spreadsheetName = spreadsheet.getName();
activeSheet.getRange(currentCell.getRow(), ACKNOWLEDGE_COLUMN)
.setValue(true);
for (i = 1; i <= todoLastColumn; i++) {
if (i == TODO_TASK_COLUMN) {
valuesToAdd.push(activeSheet.getRange(firstRow, COMMENT_COLUMN)
.getValue());
continue;
}
if (i == TODO_CATEGORY_COLUMN) {
valuesToAdd.push(activeSheet.getRange(firstRow, CATEGORY_COLUMN)
.getValue());
continue;
}
if (i == TODO_AUTHOR_COLUMN) {
valuesToAdd.push(activeSheet.getRange(firstRow, AUTHOR_COLUMN)
.getValue());
continue;
}
if (i == TODO_SOURCE_COLUMN) {
valuesToAdd.push(spreadsheetName + ' - UID: ' + uidValue);
continue;
}
valuesToAdd.push('');
}
todoDataSheet.appendRow(valuesToAdd);
}
/**
* Create a custom menu for this S:Triman spreadsheet.
*
*/
function addStrimanMenu() {
SpreadsheetApp.getUi()
.createMenu('S:Triman')
.addItem('Afficher Catégories', 'showDialog')
.addSeparator()
.addSubMenu(SpreadsheetApp.getUi().createMenu('Options avancées')
.addItem('Générer UID', 'generateUID')
.addItem('Insérer NewLine dans Catégorie', 'insertNewLineInCategory')
.addItem('Supprimer la cache \'Catégories\'', 'removeCachedCategories'))
.addToUi();
generateUID();
}
/* exported showDialog */
/**
* Show a sidebar with a list of check box items.
* This list comes from a data validation range.
* This function is called from 'Page.html'.
*
*/
function showDialog() {
// var activeSheet = SpreadsheetApp.getActiveSheet();
// var columnHeaderValues = activeSheet.getRange(1, 1, 1, activeSheet
// .getLastColumn()).getValues();
// var CATEGORY_COLUMN = columnHeaderValues[0].indexOf('Catégorie') + 1;
// if (SpreadsheetApp.getCurrentCell().getColumn() != CATEGORY_COLUMN) return;
var html = HtmlService.createTemplateFromFile('Page').evaluate()
.setTitle('Liste des catégories');
SpreadsheetApp.getUi().showSidebar(html);
}
/* exported valid */
/**
* Returns an array of categories, as defined by a data validation range.
*
* @return {array}
*/
var valid = function() {
try {
return getCategories();
} catch (e) {
return null;
}
};
/* exported currentSelection */
/**
* Returns the strings contained in the current cell, in an array and
* separated by 'new line'.
*
* @return {array}
*/
var currentSelection = function() {
try {
var arrayOfValues = SpreadsheetApp.getActiveRange().getValue().split('\n');
return arrayOfValues;
} catch (e) {
return null;
}
};
/* exported fillCell */
/**
* Sets the value of the current cell with user's sidebar selection.
*
* @param {*} e
*/
function fillCell(e) {
// First, check that the selected cell's column is valid
var activeSheet = e.getActiveSheet();
var columnHeaderValues = activeSheet.getRange(1, 1, 1, activeSheet
.getLastColumn()).getValues();
var CATEGORY_COLUMN = columnHeaderValues[0].indexOf('Catégorie') + 1;
// If not, refresh sidebar. Otherwise fill in selected cell with user choice.
if (e.getCurrentCell().getColumn() != CATEGORY_COLUMN) {
showDialog();
} else {
var s = [];
for (var i in e) {
if (i.substr(0, 2) == 'ch') s.push(e[i]);
}
if (s.length) SpreadsheetApp.getActiveRange().setValue(s.join('\n'));
}
}
/**
* Returns an array of categories from cached data if available.
* If not, returns an array of categories from 'Catégorie' named range and
* put the data in cache for faster future calls.
*
* @return {array}
*/
function getCategories() {
// if (SpreadsheetApp.getActiveRange().getDataValidation() == null)
// return null;
var activeSheet = SpreadsheetApp.getActiveSheet();
var columnHeaderValues = activeSheet.getRange(1, 1, 1, activeSheet
.getLastColumn()).getValues();
var CATEGORY_COLUMN = columnHeaderValues[0].indexOf('Catégorie') + 1;
if (SpreadsheetApp.getCurrentCell().getColumn() != CATEGORY_COLUMN) {
return null;
}
var cache = CacheService.getScriptCache();
var cached = cache.get('categories');
if (cached != null) {
var newArray1D = cached.split(',');
var newArray2D = [];
while (newArray1D.length) newArray2D.push(newArray1D.splice(0, 1));
Logger.log('newArray2D = %s', newArray2D);
return newArray2D;
}
// var categoryArray = SpreadsheetApp.getActiveRange().getDataValidation()
// .getCriteriaValues()[0].getValues();
var categoryArray = SpreadsheetApp.getActiveSpreadsheet()
.getRangeByName('Catégories').getValues();
cache.put('categories', categoryArray, 1500);
Logger.log('categoryArray = %s', categoryArray);
return categoryArray;
}
/* exported removeCachedCategories */
/**
* Remove categories from cache.
*
*/
function removeCachedCategories() {
var cache = CacheService.getScriptCache();
cache.remove('categories');
}
/* exported showCachedCategories */
/**
*
*
*/
function showCachedCategories() {
var cache = CacheService.getScriptCache();
var cached = cache.get('categories');
if (cached != null) {
Logger.log('Catégories = %s', cached);
} else {
Logger.log('No categories in cache');
}
}
/* exported insertNewLineInCategory */
/**
* Insert 'new line' after each string of the cells of a column 'Catégorie'..
*
*/
function insertNewLineInCategory() {
var activeSheet = SpreadsheetApp.getActiveSheet();
var columnHeaderValues = activeSheet.getRange(1, 1, 1, activeSheet
.getLastColumn()).getValues();
var CATEGORY_COLUMN = columnHeaderValues[0].indexOf('Catégorie') + 1;
var lastRow = activeSheet.getLastRow();
var categoryRange = activeSheet.getRange(2, CATEGORY_COLUMN, lastRow - 1);
var categoryValues = categoryRange.getValues();
for (var i = 0; i < categoryValues.length; i++) {
categoryValues[i][0] = categoryValues[i][0].toString()
.replace(/, /g, '\n');
}
categoryRange.setValues(categoryValues);
}