-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgsDB.gs
More file actions
229 lines (219 loc) · 7.83 KB
/
Copy pathgsDB.gs
File metadata and controls
229 lines (219 loc) · 7.83 KB
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
class gsDB
{
//---------------------------------------------------------
constructor(spreadId,sheetName)
{
this.MAX_NUM_COLS = 100;
this.MAX_NUM_ROWS = 10000;
this.spreadsheet = SpreadsheetApp.openById(spreadId);
this.sheet = this.spreadsheet.getSheetByName(sheetName);
this.sheetName = sheetName;
this.colNames = [];
this.cache = {}; // cache for fast queries
this.clearCacheOnUpdates = true;
// first line contains the table columns names
for (var i = 1; i <= this.MAX_NUM_COLS; i++)
{
var value = this.sheet.getRange(1,i).getValue().toString().trim();
if( value.length < 1 )
{
break;
}
this.colNames.push(value);
}
}
//---------------------------------------------------------
getNumOfActiveRows()
{
return this.sheet.getLastRow();
}
//---------------------------------------------------------
truncate()
{
var numOfRows = this.getNumOfActiveRows();
for ( var i = 2 ; i <= numOfRows ; i ++)
{
for ( var j = 1 ; j <= this.colNames.length ; j ++)
{
this.sheet.getRange(i,j).setValue("")
}
}
if(this.clearCacheOnUpdates)
{
this.cache = {};
console.log("truncate:clearCache")
}
return true;
}
//---------------------------------------------------------
add(row)
{
//console.log("add(row) function : before this.getNumOfActiveRows() + 1 ");
var nextRowNum = this.getNumOfActiveRows() + 1 ;
//console.log("add(row) function : after this.getNumOfActiveRows() + 1 ");
var colName = ""; var newValue = "";
for ( var i = 0 ; i < this.colNames.length ; i ++)
{
colName = this.colNames[i];
newValue = row[colName];
this.sheet.getRange(nextRowNum,i+1).setValue(newValue)
}
if(this.clearCacheOnUpdates)
{
this.cache = {};
console.log("add(row):clearCache")
}
return true;
}
//---------------------------------------------------------
query(query_str)
{
var query_str = "=QUERY(" + this.sheetName + "!A2:ZZZ; \"" + query_str + "\")";
for ( var i = 0 ; i < this.colNames.length ; i ++)
{
const regex = new RegExp(this.colNames[i], "g");
query_str = query_str.replace( regex , "Col"+(i+1) )
}
//console.log(query_str);
// very ugly and danger ...
var sheet = this.spreadsheet.insertSheet();
var r = sheet.getRange(1, 1).setFormula(query_str);
SpreadsheetApp.flush();
var reply = sheet.getDataRange().getValues();
this.spreadsheet.deleteSheet(sheet);
return reply;
}
//---------------------------------------------------------
query_fast(field_name,field_value)
{
if(this.cache != {} && this.cache[field_name] && this.cache[field_name] )
{
//console.log(this.cache[field_name])
var inx = this.cache[field_name].indexOf(field_value)
if (inx == -1)
{
return null;
}
var curRowArr = [];
for ( var j = 1 ; j <= this.colNames.length ; j ++)
{
// sheet start from 1 and also there is header row ...
curRowArr.push(this.sheet.getRange(inx+2,j).getValue());
}
//console.log("-- query_fast result from cache")
return curRowArr;
}
//-------------
var numOfRows = this.getNumOfActiveRows()
var col_index = -1;
for ( var i = 0 ; i < this.colNames.length ; i ++)
{
if( field_name == this.colNames[i] )
col_index = i+1;
}
if ( col_index == -1)
return null;
this.cache[field_name] = [];
for ( var i = 2 ; i <= numOfRows ; i ++)
{
var row_value = this.sheet.getRange(i,col_index).getValue()
this.cache[field_name].push(row_value); // we need to loop even if value was found for cache ...
if ( field_value == row_value )
{
var curRowArr = [];
for ( var j = 1 ; j <= this.colNames.length ; j ++)
{
curRowArr.push(this.sheet.getRange(i,j).getValue());
}
//console.log("-- query_fast result from sheet")
}
}
if ( curRowArr )
return curRowArr;
else
return null;
}
//---------------------------------------------------------
update_fast(field_name,field_value,update_field_name,update_field_value)
{
var numOfRows = this.getNumOfActiveRows()
//---------------
var col_index = -1;
for ( var i = 0 ; i < this.colNames.length ; i ++)
{
if( field_name == this.colNames[i] )
col_index = i+1;
}
if ( col_index == -1)
return false;
//---------------
var update_col_index = -1;
for ( var i = 0 ; i < this.colNames.length ; i ++)
{
if( update_field_name == this.colNames[i] )
update_col_index = i+1;
}
if ( update_col_index == -1)
return false;
//---------------
for ( var i = 2 ; i <= numOfRows ; i ++)
{
if ( field_value == this.sheet.getRange(i,col_index).getValue() )
{
this.sheet.getRange(i,update_col_index).setValue(update_field_value)
return true;
}
}
if(this.clearCacheOnUpdates)
{
this.cache = {};
console.log("update_fast:clearCache")
}
return false;
}
//---------------------------------------------------------
update(details_for_updating_matched_rows,where_query_str)
{
var rows_to_update = this.query("select * " + where_query_str)
var numOfRows = this.getNumOfActiveRows();
for ( var i = 2 ; i <= numOfRows ; i ++)
{
var curRowArr = [];
for ( var j = 1 ; j <= this.colNames.length ; j ++)
{
curRowArr.push(this.sheet.getRange(i,j).getValue());
}
for (const innerList of rows_to_update)
{
if (JSON.stringify(innerList) === JSON.stringify(curRowArr))
{
// found a match - current row need to be updated
for ( var j = 1 ; j <= this.colNames.length ; j ++)
{
if ( details_for_updating_matched_rows[this.colNames[j-1]] ) // there is a field to update
{
var new_val = details_for_updating_matched_rows[this.colNames[j-1]] ;
this.sheet.getRange(i,j).setValue(new_val);
}
}
}
}
}
if(this.clearCacheOnUpdates)
{
this.cache = {};
console.log("update:clearCache")
}
}
//---------------------------------------------------------
flush()
{
SpreadsheetApp.flush();
if(this.clearCacheOnUpdates)
{
this.cache = {};
console.log("flush:clearCache")
}
}
//---------------------------------------------------------
}