-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.gs
309 lines (265 loc) · 10.2 KB
/
main.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
/** TODO
* simplification of the SheetObject constructor method into accepting one object as parameter
* then creation of new such objects is more DRY, without repeating the constants
*
*/
/** NOTES
* the comments in the form [sheetobject].output.setValue are for debugging purposes
* useful if you want to tweak the code for your needs
*
*/
const JOURNEY_COLUMN = 3;
const ACTION_COLUMN = 13;
const CUSTOM_ROW = 1;
const SORT_ROW = 2;
const STARTING_ROW = 4;
const HEADERS_RANGE = 'B3:R3';
const OUTPUT_STRING = 'H1';
const DEFAULT_RANGE = 'B4:R600';
const DEFAULT_RANGE_STRING = 'B2:L2';
const DEFAULT_RANGE_UNBOUNDED = 'B4:R';
const LAST_INDEX = 'A1';
const COLUMN_AS_TYPE = 'K1';
const WHICH_ITEM_ON_COLUMN = 'M1';
// JOURNEY
const JOURNEY_COLUMN_IN_LIST = 5;
const LABELS_ROW = 2;
const LABELS_STARTING_COLUMN = 2;
const LABELS_DISTANCE_BETWEEN_COLUMNS = 2;
function SheetObject(name, rangeString, sortRangeString){
var sheet = SpreadsheetApp.getActive().getSheetByName(name);
if (rangeString == 0){
rangeString = getDefaultRange(sheet);
}
var result = {
sheet: sheet,
range: sheet.getRange(rangeString),
sortingRange: sheet.getRange(sortRangeString),
output: sheet.getRange(OUTPUT_STRING),
name: name
}
return result;
}
function getDefaultRange(sheet){
var lastIndex = sheet.getRange(LAST_INDEX).getValue().toString();
return DEFAULT_RANGE_UNBOUNDED + lastIndex + STARTING_ROW;
}
// these cannot work as global variables (in apps script as oppposed to JS), so are here just for reference
var listSheetObject = new SheetObject('list', 0, DEFAULT_RANGE_STRING);
var customSheetObject = new SheetObject('custom', 0, DEFAULT_RANGE_STRING);
var journeySheetObject = new SheetObject('journey', 'K7:L600', 'K4');
//general function to redirect
function onEdit(event){
var sheet = event.source.getActiveSheet();
var sheetName = sheet.getName();
var editedCell = sheet.getActiveCell();
var listSheetObject = new SheetObject('list', 0, DEFAULT_RANGE_STRING);
var customSheetObject = new SheetObject('custom', 0, DEFAULT_RANGE_STRING);
var journeySheetObject = new SheetObject('journey', 'K7:L600', 'K4');
listSheetObject.output.setValue("onedit: " + sheetName + ": vs " + listSheetObject.name);
switch (sheetName){
case listSheetObject.name:
listSheetObject.output.setValue("sending to list functions");
listFunctions(editedCell, listSheetObject);
break;
case customSheetObject.name:
if(editedCell.getRow() == CUSTOM_ROW){
listSheetObject.output.setValue("sending to custom");
moveToCustom();
}else{
listSheetObject.output.setValue("sorting the custom one");
listFunctions(editedCell, customSheetObject);
}
break;
case journeySheetObject.name:
listSheetObject.output.setValue("sending to journey");
moveToJourney();
break;
default:
}
}
//functions concerning the main list
function listFunctions(editedCell, sheetObject){
var value = editedCell.getValue();
var column = editedCell.getColumn();
var row = editedCell.getRow();
sheetObject.output.setValue("listing");
if ( row == SORT_ROW ){ //sorting by custom column order - so two or more columns possible
sortListAdvanced(sheetObject);
}else if( value == 'del' && column == ACTION_COLUMN ){
// deleteRow(row, sheetObject);
sheetObject.sheet.deleteRow(row);
}else if( column == JOURNEY_COLUMN_IN_LIST ){
sheetObject.output.setValue("moving to journey");
moveToJourney();
}
}
function deleteRow(row, sheetObject){
var resetRange = sheetObject.sheet.getRange("A"+row+":R" + row);
resetRange.clearContent();
sortList(sheetObject.sheet);
}
function getSortDirections(sheetObject){
sheetObject.output.setValue("getting sort directions");
var values = sheetObject.sortingRange.getValues();
sheetObject.output.setValue(values);
const COLUMN_OFFSET = 2;
var columnPriorityPairs = [];
var justOneRow = values[0];
for(var i = 0; i< justOneRow.length; i++){
if(justOneRow[i] != 0){
columnPriorityPairs.push({column: COLUMN_OFFSET + i, prio: justOneRow[i]});
}
}
return columnPriorityPairs;
}
//it's set for descending now, to reverse change (1 into -1) and vice versa
function comparePairs(a, b) {
if (a.prio < b.prio) {
return 1;
//return -1;
}
if (a.prio > b.prio) {
return -1;
// return 1;
}
return 0;
}
function sortListAdvanced(sheetObject){
var columnPriorityPairs = getSortDirections(sheetObject);
sheetObject.output.setValue("starting sorting");
sheetObject.output.setValue(typeof columnPriorityPairs);
columnPriorityPairs.sort(comparePairs);
var sortArray = [];
for(var i = 0; i < columnPriorityPairs.length; i++){
sortArray.push({column: columnPriorityPairs[i].column, ascending: false});
}
sheetObject.output.setValue(sortArray.length);
sheetObject.range.sort(sortArray);
}
function getColumnNumber(string){
var listSheetObject = new SheetObject('list', 0, DEFAULT_RANGE_STRING);
var values = listSheetObject.sheet.getRange(HEADERS_RANGE).getValues();
var result = '-1';
var counter = 0;
for (var i = 0; i < values[0].length;i++){
counter++;
listSheetObject.output.setValue("that's the column value: " + values[0][i] + ";that's values" + values + "string: " + string);
if (values[0][i] == string){ //first non empty that matches
result = i;
//listSheetObject.output.setValue("i value: " + i);
}
}
listSheetObject.output.setValue("that's the column index: " + result +";that's values" + values + "string: " + string + " c: " + counter);
return result;
}
//functions for displaying in different sheets
function moveToCustom(){
var listSheetObject = new SheetObject('list', 0, DEFAULT_RANGE_STRING);
var customSheetObject = new SheetObject('custom', 0, DEFAULT_RANGE_STRING);
//get column and input wanted
const DESIRED_COLUMN_STRING = customSheetObject.sheet.getRange(COLUMN_AS_TYPE).getValue();
const DESIRED_COLUMN_VALUE = customSheetObject.sheet.getRange(WHICH_ITEM_ON_COLUMN).getValue();
const DESIRED_COLUMN_INDEX = getColumnNumber(DESIRED_COLUMN_STRING);
// listSheetObject.output.setValue("actual column number: " + DESIRED_COLUMN_INDEX + " string: " + DESIRED_COLUMN_STRING);
var data = listSheetObject.range.getValues();
var newData = [];
for (var i in data) {
//listSheetObject.output.setValue("actual vs expected: " + data[i] + ":" + data[i][DESIRED_COLUMN_INDEX] + DESIRED_COLUMN_VALUE);
if (data[i][DESIRED_COLUMN_INDEX] == DESIRED_COLUMN_VALUE) {
listSheetObject.output.setValue("data: " + data[i] + "i values: " + [data[i][0], data[i][1]]);
newData.push(data[i]);
}
}
//copy to the new place
customSheetObject.range.clearContent();
if(newData[0] != undefined){
customSheetObject.sheet.getRange(4, 2, newData.length, newData[0].length).setValues(newData);
customSheetObject.output.setValue("success!");
}else{
customSheetObject.output.setValue("error in getting data");
}
}
function getValueOfRangeString(sheetObject, string){
return sheetObject.sheet.getRange(string).getValue();
}
function getValueOfRangeNums(sheetObject, row, column){
return sheetObject.sheet.getRange(row, column).getValue();
}
function getColumnForItems(columnIndex){
return columnIndex - 1;
}
function labelsToString(labels){
var string = "";
for(var i = 0; i < labels.length; i++){
string += labels[i].value + " " + labels[i].row + " " + labels[i].column + " : ";
}
return string;
}
function getLabels(sheetObject){
const LABEL_LENGTH = 1;
var labels = [];
var currentColumn = LABELS_STARTING_COLUMN;
var cellValue = getValueOfRangeNums(sheetObject, LABELS_ROW, currentColumn);
var rowForItems = LABELS_ROW + 2;
while(cellValue.length == LABEL_LENGTH){
labels.push({
value: cellValue,
row: rowForItems,
column: getColumnForItems(currentColumn)
});
currentColumn += LABELS_DISTANCE_BETWEEN_COLUMNS;
cellValue = getValueOfRangeNums(sheetObject, LABELS_ROW, currentColumn);
}
// sheetObject.output.setValue(labelsToString(labels));
return labels;
}
// go through the items and putting into 3d array; but not equal sizes
// output the stuff into the correct rows; nope, do it all at one time, get and move
function moveToJourney(){
var listSheetObject = new SheetObject('list', 0, DEFAULT_RANGE_STRING);
var journeySheetObject = new SheetObject('journey', 'K7:L600', 'K4');
journeySheetObject.sheet.getRange('A4:J200').clearContent();
var data = listSheetObject.range.getValues();
var labels = getLabels(journeySheetObject);
const DATA_WIDTH = 2;
var counter = new Array(labels.length).fill(0);
for (var dataRowIndex in data) {
var value = data[dataRowIndex][JOURNEY_COLUMN];
for(var labelIndex = 0; labelIndex < labels.length; labelIndex++){
if (value == labels[labelIndex].value) {
var finalRow = labels[labelIndex].row + counter[labelIndex];
// journeySheetObject.output.setValue("final row: "+ finalRow + "labels: " + labels[1].value);
journeySheetObject.sheet.getRange(finalRow, labels[labelIndex].column, 1, DATA_WIDTH).setValues([[data[dataRowIndex][0], data[dataRowIndex][1]]]);
// copying just name and mass
// journeySheetObject.output.setValue("i values: " + [data[dataRowIndex][0], data[dataRowIndex][1]]);
counter[labelIndex]++;
}
// journeySheetObject.output.setValue("actual vs expected: " + counter + ":" + data[i] + ":" + data[i][JOURNEY_COLUMN] + " : " + JOURNEY_VALUE + "::" + JOURNEY_COLUMN);
}
}
// journeySheetObject.output.setValue("succeeded custom moving");
}
//functions invoked by macros - not automatic
/**
* Removes duplicate rows from the list table range
*/
function removeDuplicates(){
var listSheetObject = new SheetObject('list', 0, DEFAULT_RANGE_STRING);
var data = listSheetObject.range.getValues();
var newData = [];
for (var i in data) {
var row = data[i];
var duplicate = false;
for (var j in newData) {
if (row[0] == newData[j][0]) {
duplicate = true;
}
}
if (!duplicate) {
newData.push(row);
}
}
listSheetObject.range.clearContent(); //that is not a function error
listSheetObject.sheet.getRange(4, 2, newData.length, newData[0].length).setValues(newData);
}