-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneral.gs
189 lines (161 loc) · 4.45 KB
/
general.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
function CSVToArray( strData, strDelimiter ){
//Logger.log(strData);
strDelimiter = (strDelimiter || ",");
var objPattern = new RegExp(
(
"(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +
"(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +
"([^\"\\" + strDelimiter + "\\r\\n]*))"
),
"gi"
);
var arrData = [[]];
var arrMatches = null;
while (arrMatches = objPattern.exec( strData )){
var strMatchedDelimiter = arrMatches[ 1 ];
if (
strMatchedDelimiter.length &&
(strMatchedDelimiter != strDelimiter)
){
arrData.push( [] );
}
if (arrMatches[ 2 ]){
var strMatchedValue = arrMatches[ 2 ].replace(
new RegExp( "\"\"", "g" ),
"\""
);
} else {
var strMatchedValue = arrMatches[ 3 ];
}
arrData[ arrData.length - 1 ].push( strMatchedValue );
}
return( arrData );
}
function loadtheData (dataRow, theSheet) {
var THE_SHEET = theSheet; // name of sheet to load water meter data
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(THE_SHEET);
var lastRow = sheet.getLastRow() + 1;
if ( dataRow.length > 2 ) {
}
else {
//sheet.getRange(lastRow,1,1,2).setValues(dataRow);
for (var j = 0; j <= dataRow.length - 1; j++) {
sheet.getRange(lastRow,j+1,1,1).setValue(dataRow[j]);
}
}
/*
"Low battery"
"Battery normal"
"Host comms. start"
"Host comms. end"
"Unit started"
"Unit stopped"
"Unit restarted"
"No solar power"
"Solar power restored"
"Battery was flat"
"Head detected"
"Head not detected"
*Note
*Flow total
*Flow start
*Flow stop
*Missed meas
*Mace
*Temperature
*/
}
function logData( plogRow, pdataRow ) {
//Logger.log(plogRow);
switch (pdataRow[0]) {
case "!":
break;
case "!Program":
plogRow[2] = pdataRow[1] + " - " + pdataRow[2]; //,FloCom,2.6.1.9
break;
case "!Download Start":
plogRow[3] = pdataRow[1]; //,2015/01/05 20:33:58
break;
case "!Ident":
plogRow[4] = pdataRow[1]; //,"1-228-1"
break;
case "!SerialNo":
plogRow[5] = pdataRow[1]; //,"14553"
break;
case "!Version":
plogRow[6] = pdataRow[1] + " - " + pdataRow[2]; //,313,167
break;
case "!Logger Time":
plogRow[7] = pdataRow[1]; //,2015/01/05 19:58:38
plogRow[17] = stringToDate(plogRow[3]) - stringToDate(plogRow[7]); // time difference for time correction
break;
case "!Battery":
plogRow[8] = pdataRow[1] + " - " + pdataRow[2]; //,"OK",6.44V
break;
case "!Logger temperature":
plogRow[9] = pdataRow[1]; //,30
break;
case "!Total flow units":
plogRow[10] = pdataRow[1]; //,"Ml"
break;
case "!Channels":
plogRow[11] = pdataRow[1]; //,1
break;
case "!Names":
plogRow[12] = pdataRow[1]; //,"Flow rate"
break;
case "!Units":
plogRow[13] = pdataRow[1]; //,"Ml/day"
break;
case "!Points":
plogRow[14] = pdataRow[1]; //,22525
break;
case "!Interval":
plogRow[15] = pdataRow[1]; //,600
break;
case "!Download End":
plogRow[16] = pdataRow[1]; //,2015/01/05 20:35:17
break;
default:
Logger.log(pdataRow);
break;
}
}
var toType = function(obj) {
return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
}
function stringToDate(dateString) {
var dateTimeArray = dateString.split(" ");
var dateArray = dateTimeArray[0].split("/");
var vyear = Number(dateArray[0]);
var vmonth = Number(dateArray[1]) - 1;
var vday = Number(dateArray[2]);
var timeArray = dateTimeArray[1].split(":");
var vhour = Number(timeArray[0]);
var vminute = Number(timeArray[1]);
var vsecond = Number(timeArray[2]);
if ( vyear == Number.NaN ||
vmonth == Number.NaN ||
vday == Number.NaN ||
vhour == Number.NaN ||
vminute == Number.NaN ||
vsecond == Number.NaN ) {
Logger.log(vyear + ' | ' + vmonth + ' | ' + vday + ' | ' + vhour + ' | ' + vminute + ' | ' + vsecond + ' | ' + dateTimeArray + ' | ' + dateArray + ' | ' + timeArray);
}
var vdate = new Date(vyear, vmonth, vday, vhour, vminute, vsecond);
//if (date instanceof Date) {
if ( toType(vdate) === "date" ) {
return vdate;
} else {
Logger.log(vdate);
Logger.log(dateString);
Logger.log("non-compliant Date object detected!");
}
}
function adjustedDate(originalDate, adjustement) {
var vtime = originalDate.getTime() + adjustement;
var vdate = new Date();
vdate.setTime(vtime);
return vdate;
}