Say I have Account Numbers, Sub-account Numbers and values in a google spreadsheet where there can be duplicate Account Numbers with different sub-acc numbers:
acc no / sub acc no / val
2 / 5 / 6
3 / 10 / 8
4 / 9 / 2
2 / 14 / 1
After I sort the whole sheet by the third column, values, it's gonna look like this:
acc no / sub acc no / val
3 / 10 / 8
2 / 5 / 6
4 / 9 / 2
2 / 14 / 1
So, what I want to do is to group all meters under the same accounts while having the original sort remain intact:
acc no / sub acc no / val
3 / 10 / 8
2 / 5 / 6
2 / 14 / 1
4 / 9 / 2
Seems like after sorting it by values, I need a script that loops through the account No column, for each cell it will check the remaining account numbers, and if there's a matching duplicate one, it's gonna get moved up.
This is what I could come up with so far:
function group(){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
// find how many accounts we have
var Avals = sheet.getRange("A2:A").getValues();
var Alast = Avals.filter(String).length;
for (var i = 0; i < Alast-2; i++) {
if(Avals[i].toString() == Avals[i+1].toString()){
continue;
}
for (var j = i+2; j < Alast; j++){
if(Avals[i].toString() == Avals[j].toString()){
//move
break;
}
}
}
}
Thanks in advance!
[ [r1c1, r1c2, ... r1cN], [r2c1, r2c2, ...r2cN], ... [rNc1, ... rNcN] ]See also stackoverflow.com/help/how-to-ask - tehhowch