What I want to do: sort a range of data by the active column via shortcut key(s). I do this regularly and want to automate. However, the size and location of the range changes each time I do this.
My approach
- Select a cell in the range of interest that's in the first row and specific column I want to sort by, call it C4
- Select down to last row of the range
- Select left and/or right to all columns of the range
- Get column number / address of C4 (which should be 3, since C = column 3)
- Sort range by column of C4
I've been able to accomplish 1. to 3. I'm struggling with 4. Below is what I've got. I suspect that if I can get 4. sorted, then then last line will accomplish 5.
function sortCol() {
// KGF - 22,49
// Purpose: automatically select a range of data and sort by column of active cell
// Status - wip; haven't determined how to get col # of active cell
// Last updated: 22-12-4
var spreadsheet = SpreadsheetApp.getActive();
var currentCell = spreadsheet.getCurrentCell();
let cc1 = currentCell;
spreadsheet.getSelection().getNextDataRange(SpreadsheetApp.Direction.DOWN).activate();
spreadsheet.getActiveRange().getDataRegion(SpreadsheetApp.Dimension.COLUMNS).activate();
// how to get col # of active cell?? 22,49
let cc1_col = cc1.getColumn
console.log(cc1_col)
Logger.log(cc1_col)
//
cc1.activateAsCurrentCell();
spreadsheet.getActiveRange().sort({column: cc1_col, ascending: true});
};