I have a spreadsheet with several columns that I need to sort individually.
I wrote the script below that works but is a bit slow since it handles each column in turn with getValues() and setValues().
I'd like to find a way to do the whole sorting at an array level for more efficiency but I don't know how... any suggestion ?
Here is the relevant part of the code I use now :
...
sh3.getRange(1,1,1,newData[0].length).setFontWeight('bold');// newData is an array corresponding to the whole sheet
for(col=1;col<newData[0].length;++col){
var val = sh3.getRange(2,col,getLastRowInCol(col),1).getValues().sort();// each column have a different height
sh3.getRange(2,col,getLastRowInCol(col),1).setValues(val)
}
}
function getLastRowInCol(col){
var values = sh3.getRange(2,col,sh3.getLastRow(),1).getValues();// skip header and find last non empty row in column
for(n=0;n<values.length;++n){
if(values[n]==''){break}
}
return n
}
Note : I know there is a Library by Romain Vialard that does the job (sorting columns in 2D arrays) but I'm interrested on how to do it 'manually' for personal JS skills improvement ;-) and also I need to sort every column independently without needing to update the sheet for every column.