-
Notifications
You must be signed in to change notification settings - Fork 0
/
ForecastingMethodologies.gs
240 lines (189 loc) · 6.61 KB
/
ForecastingMethodologies.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
/**
* class to manage Forecasting Methodologies
*/
var ForecastingMethodologies = new( function() {
/**
* do the format operations of each element of the Forecasting Methodologies
* @param {string} val cell value
* @return {string} the value without any space
*/
this.formatValue = function( val ) {
return val.toUpperCase().replace( / /g, "" ).split( "," ).filter( function( e ) {
return e;
} ).join( "," );
};
/**
* check if a given string is a valid Forecasting Methodology
* @param {string} value the string to validate
* @return {bool} true if valid, false otherwise
*/
this.isValid = function( value ) {
return /^((\s?[CRGTSIMFBO]\s?(,\s?[CRGTSIMFBO]\s?)*)|\s*)$/i.test( value );
};
/**
* show the Forecasting Methodology's dialog
* @param {string} CellNotation cell in A1Notation
* @param {string} CellValue cell value
*/
this.showMethodsDialog = function( CellNotation,CellValue ) {
currCellNotation = CellNotation;
currCellValue = CellValue;
var html = HtmlService.createTemplateFromFile( 'MethodsDialog' )
.evaluate()
.setWidth( 800 )
.setHeight( 400 );
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.showModalDialog( html, 'Forecasting Methodologies' );
};
/**
* reads the forecasting Methodology ranges from namedRanges
* @param {object} sheet [optional] the sheet
* @return {array} array of ranges, null otherwise
* @throws {InvalidArgument}
*/
this.getFMRanges = function( sheet ) {
if ( sheet === null ) {
throw "InvalidArgument";
}
sheet = ( sheet || SpreadSheetCache.getActiveSheet() );
var ranges = AmisNamedRanges.getCommodityNamedRangesBySheet( sheet ).fm;
return ranges;
};
/**
* function to attach on the onEdit event
* @param {Object} e
*/
this.onEdit = function(e) {
var fmRanges, multiple, rangeValues, oldValues;
rangeValues = void 0;
if (Utility.isMaster()) {
return;
}
fmRanges = this.getFMRanges();
if (!fmRanges) {
return;
}
rangeValues = e.range.getValues();
oldValues=JSON.parse(JSON.stringify(rangeValues));
multiple = rangeValues.length > 1;
if(multiple){
return rangeValues;
}
// for (_i = 0, _len = rangeValues.length; _i < _len; _i++) {
// row=rangeValues[_i];
// for (var _j = 0, row_length=row.length; _j<row_length; _j++) {
// cellValue=row[_j];
// cell = e.range.getCell(_i + 1, _j+1);
// rangeValues[_i][_j]=this.onEditCell(cell, fmRanges, cellValue, multiple);
// }
// }
//TODO pass range in A1 to onEditCell
rangeValues[0][0]=this.onEditCell(e.range, fmRanges, rangeValues[0][0], multiple);
if(rangeValues[0][0]!==oldValues[0][0]){
e.range.setValues(rangeValues);
}
//e.range.setDataValidation(null);
return rangeValues;
};
/**
* check all the FM ranges and fix their value (if not valid)
* @param {array} sheetValues sheet's data
* @param {object} sheet [optional] the sheet
* @return {void}
* @throws {InvalidArgument}
*/
this.fixAllFMRanges = function( sheetValues, sheet ) {
if ( sheet === null ) {
throw "InvalidArgument";
}
sheet = ( sheet || SpreadSheetCache.getActiveSheet() );
var fmRanges;
fmRanges = this.getFMRanges(sheet);
for ( var i = 0, fmRanges_length = fmRanges.length, range; i < fmRanges_length; i++ ) {
range = fmRanges[ i ];
sheetValues = this.fixSingleFMRanges( sheetValues, range, sheet );
}
return sheetValues;
};
/**
* check a single FM range and fix its value (if not valid) and write it to the sheet
* @param {array} sheetValues sheet's data
* @param {string} fmRange the FM range to check in A1Notation
* @param {object} sheet [optional] the sheet
* @return {array} sheet's data
* @throws {InvalidArgument}
*/
this.fixSingleFMRanges = function( sheetValues, fmRange, sheet ) {
if ( sheet === null ) {
throw "InvalidArgument";
}
sheet = ( sheet || SpreadSheetCache.getActiveSheet() );
var fmRangeIx, cellValue, cellA1, c, fixedCellValue, changed = false,
newFMvalues, bottom;
fmRangeIx = ConvertA1.rangeA1ToIndex( fmRange );
c = fmRangeIx.left;
bottom = fmRangeIx.bottom;
//check all cell in current fmRange
for ( var r = fmRangeIx.top; r <= bottom; r++ ) {
cellValue = sheetValues[ r ][ c ];
cellA1 = ConvertA1.indexToColA1( c + 1 ) + r;
fixedCellValue = this.fixFMValue( cellA1, cellValue, true );
sheetValues[ r ][ c ] = fixedCellValue;
if ( cellValue !== fixedCellValue ) {
changed = true;
}
}
//if the values changed write it to the sheet
if ( changed ) {
newFMvalues = Utility.getRangeValuesFromArray( sheetValues, fmRange );
sheet.getRange( fmRange ).setValues( newFMvalues );
}
return sheetValues;
};
/**
* fix a value of a cell: if not valid show the dialog or empty the value, if valid format it
* @param {[type]} cellA1 cell in A1Notation
* @param {string} cellValue value of the cell
* @param {bool} multiple set to true if the user edited a range
* @return {string} the value fixed
*/
this.fixFMValue=function(cellA1, cellValue, multiple){
//check if cell is not valid and is to open the dialog
if ( !ForecastingMethodologies.isValid( cellValue ) ) {
//if !multiple && !valid show a dialog
if(!multiple) {
ForecastingMethodologies.showMethodsDialog( cellA1, cellValue );
}
//if multiple && !valid empty value
return "";
} else {
//if !multiple && valid -> format
//if multiple && valid -> format
return ForecastingMethodologies.formatValue( cellValue );
}
};
/**
* events called by this.onEdit on the single cell
* @param {object|string} cell current cell or current cell in A1Notation (for better performance)
* @param {array} fmRanges ForecastingMethodologies ranges from firebase
* @param {string} cellValue value of the cell
* @param {bool} multiple set to true if the user edited a range
* @return {array} the value to be writed
*/
this.onEditCell = function( cell, fmRanges, cellValue, multiple ) {
var r, cellA1;
cellA1=(typeof cell==="string")?cell:cell.getA1Notation();
for ( var i = fmRanges.length; i--; ) {
r = fmRanges[ i ];
//check if is in a FM range
if ( Utility.isInRange( r, cellA1 ) ) {
//THIS AVOID PROBLEMS IN CASE SOMEBODY COPY AND PASTE VALUES FROM A CELL WITH VALIDATION
//cell.setDataValidation( null );
//check if cell is not valid and is to open the dialog
return this.fixFMValue(cellA1, cellValue, multiple);
}
}
//if cell is not in a fmRanges
return cellValue;
};
} );