I’m trying to get some answers on how to automatically sort/match/filter columns and rearrange data if they match.
For example:
screenshot here
So when the name in column A matches with Column E, column E will them move to the adjacent cell of the match and so on. So for those that does not have a match, whether in Column A and B, they’ll be thrown below or above.
I tried using a script that was answered by Tanaike but the application might not be the same based on the conditions.
Any help would be appreciated big time!
Thanks in advance!
Dexter
I think this is what you want but I can’t be sure:
function sortof() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet0");
const vs = sh.getRange(2, 1, sh.getLastRow() - 1, sh.getLastColumn()).getDisplayValues();
vs.sort((a, b) => {
if (a[0] > b[0]) return 1;
if (b[0] > a[0]) return -1;
if (a[0] == b[0]) return 0;
})
sh.getRange(2, 1, vs.length, vs[0].length).setValues(vs)
let ob = vs.reduce((a, r, i, A) => {
let idx = a["col1"].indexOf(r[4]);
if (~idx) {
let t = A[idx][4]
a["col5"][idx] = [r[4]];
a["col5"][i] = [t];
} else {
if (!a["col5"][i]) {
a["col5"][i] = [r[4]];
}
}
return a;
}, { col1: vs.map(r => r[0]), col5: [] })
sh.getRange(2, 5, ob["col5"].length).setValues(ob["col5"]);
Logger.log(JSON.stringify(ob));
}
Sheet0: before
COL1 | COL2 | COL3 | COL4 | COL5 |
---|---|---|---|---|
Name1 | Name11 | |||
Name2 | Name12 | |||
Name3 | Name13 | |||
Name4 | Name14 | |||
Name5 | Name15 | |||
Name6 | Name16 | |||
Name7 | Name17 | |||
Name8 | Name18 | |||
Name9 | Name19 | |||
Name10 | Name20 | |||
Name11 | Name21 | |||
Name12 | Name22 | |||
Name13 | Name23 | |||
Name14 | Name24 | |||
Name15 | Name25 |
Sheet0: after
COL1 | COL2 | COL3 | COL4 | COL5 |
---|---|---|---|---|
Name1 | Name21 | |||
Name10 | Name20 | |||
Name11 | Name11 | |||
Name12 | Name12 | |||
Name13 | Name13 | |||
Name14 | Name14 | |||
Name15 | Name15 | |||
Name2 | Name22 | |||
Name3 | Name23 | |||
Name4 | Name24 | |||
Name5 | Name25 | |||
Name6 | Name16 | |||
Name7 | Name17 | |||
Name8 | Name18 | |||
Name9 | Name19 |
Thrown below or above is not a useful description.
– Array.sort