-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAuxilliaries.gs
199 lines (168 loc) · 4.07 KB
/
Auxilliaries.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
/**
* Createsa two dimensional array out of an associative array
*/
function untable(table) {
var output = [[]];
var keys = Object.keys(table)
for (var col in keys) {
output[0].push(keys[col]);
}
for (var row = 0; row < table[keys[0]].length; row++) {
output.push([]);
for (var col = 0; col < keys.length; col++) {
output[row + 1].push(table[keys[col]][row]);
}
}
return output;
}
function objectToArray(object) {
var output = [];
return Object.keys(object).map(function(key) {return object[key];});
}
function repeat(input, times) {
var output = [];
for (var i = 0; i < times; i++) {
output.push(input);
}
return output;
}
function isRectangularArray(data) {
if (!Array.isArray(data)) {return false;}
var initialLength = undefined;
for (var row in data) {
if (!Array.isArray(data[row])) {return false;}
if (initialLength === undefined) {initialLength = data[row].length;}
if (data[row].length !== initialLength) {return false;}
}
return true;
}
/*
* Returns the length of a table.
* More generally it returns the length of the longest array within an object.
*/
function nRow(o) {
var keys = Object.keys(o);
var maxLength = 0
for (var key in keys) {
if (o[keys[key]].length > maxLength) {
maxLength = o[keys[key]].length;
}
}
return maxLength;
}
/**
* Creates an associative Object out of a two dimensional array
*/
function table(data, headers) {
if (!isRectangularArray(data)) {throw "Data not unragged array of arrays"}
if (!(headers < data.length && headers >= -1)) {
throw "Header is " + headers + " must be between -1 and " + data.length;
}
headers = (headers === undefined ? 0 : headers);
var output = {};
// Create Columns
for (var col in data[0]) {
output[(headers === -1 ? "Col " + col : data[headers][col])] = [];
}
// Fill columns
for (var row in data) {
if(row != headers) {
for (var col in data[row]) {
output[(headers === -1 ?
"Col " + col :
data[headers][col])].push(data[row][col]);
}
}
}
return output;
}
/**
* Checks if a row of a two dimensional array contains only empty strings
*/
function rowIsEmpty(a, row) {
for (var col in a[row]) {
if (a[row][col] !== "" && a[row][col] !== undefined) {
return false;
}
}
return true;
}
/**
* Checks if a column is empty
*/
function columnIsEmpty(a, col) {
for (var row in a[0]) {
if (a[row] == null) {
return true;
}
if (a[row][col] !== "" && a[row][col] !== undefined) {
return false;
}
}
return true;
}
/**
* Filters a two dimensional array for empty values
*/
function filterMatrix(m, behavior) {
if (behavior == 0 || behavior == 1) {
var row = 0;
while(row < m.length) {
if (rowIsEmpty(m, row)) {
m.splice(row,1);
row--;
}
row++;
}
}
if (behavior == 0 || behavior == 2) {
var col = 0;
while (col < m[0].length) {
if (columnIsEmpty(m, col)) {
for (var row in m) {
m[row].splice(col, 1);
}
col--;
}
col++;
}
}
return m;
}
/**
* Converts a JS date object into a Google Sheets Date Value
*/
function sheetDate(a) {
var ms_per_day = 1000 * 60 * 60 * 24;
var b = new Date(1899, 11, 30)
var utc1 = Date.UTC(a.getFullYear(),
a.getMonth(),
a.getDate(),
a.getHours(),
a.getMinutes(),
a.getSeconds(),
a.getMilliseconds());
var utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());
return (utc1 - utc2) / ms_per_day;
}
/**
* Casts a string to a number of possible, otherwise returns the string.
*/
function nativeType(x) {
if (!isNaN(new Date(x).getTime())) {
return new Date(x);
}
if (!isNaN(Number(x))) {
return Number(x);
}
return x;
}
/*
* Converts each coumn of a row of a 2d array to its native type
*/
function convertRowToNative(a, row) {
row = row || 0;
for (var col in a[row]) {
a[row][col] = nativeType(a[row][col]);
}
}