-
Notifications
You must be signed in to change notification settings - Fork 0
/
AmisNamedRanges.gs
210 lines (181 loc) · 6.04 KB
/
AmisNamedRanges.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
/**
* class to interact with namedRanges
* @return {class}
*/
AmisNamedRanges=new function() {
var that=this;
/**
* parse all named range in the whole Spreadsheet
* @return {object} an object representing the named ranges: {commodity: {type: [index]}}
*/
this.parseAllNamedRanges=function() {
var s=SpreadSheetCache.getActiveSpreadsheet();
var rs=s.getNamedRanges();
var retVal={}, _rangeName, _rangeNameMatch, _sheetName, _type, _index, _a1;
var r;
for (var i = rs.length; i--;) {
r=rs[i];
_rangeName=r.getName();
_rangeNameMatch=_rangeName.match(/^(\w+)_(\w+)_((\d+)|(\w+))$/);
_sheetName=_rangeNameMatch[1];
_type=_rangeNameMatch[2],
_index=_rangeNameMatch[3];
_a1=r.getRange().getA1Notation();
retVal[_sheetName]=(retVal[_sheetName] || {});
//if the index is numeric
if(_rangeNameMatch[4]){
retVal[_sheetName][_type]=(retVal[_sheetName][_type] || []);
retVal[_sheetName][_type][parseInt(_index, 10)]=_a1;
}
//the index is a key
else{
retVal[_sheetName][_type]=(retVal[_sheetName][_type] || {});
retVal[_sheetName][_type][_index]=_a1;
}
}
return retVal;
};
/**
* reads all named range in the whole Spreadsheet. If they are cached it will use the cache
* @return {object} an object representing the named ranges: {commodity: {type: [index]}}
*/
this.getAllNamedRanges=function() {
var namedRanges=APPCache.get("namedRanges");
if (!namedRanges) {
namedRanges=AmisNamedRanges.parseAllNamedRanges();
APPCache.put("namedRanges", namedRanges);
}
return namedRanges;
};
/**
* Removes all named range from the cache.
* @return {void}
*/
this.clearNamedRanges=function() {
APPCache.remove("namedRanges");
};
/**
* return all namedRanges of the current commodity
* @param {string} commodityName the commodity name
* @return {object} an object representing the named ranges: {type: [index]}
*/
this.getCommodityNamedRanges=function(commodityName){
commodityName = commodityName || FirebaseConnector.getCommodityName();
return this.getAllNamedRanges()[commodityName];
};
/**
* return all namedRanges of the current commodity
* @param {object} sheet [optional] the sheet
* @return {object} an object representing the named ranges: {type: [index]}
* @throws {InvalidArgument}
*/
this.getCommodityNamedRangesBySheet = function( sheet ) {
if ( sheet === null ) {
throw "InvalidArgument";
}
sheet = ( sheet || SpreadSheetCache.getActiveSheet() );
var commodityName = FirebaseConnector.getCommodityName( sheet );
return this.getCommodityNamedRanges(commodityName);
};
/**
* Class to manage the mapping of data between firebase and the db
* @return {object}
*/
this.DbMapping = new (function() {
this.parent = that;
/**
* reads and parse all the rowMap namedRanges of the Spreadsheet
* @return {object} the object in the same shape of firebase
*/
this.readRows = function() {
var com, namedRanges, retVal;
namedRanges = this.parent.getAllNamedRanges();
retVal = {};
for (com in namedRanges) {
if (namedRanges[com]) {
retVal[com] = this.readCommodityRows(com, namedRanges[com].rowMap);
}
}
return retVal;
};
/**
* reads and parse all the rowMap namedRanges of a sheet
* @param {string} commodity the commodity
* @param {object} namedRanges the namedRanges of the commodity (returned from getAllNamedRanges())
* @return {array} array in this form [id]=rowNumber
* @throws "InvalidArgument"
*/
this.readCommodityRows = function(commodity, namedRanges) {
var nr, range, retVal, rowNumRegEx;
retVal = {};
rowNumRegEx = /[A-Z]*(\d+):.*/;
if (!commodity || !namedRanges) {
throw "InvalidArgument";
}
for (nr in namedRanges) {
range = namedRanges[nr];
if (range) {
retVal[nr] = Number(range.replace(rowNumRegEx, "$1"));
}
}
return retVal;
};
/**
* reads and parse all the colMap namedRanges of the Spreadsheet
* @return {object} the object in the same shape of firebase
*/
this.readCols = function() {
var com, namedRanges, retVal;
namedRanges = this.parent.getAllNamedRanges();
retVal = {};
for (com in namedRanges) {
if (namedRanges[com]) {
retVal[com] = this.readCommodityCols(com, namedRanges[com].colMap);
}
}
return retVal;
};
/**
* reads and parse all the colMap namedRanges of a sheet
* @param {string} commodity the commodity
* @param {object} namedRanges the namedRanges of the commodity (returned from getAllNamedRanges())
* @return {array} array in this form [year]=columnLetter
* @throws "InvalidArgument"
*/
this.readCommodityCols = function(commodity, namedRanges) {
var colLetRegEx, nr, range, retVal;
retVal = {};
colLetRegEx = /([A-Z]+)\d*:.*/;
if (!commodity || !namedRanges) {
throw "InvalidArgument";
}
for (nr in namedRanges) {
range = namedRanges[nr];
if (range) {
retVal[nr] = range.replace(colLetRegEx, "$1");
}
}
return retVal;
};
/**
* update the mapping on firebase (batchRowArray & batchRowColumn)
* @param {sting} userToken firebase token
* @return {void}
* @throws "InvalidArgument"
* @throws "NamedRangesParsingErr" if error parsing namedRanges
*/
this.updateFbMapping = function(userToken) {
var batchRowArray, batchRowColumn;
if (!userToken) {
throw "InvalidArgument";
}
batchRowArray = AmisNamedRanges.DbMapping.readRows();
batchRowColumn = AmisNamedRanges.DbMapping.readCols();
if (!batchRowColumn || !batchRowArray) {
throw "NamedRangesParsingErr";
}
FirebaseConnector.writeOnFirebase(batchRowArray, "/config/batchRowArray", userToken);
FirebaseConnector.writeOnFirebase(batchRowColumn, "/config/batchRowColumn", userToken);
};
});
};