I need to merge duplicate rows in an array and concatenate cells in multiple columns.
Script to do this on a single column gotten here: google script sheet, merging rows duplicate in array
Id | From | To | Transaction | Flow | Running Balance | Description | Date | Type |
---|---|---|---|---|---|---|---|---|
conn-1 | Alan | Alícia | Giving1 | 1 | 60 | Stuff1 | 20/10/2022 | Person |
conn-2 | Alan | Ashish | Giving2 | 2 | 62 | Stuff2 | 26/10/2022 | Person |
conn-3 | Alan | Ashish | Giving3 | 3 | 59 | Stuff3 | 26/11/2022 | Person |
conn-4 | Alan | Deborah | Giving4 | 15 | 48 | Stuff4 | 26/10/2022 | Person |
conn-5 | Christine | Deborah | Giving5 | 1 | 47 | Stuff5 | 26/10/2022 | Person |
conn-6 | Christine | Deborah | Giving6 | 4 | 61 | Stuff6 | 26/10/2022 | Person |
conn-7 | Christine | Deborah | Giving7 | 4 | 61 | Stuff7 | 26/10/2023 | Person |
conn-7 | Christine | Gitanjali | Giving8 | 1 | 54 | Stuff8 | 26/10/2022 | Person |
conn-8 | Christine | Jacklina | Giving9 | 7 | 43 | Stuff9 | 26/10/2022 | Person |
If run merrgRows on the Transaction
column, I get
Id | From | To | Transaction | Flow | Running Balance | Description | Date | Type |
---|---|---|---|---|---|---|---|---|
conn-1 | Alan | Alícia | Giving1 | 1 | 60 | Stuff1 | 20/10/2022 | Person |
conn-2 | Alan | Ashish | Giving2|Giving3 | 2 | 62 | Stuff2 | 26/10/2022 | Person |
conn-4 | Alan | Deborah | Giving4 | 15 | 48 | Stuff4 | 26/10/2022 | Person |
conn-5 | Christine | Deborah | Giving5|Giving6|Giving7 | 1 | 47 | Stuff5 | 26/10/2022 | Person |
conn-7 | Christine | Gitanjali | Giving8 | 1 | 54 | Stuff8 | 26/10/2022 | Person |
conn-8 | Christine | Jacklina | Giving9 | 7 | 43 | Stuff9 | 26/10/2022 | Person |
Which is what I want.
But I need to concatenate multiple columns, not just on Transaction.
Original script
function mergeRow (rows) {
let newRows = []
let matched = []
for (var i = 0; i < rows.length; i ) {
if (!matched.includes(i)) {
let a = rows[i]
let nextIndex = i 1
let matches = []
for (var x = nextIndex; x < rows.length; x ) {
if (a[1] === rows[x][1] && a[2] === rows[x][2]) {
matches.push(x);
}
}
let newRow = a,
n = 3,
lastItem = a[n];
matches.forEach(index => {
lastItem = ':' rows[index][n]
matched.push(index)
})
newRow[n] = lastItem
newRows.push(newRow)
}
}
return newRows
}
My attempt to generalize mergeRow
function mergeRow2 (rows) {
let newRows = []
let matched = []
for (var i = 0; i < rows.length; i ) {
if (!matched.includes(i)) {
let a = rows[i] //iterating trough each row
let nextIndex = i 1
let matches = []
for (var x = nextIndex; x < rows.length; x ) {
if (a[1] === rows[x][1] && a[2] === rows[x][2]) {
matches.push(x);
}
}
let cols = [3,6];
cols.forEach(function(n){
var newRow = a
var lastItem = a[n] // The Input values for the first part of the concatenation
matches.forEach(index => {
lastItem = '|' rows[index][n] //The column values to be concatenated to lastItem
matched.push(index)
})
newRow[n] = lastItem //The OUTPUT Column
newRows.push(newRow)
})
}
}
return newRows
}
I get
Id | From | To | Transaction | Flow | Running Balance | Description | Date | Type |
---|---|---|---|---|---|---|---|---|
Id | From | To | Transaction | Flow | Running Balance | Description | Date | Type |
conn-1 | Alan | Alícia | Giving1 | 1 | 60 | Stuff1 | 20/10/2022 | Person |
conn-1 | Alan | Alícia | Giving1 | 1 | 60 | Stuff1 | 20/10/2022 | Person |
conn-2 | Alan | Ashish | Giving2|Giving3 | 2 | 62 | Stuff2|Stuff3 | 26/10/2022 | Person |
conn-2 | Alan | Ashish | Giving2|Giving3 | 2 | 62 | Stuff2|Stuff3 | 26/10/2022 | Person |
conn-4 | Alan | Deborah | Giving4 | 15 | 48 | Stuff4 | 26/10/2022 | Person |
conn-4 | Alan | Deborah | Giving4 | 15 | 48 | Stuff4 | 26/10/2022 | Person |
conn-5 | Christine | Deborah | Giving5|Giving6|Giving7 | 1 | 47 | Stuff5|Stuff6|Stuff7 | 26/10/2022 | Person |
conn-5 | Christine | Deborah | Giving5|Giving6|Giving7 | 1 | 47 | Stuff5|Stuff6|Stuff7 | 26/10/2022 | Person |
conn-7 | Christine | Gitanjali | Giving8 | 1 | 54 | Stuff8 | 26/10/2022 | Person |
conn-7 | Christine | Gitanjali | Giving8 | 1 | 54 | Stuff8 | 26/10/2022 | Person |
conn-8 | Christine | Jacklina | Giving9 | 7 | 43 | Stuff9 | 26/10/2022 | Person |
conn-8 | Christine | Jacklina | Giving9 | 7 | 43 | Stuff9 | 26/10/2022 | Person |
What I need
Id | From | To | Transaction | Flow | Running Balance | Description | Date | Type |
---|---|---|---|---|---|---|---|---|
conn-1 | Alan | Alícia | Giving1 | 1 | 60 | Stuff1 | 20/10/2022 | Person |
conn-2 | Alan | Ashish | Giving2|Giving3 | 2 | 62 | Stuff2|Stuff3 | 26/10/2022 | Person |
conn-4 | Alan | Deborah | Giving4 | 15 | 48 | Stuff4 | 26/10/2022 | Person |
conn-5 | Christine | Deborah | Giving5|Giving6|Giving7 | 1 | 47 | Stuff5|Stuff6|Stuff7 | 26/10/2022 | Person |
conn-7 | Christine | Gitanjali | Giving8 | 1 | 54 | Stuff8 | 26/10/2022 | Person |
conn-8 | Christine | Jacklina | Giving9 | 7 | 43 | Stuff9 | 26/10/2022 | Person |
How to concatenate multiple columns?
Google sheet with data https://docs.google.com/spreadsheets/d/11GOevQJXEQAvljesu46SYWD4Yvfi-FCLl39EtCdBBa0/edit?usp=sharing
CodePudding user response:
Try this (I generalized a bit more)
//mC1 1st column index to test for dups
//mC2 2nd column index to test for dups
//Array of column indices to concatenate when merging rows duplicates in array
function mergeRow2(rows,mC1,mC2, cols) {
let newRows = [],
matched = [],
headers = rows[0];
for (var i = 0; i < rows.length; i ) {
if (!matched.includes(i)) {
let a = rows[i],
nextIndex = i 1,
matches = [];
for (var x = nextIndex; x < rows.length; x ) {
if (a[mC1] === rows[x][mC1] && a[mC2] === rows[x][mC2]) {
matches.push(x);
}
}
let newRow = a,
lastItem = [];
for(var n=0; n<= cols.length-1; n ) {
matches.forEach(index => {
lastItem = a[cols[n]]
lastItem = '|' rows[index][cols[n]]
matched.push(index)
newRow[cols[n]] = lastItem
})
}
newRows.push(newRow)
};
}
return newRows
}