-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadfile.gs
347 lines (316 loc) · 13.2 KB
/
loadfile.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/** -------------------------------------------------------------------------- **/
var email = String(Session.getActiveUser().getEmail());
function doclistUI(){
/*
// or function doGet()
{ //** see text
*/
var folderlist = new Array();
var folders = DriveApp.getFolders()
//Logger.log(folders);
for(ff=0;ff<folders.length;++ff){
//Logger.log(folders[ff]);
folderlist.push(folders[ff].getName());
}
var app = UiApp.createApplication().setHeight(260).setWidth(700).setStyleAttribute('background', 'beige')
.setStyleAttribute('margin-top', '10px').setStyleAttribute('margin-left', '10px').setTitle("Doclist UI");
var panel = app.createVerticalPanel()
var hpanel = app.createHorizontalPanel();
var Flist= app.createListBox(false).setName("Flb").setId("Flb").setVisibleItemCount(4).setWidth("180");
var Dlist= app.createListBox(false).setName("Dlb").setId("Dlb").setVisibleItemCount(8).setWidth("280");
var hidden = app.createHidden('hidden').setId('hidden');
var hiddenlink = app.createHidden('hiddenlink').setId('hiddenlink');
var Flab = app.createLabel('Folder List').setWidth("80");
var Dlab = app.createLabel('Document List').setWidth("100");
var spacer = app.createLabel(' ').setWidth("30");
Flist.addItem('Choose a folder').addItem('Root content');
for(ff=0;ff<folderlist.length;++ff){
Flist.addItem(folderlist[ff]);
Logger.log(folderlist[ff]);
}
hpanel.add(Flab).add(Flist).add(spacer).add(Dlab).add(Dlist).add(hidden).add(hiddenlink);
var docname = app.createLabel().setId('doc').setSize("360", "35");
var link = app.createAnchor('open ', 'href').setId("link").setVisible(false);
var load = app.createButton("Load meter data").setId("load").setVisible(false);
var sent = app.createButton("Loading the Meter Data").setId("sent").setVisible(false);
panel.add(hpanel).add(docname).add(link).add(load).add(sent);
var clihandler = app.createClientHandler()
.forTargets(sent).setVisible(true)
.forEventSource().setVisible(false);
load.addClickHandler(clihandler);
var FHandler = app.createServerHandler("click");
Flist.addChangeHandler(FHandler)
FHandler.addCallbackElement(hpanel);
var DHandler = app.createServerHandler("showlab");
Dlist.addChangeHandler(DHandler);
DHandler.addCallbackElement(hpanel);
var keyHandler = app.createServerHandler("LoadData");
load.addClickHandler(keyHandler)
keyHandler.addCallbackElement(hpanel);
app.add(panel);
var doc = SpreadsheetApp.getActive();//**
doc.show(app);//**
}
/* ** if you want to publish the script as a service, rename this function as doget(e) , remove both lines marked with ** and replace them by the following :
return app;
}
*/
/** -------------------------------------------------------------------------- **/
function click(e){
var app = UiApp.getActiveApplication();
var Dlist = app.getElementById("Dlb");
var hidden = app.getElementById("hidden");
var doclist = new Array();
var label = app.getElementById('doc')
var folderName = e.parameter.Flb
if (folderName=='Choose a folder'){Dlist.clear();label.setText(" ");return app}
if (folderName!='Root content'){
doclist=DriveApp.getFoldersByName(folderName).getFiles(0,2000)
var names = new Array();
for (nn=0;nn<doclist.length;++nn){
names.push([doclist[nn].getName(),doclist[nn].getId(),doclist[nn].getType()]);
}
}else{
doclist=DriveApp.getFoldersByName().getFiles(0,2000)
var names = new Array();
for (nn=0;nn<doclist.length;++nn){
names.push([doclist[nn].getName(),doclist[nn].getId(),doclist[nn].getType()]);
}
}
names.sort();
Dlist.clear();
for(dd=0;dd<names.length;++dd){
Dlist.addItem(names[dd][0]+" (doc Nr:"+dd+")");
}
hidden.setValue(names.toString());
return app ;// update UI
}
/** -------------------------------------------------------------------------- **/
function showlab(e){
var app = UiApp.getActiveApplication();
var label = app.getElementById('doc');
var link = app.getElementById('link');
var load = app.getElementById("load");
var hidden = e.parameter.hidden.split(',');
var hiddenlink = app.getElementById("hiddenlink");
//Logger.log(hidden)
if (e.parameter.Dlb!=""){
var docname = e.parameter.Dlb
var docN = docname.substr(0,docname.lastIndexOf("("));
var docindex = docname.substring(Number(docname.lastIndexOf(":"))+1,Number(docname.lastIndexOf(")")));
var doctype = hidden[docindex*3+2]
//Logger.log(doctype)
label.setText(doctype+" : "+docN).setEnabled(false).setStyleAttribute('fontSize', '15');
if (doctype=='document'){var urlstring = "https://docs.google.com/document/d/";var poststring = "/edit"}
if (doctype=='spreadsheet'){var urlstring = "https://docs.google.com/spreadsheet/ccc?key=";var poststring = "#gid=0"}
if (doctype=='item'||doctype=='other'||doctype=='blob_item'||doctype=='photo'){var urlstring = "https://docs.google.com/file/d/";var poststring = "/edit"}
var doclink = urlstring+hidden[docindex*3+1]+poststring;
//Logger.log(doclink)
link.setVisible(true).setText("Open "+doctype+" in browser").setHref(doclink);
load.setVisible(true);
hiddenlink.setValue(hidden[docindex*3+1]+"|"+doctype);
}
return app ; // update UI
}
/** -------------------------------------------------------------------------- **/
function parseFlocomData(strFile, content) {
// parseCsv will return a [][] array we can write to a sheet
var csvData = CSVToArray(content, ",");
var ss = SpreadsheetApp.getActiveSpreadsheet();
var LOAD_LOG = "Log"; // name of sheet to load logging information about each file loaded
var lsheet = ss.getSheetByName(LOAD_LOG);
var llastRow = lsheet.getLastRow();
var DATA_SHEET = "Data"; // name of sheet to load data from each file loaded
var dsheet = ss.getSheetByName(DATA_SHEET);
var dlastRow = dsheet.getLastRow();
var EVENT_SHEET = "Events"; // name of sheet to load event data from each file loaded
var esheet = ss.getSheetByName(EVENT_SHEET);
var elastRow = esheet.getLastRow();
var OTHER_SHEET = "Other"; // name of sheet to load unaccounted for data from each file loaded
var osheet = ss.getSheetByName(OTHER_SHEET);
var olastRow = osheet.getLastRow();
var firstData = true;
var data = [];
var event = [];
var other = [];
var compDate = new Date();
var lastTotal = 0.0;
var flowRatepermin = 0.0;
var interval = 0.0;
var intervalflow = 0.0;
var newtotal = 0.0;
var newadjtotal = 0.0;
var rowstatus = "";
var cnt1 = 0;
var cnt2 = 0;
var cnt3 = 0;
var lastTimeStamp = new Date(1970, 01, 01); // Time stamp of last event in data sheet
if (dlastRow > 1) {
data = dsheet.getRange(2,1,dlastRow-1,11).getValues();
}
if (elastRow > 1) {
event = esheet.getRange(2,1,elastRow-1,3).getValues();
lastTimeStamp = event[event.length-1][0];
}
Logger.log(llastRow);
Logger.log(dlastRow + " : " + data.length);
Logger.log(elastRow + " : " + event.length);
Logger.log(lastTimeStamp);
var logRow = new Array(20);
logRow[0] = strFile;
logRow[1] = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd HH:mm:ss");
for (var i = 0; i < csvData.length; i++) {
var row = csvData[i];
var checkString = row[0].slice(0,1);
if ( checkString == "!" ) {
logData( logRow, row ); // Comments
} else {
if ( row[1] == null ) {
other.push(row) ; // unaccounted for
Logger.log(row + " : " + i) ;
} else {
checkString = row[1].slice(0,1);
compDate = stringToDate(row[0]);
logRow[19] = row[0]; // Last date in file
if ( firstData ) {
logRow[18] = row[0]; // First date in file
firstData = false;
}
cnt1++;
if ( compDate > lastTimeStamp ) {
cnt2++;
if ( checkString == "*" ) {
event.push(row) ; // events
if ( row[1] == "*Flow stop" || row[1] == "*Flow start" || row[1] == "*Flow total" ) {
lastTotal = parseFloat(row[2]);
rowstatus = row[1].split(" ").pop();
}
if ( row[1] == "*Flow stop" ) {
var lastrec = data.length - 1;
interval = (compDate - data[lastrec][0]) / ( 1000 * 60 );
intervalflow = data[lastrec][2] / (60 * 24) * interval;
newadjtotal = data[lastrec][9] + intervalflow;
data[lastrec][6] = interval;
data[lastrec][7] = intervalflow;
var rowData = new Array(compDate,
rowstatus,
0.0,
adjustedDate(compDate, logRow[17]),
lastTotal,
0.0,
interval,
0.0,
lastTotal,
newadjtotal,
"");
data.push(rowData); // data
rowstatus = "";
}
} else {
flowRatepermin = parseFloat(row[1]) / (60 * 24);
if (data.length > 0) {
var lastrec = data.length - 1;
interval = (compDate - data[lastrec][0]) / ( 1000 * 60 );
intervalflow = data[lastrec][2] / (60 * 24) * interval;
data[lastrec][6] = interval;
data[lastrec][7] = intervalflow;
if ( data[lastrec][8] == 0 ) {
newtotal = 0;
newadjtotal = 0;
} else {
newtotal = data[lastrec][8] + intervalflow;
newadjtotal = data[lastrec][9] + intervalflow;
}
if ( rowstatus == "total" ) {
newtotal = lastTotal;
if ( data[lastrec][9] == 0 ) {
newadjtotal = lastTotal;
}
}
} else {
interval = logRow[15] / 60;
intervalflow = flowRatepermin * interval;
newtotal = 0;
newadjtotal = 0;
}
rowData = new Array(compDate,
rowstatus,
parseFloat(row[1]),
adjustedDate(compDate, logRow[17]),
lastTotal,
flowRatepermin,
interval,
intervalflow,
newtotal,
newadjtotal,
"");
data.push(rowData); // data
rowstatus = "";
}
} else {
cnt3++;
}
}
}
}
if ( data.length > 0 ) {
Logger.log((dlastRow+1) + " : " + data.length + " : " + data[0].length);
dsheet.getRange(2,1,data.length,data[0].length).setValues(data);
}
if ( event.length > 0 ) {
Logger.log((elastRow+1) + " : " + event.length + " : " + event[0].length);
esheet.getRange(2,1,event.length,event[0].length).setValues(event);
}
if ( other.length > 0 ) {
osheet.getRange(2,1,other.length,other[0].length).setValues(other);
}
var theLogRow = [];
theLogRow[0] = logRow;
lsheet.getRange(llastRow+1,1,1,logRow.length).setValues(theLogRow);
Logger.log(cnt1 + " : " + cnt2 + " : " + cnt3);
} // function parseFlocomData
/** -------------------------------------------------------------------------- **/
/* 0B6NHem9C-Di5M3NEb1Y0TWJLcVU */
function importGDKCSV(strFile) {
//strFile = (strFile || "flocom-gdk-201611242110.prn");
//strFile = (strFile || "flocom-gdk-201703070645.prn");
//strFile = (strFile || "flowcom-gdk-201705091906.prn");
//strFile = (strFile || "flowcom-gdk-201710070813.prn");
//strFile = (strFile || "flocom20180108192700.prn");
//strFile = (strFile || "flocom-gdk-20180412080600.prn");
//strFile = (strFile || "flocom-gdk-20180604193400.prn");
//strFile = (strFile || "flocom-gdk-20181021120900.prn");
//strFile = (strFile || "flocom-gdk-20181225095300.prn");
//strFile = (strFile || "flocom-gdk-20190125204500.prn");
//strFile = (strFile || "flocom-gdk-20190216122700.prn");
//strFile = (strFile || "flocom-gdk-20190401000000.prn");
strFile = (strFile || "flocom-gdk-20190804173200");
var searchTerm = "title = '" + strFile + "'";
// search for our file
var files = DriveApp.searchFiles(searchTerm)
var content = "";
// Loop through the results
while (files.hasNext()) {
var file = files.next();
// assuming the first file we find is the one we want
if (file.getName() == strFile) {
// get file as a string
content = file.getBlob().getDataAsString();
break;
}
}
parseFlocomData(strFile, content);
} // function importGDKCSV
/** -------------------------------------------------------------------------- **/
function LoadData(e) {
var app = UiApp.getActiveApplication();
var load = app.getElementById("load");
var hiddenlink = e.parameter.hiddenlink;
var ID = hiddenlink.substring(0,Number(hiddenlink.lastIndexOf("|")));
var sent = app.getElementById("sent");
var fileBlob = DriveApp.getFileById(ID).getBlob();
var content = fileBlob.getDataAsString();
parseFlocomData(DriveApp.getFileById(ID).getName(), content);
sent.setVisible(false);
return app ; // update UI
} // function LoadData