-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code.gs
96 lines (80 loc) · 2.66 KB
/
Code.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
function compileAndSort(...sheets) {
// Pulls however many arguments and turn them into an array
var sheets = Array.prototype.slice.call(arguments);
//initialize our, now empty final array
var mergedData = [];
var finalData = [];
// Loop through each sheet passed to the function
for(sheet of sheets) {
//take whatever is in our result array and mergeshot it with the next sheet
mergedData = mergeSort(mergedData, sheet)
}
//Sort the data again to be sure
mergedData = mergedData.sort(sortFirstColumn);
//check for blank cells in the first two rows
for (row of mergedData) {
if((row) && (row[1] != '') && (row[0] != '')){
finalData.push(row);
}
}
return finalData;
}
//sort function that sorts by the first column
function sortFirstColumn(a,b){
if(a[0] === b[0]){
return 0;
}
else {
return (a[0] < b[0]) ? 1 : -1;
}
}
function mergeSort(left, right) {
//initize variables
var inxLeft = 0;
var inxRight = 0;
//sum the length of our two input arrays to find out what how long the merged array should be
var finalLength = left.length + right.length;
//initialize our, now empty, final array
var result = [];
//Continue to preform a merge sort until the result array is the lenght it should be
while (result.length <= finalLength){
//Check to see if there actually is an item at this index, if not, push and advance the other array
if (!left[inxLeft]){
result.push(right[inxRight]);
if (inxRight < (right.length)) {
inxRight++
}
}
else if (!right[inxRight]){
result.push(left[inxLeft]);
if (inxLeft < (left.length)) {
inxLeft++
}
}
//Compare the first column of the selected row, push the larger row, and advance it
else if(left[inxLeft][0] > right[inxRight][0]){
result.push(left[inxLeft]);
if (inxLeft < (left.length)) {
inxLeft++
}
}
//If they're the same push both
else if(left[inxLeft][0] == right[inxRight][0]) {
result.push(left[inxLeft]);
result.push(right[inxRight]);
if (inxLeft < (left.length)) {
inxLeft++
}
if (inxRight < (right.length)) {
inxRight++
}
}
else {
result.push(right[inxRight]);
if (inxRight < (right.length)) {
inxRight++
}
}
}
return result;
}