I use a script to sort a sheet on a particular item in a column.
Data in this column is a number preceded by letters so the normal sort
method from the range
or sheet
class does not work properly.
Here is the script, the actual question comes below.
function sortIDs(){ // test on column 5
sortOnNumbersInCol(5);
}
function sortOnNumbersInCol(col){ // numeric sort except for 2 first rows on numeric value in column col
var sh = SpreadsheetApp.getActive().getSheetByName('copie de travail');
var data = sh.getDataRange().getValues();
//Logger.log('length départ='+data.length);
var codes = data.shift();
var headers = data.shift();
var dataOut=[];
for(var n=0 ; n<data.length ; n++){
if(data[n][col] != ''){
dataOut.push(data[n]);
//Logger.log('data['+n+']['+col+']='+data[n][col].substring(7)+'|');
}
}
dataOut.sort(function(a,b){
var aE,bE;
aE=a[col].substring(7); // the numeric part comes at the 7 th position (and following)
bE=b[col].substring(7);
return aE - bE
})
dataOut.unshift(headers);
dataOut.unshift(codes);
sh.getRange(1,1,dataOut.length,dataOut[0].length).setValues(dataOut);
}
This works perfectly for data sorting but it doesn't care about cell colors obviously... Some of my coworkers use colors to designate items in this sheet and when I sort the range the colors don't follow.
So my question is : How can I sort this sheet with my specific criteria and keep the correlation with cell colors ?
below is a screen capture of sheet data
If I run my script on this range the colors won't move... that's my problem ;)
getBackgrounds
array, then instead of pushing values indataOut
array, how about expanding a little and pushing an object for each value with properties like{ color : "000000", value: "" }
. Or what TheMaster said below for an optimal solution: - Oleg Valter is with Ukraine01
instead of1
:MCINPRO04
instead ofMCINPRO4
. Constant 2 or 3 digits should work. - TheMaster