-
Notifications
You must be signed in to change notification settings - Fork 4
/
autoSort
52 lines (45 loc) · 2.29 KB
/
autoSort
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
/* FUNCTION to automatically sort selected sheet by Columns */
function autoSort(SheetToSort,SortFieldOne,SortFieldOneType,SortFieldOneDir,SortFieldTwo,SortFieldTwoType,SortFieldTwoDir,NumColHeaders) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh0 = ss.getSheetByName(SheetToSort);
var SheetToTarget = sh0;
var NumHeaderRows = NumColHeaders;
var FirstColumnToSortBy = SortFieldOne;
var SecondColumnToSortBy = SortFieldTwo;
var SetDateFormat = "dddd m/dd/yyyy h:mm am/pm";
if (SortFieldOneDir == "Asc") {
var FirstColumnToSortByDir = true;
}else{
var FirstColumnToSortByDir = false;
};
if (SortFieldTwoDir == "Asc") {
var SecondColumnToSortByDir = true;
}else{
var SecondColumnToSortByDir = false;
};
if (SortFieldOneType == "Date") {
var DateColumnLetter = convertColumnIndexToLetter(FirstColumnToSortBy);
var DateColumnRange = SheetToSort+"!"+DateColumnLetter+NumColHeaders+":"+DateColumnLetter;
var cell = SheetToTarget.getRange(DateColumnRange);
cell.setNumberFormat(SetDateFormat);
};
if (SortFieldTwoType == "Date") {
var DateColumnLetter = convertColumnIndexToLetter(SecondColumnToSortBy);
var DateColumnRange = SheetToSort+"!"+DateColumnLetter+NumColHeaders+":"+DateColumnLetter;
var cell = SheetToTarget.getRange(DateColumnRange);
SpreadsheetApp.getActiveSheet().getRange(DateColumn);
cell.setNumberFormat(SetDateFormat);
};
// Get the entire set of data for this sheet.
var range = SheetToTarget.getDataRange();
// Then, if there are any header rows, offset our range to remove them from it; otherwise, they will end up being sorted as well.
if (NumHeaderRows > 0) {
// Setting the second parameter of offset() to 0 to prevents it from
// shifting any columns. Note that row headers wouldn't make much
// sense here, but this is where you would modify it if you
// wanted support for those as well.
range = range.offset(NumHeaderRows, 0);
}
// Perform the actual sort.
range.sort([{column: FirstColumnToSortBy, ascending: FirstColumnToSortByDir},{column: SecondColumnToSortBy, ascending: SecondColumnToSortByDir}]);
}