-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProtectionMaker.gs
307 lines (219 loc) · 8.93 KB
/
ProtectionMaker.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
var ProtectionMaker = new function() {
/**
* protect the first column of a period
* @param {string} activeRangeA1 the active range in A1A1Notation
* @return {bool} true if ok, false otherwise
*/
this.protectWrongForecast = function( activeRangeA1 ) {
if ( !activeRangeA1 ) {
return true;
}
if ( ForecastUtility.editedWrongForecast( activeRangeA1 ) ) {
Browser.msgBox( "You are trying to edit a wrong column of a forecasting period.\\nPlease click on the 'Undo' button of the sidebar on the right.");
return false;
}
return true;
};
/**
* check if user added or removed a column and shows an error and send an email to amis.outlook
* @param {object} sheet the sheet
* @param {object} template the template sheet
* @param {string} sheetName (optional) the sheetName
* @return {bool} true if ok, false otherwise
* @throws {InvalidArgument}
*/
this.validateColumnNumber=function(sheet, template, sheetName){
var scn, tcn;
if (!sheet, !template) {
throw "InvalidArgument";
}
scn=sheet.getLastColumn();
tcn=template.getLastColumn();
if(scn===tcn){
return true;
}
sheetName=(sheetName || sheet.getName());
//if column differs ask user for confirmation to restore the sheet from the database and fix the error.
Browser.msgBox("The column number of the "+sheetName+" sheet differs from the original, please click on the 'Edit' menu, then the 'Undo' button.\\nThen try to click on the 'Validate' button.");
return false;
};
/**
* validate the current sheet, restore styles and formulas
* @param {array} sheetValues [optional] sheet's data
* @param {object} spreadsheet [optional] the spreadsheet
* @param {object} sheet [optional] the sheet
* @param {string} sheetName (optional) the sheetName
* @return {void}
* @throws {InvalidArgument}
* @throws {JavaException} in case of non valid data in the sheet
*/
this.validateSheet = function( sheetValues, spreadsheet, sheet, sheetName ) {
spreadsheet = ( spreadsheet || SpreadSheetCache.getActiveSpreadsheet() );
sheet = ( sheet || SpreadSheetCache.getActiveSheet() );
sheetValues = ( sheetValues || SpreadSheetCache.getActiveSheetValues() );
sheetName=(sheetName || sheet.getName());
try {
if ( !Utility.isTemplate() ) {
ProtectionMaker.checkIfValueIsNotProtected(spreadsheet, sheet);
//forecast methodologies on edit
ForecastingMethodologies.fixAllFMRanges( sheetValues,sheet );
//restore all column width getting the current size from template
if (/^[A-Za-z]+$/.test(sheetName)) {
ProtectionMaker.restoreColsWidth(sheet, sheetName);
}
}
} catch ( e ) {
var ex = e;
if ( ex instanceof JavaException ) {
Browser.msgBox( "Error in " + sheet.getName() + " sheet: " + ex.message );
} else {
Browser.msgBox( "There is a problem with the sheet " + sheet.getName() + ". Please contact the administrator." );
}
throw e;
}
};
/**
* validate all sheet in a spreadsheet, restore styles and formulas
* @param {object} spreadsheet [optional] the spreadsheet
* @return {void}
*/
this.validateAllSheet=function(spreadsheet){
spreadsheet = ( spreadsheet || SpreadSheetCache.getActiveSpreadsheet() );
Utility.forEachSheet(spreadsheet.getId(), Config.commoditySheetsRegex, function(sheet, sheetName){
var sheetValues;
sheetValues=sheet.getDataRange().getValues();
ProtectionMaker.validateSheet(sheetValues, spreadsheet, sheet, sheetName);
});
};
/**
* restore the styles, formulas, values and the formatting from the template
* @param {object} spreadsheet [optional] the spreadsheet
* @param {object} sheet [optional] the sheet
* @return {void}
* @throws {RowsOrColChanged} if sheet's rows and columns doesn't match with template
* @throws {JavaException} in case of non valid data in the sheet
* @throws {InvalidArgument}
*/
this.checkIfValueIsNotProtected = function(spreadsheet, sheet) {
if ( !sheet || !spreadsheet ) {
throw "InvalidArgument";
}
var ss = sheet;
var rangeToBeRestored = AmisNamedRanges.getCommodityNamedRangesBySheet(ss).rangeOfRestoreSheetStyle[0];
ss.getRange( rangeToBeRestored ).setDataValidation( null );
//destroy eventually CONDITIONS FORMATTING COPIED AND PASTED
//e.range.clearFormat(); //commented because now with the validate button there isn't the event var
var sheetName = ss.getName();
var templateSheet = spreadsheet.getSheetByName( "Template_" + sheetName );
var sheetValues = ss.getRange( rangeToBeRestored ).getValues();
//var sheetFormulas = ss.getRange(rangeToBeRestored).getFormulas();
var tmpDataValidation = templateSheet.getRange( rangeToBeRestored ).getDataValidations();
var tmpFormulas = templateSheet.getRange( rangeToBeRestored ).getFormulas();
var tmpValues = templateSheet.getRange( rangeToBeRestored ).getValues();
//var lenght= tmpValues.length
var row;
//If user removes a column/row show a dialog with a message
if ( ( sheetValues.length !== tmpValues.length ) || ( sheetValues[ 0 ].length !== tmpValues[ 0 ].length ) ) {
throw "RowsOrColChanged";
}
for ( var r = tmpValues.length; r--; ) {
row = tmpValues[ r ];
for ( var c = row.length; c--; ) {
if ( row[ c ] != '' ) {
sheetValues[ r ][ c ] = row[ c ];
}
if ( tmpFormulas[ r ][ c ] != '' ) {
sheetValues[ r ][ c ] = tmpFormulas[ r ][ c ];
}
}
}
//restore the style from hidden template
templateSheet.getRange( rangeToBeRestored ).copyTo( ss.getRange( rangeToBeRestored ), {
formatOnly: true
} );
//restore data validations
ss.getRange( rangeToBeRestored ).setDataValidations( tmpDataValidation );
//restore FORMULAS and VALUES not EDITABLE. Getvalues is needed to throw exception in case of non valid data in the sheet
ss.getRange( rangeToBeRestored ).setValues( sheetValues ).getValues();
};
/**
* get all row with in each template. Cache values will be used if present.
* @return {array} an array in the form [sheetName][row number]=number
*/
this.getTmplRowsHeight = function() {
var rowsHeight = APPCache.get( "rowsHeight" );
if ( !rowsHeight ) {
rowsHeight = {};
Utility.forEachSheet( undefined, new RegExp( "^" + Config.templatePrefix ), function( s, sheetName ) {
rowsHeight[ sheetName ] = [];
Utility.forEachRow(s, function(s, rowNum){
rowsHeight[ sheetName ][ rowNum ] = s.getRowHeight( rowNum );
});
} );
APPCache.put( "rowsHeight", rowsHeight );
}
return rowsHeight;
};
/**
* get all cols with in each template. Cache values will be used if present.
* @return {array} an array in the form [sheetName][col number]=number
*/
this.getTmplColsWidth = function() {
var colsWidth = APPCache.get( "colsWidth" );
if ( !colsWidth ) {
colsWidth = {};
Utility.forEachSheet( undefined, new RegExp( "^" + Config.templatePrefix ), function( s, sheetName ) {
colsWidth[ sheetName ] = [];
Utility.forEachColumn(s, function(s, colNum){
colsWidth[ sheetName ][ colNum ] = s.getColumnWidth( colNum );
});
} );
APPCache.put( "colsWidth", colsWidth );
}
return colsWidth;
};
/**
* restore all row height getting the current size from template. CAN BE USED ONLY FROM USER WITH ALL PERMISSIONS!
* @return {void}
* @throws "InvalidTemplateSizes" if no data from templates
*/
this.restoreRowsHeight=function(){
var rowsHeight=this.getTmplRowsHeight();
if(!rowsHeight){
throw "InvalidTemplateSizes";
}
Utility.forEachSheet( undefined, /^[A-Za-z]+$/, function( s, sheetName ) {
Utility.forEachRow(s, function(s, rowNum){
s.setRowHeight(rowNum, rowsHeight[ Config.templatePrefix+sheetName ][ rowNum ]);
});
} );
};
/**
* restore all column width getting the current size from template
* @return {void}
* @throws {InvalidTemplateSizes} if no data from templates
* @throws {InvalidArgument}
*/
this.restoreColsWidth=function( sheet, sheetName ){
var colsWidth=this.getTmplColsWidth(), s=sheet;
if (!sheet || !sheetName) {
throw "InvalidArgument";
}
if(!colsWidth){
throw "InvalidTemplateSizes";
}
var nr, previusFc_number;
nr=AmisNamedRanges.getCommodityNamedRangesBySheet(s);
previusFc_number=ConvertA1.rangeA1ToIndex(nr.previousForecast.first, 1).left;
Utility.forEachColumn(s, function(s, colNum){
var templateWidths=colsWidth[ Config.templatePrefix+sheetName ];
if (colNum<previusFc_number) {
return;
}
if (!templateWidths[ colNum ]) {
throw "InvalidTemplateSizes";
}
s.setColumnWidth(colNum, templateWidths[ colNum ]);
});
};
};