Home > Blockchain >  How to compare and concatenate several data in a 2D array on google app script
How to compare and concatenate several data in a 2D array on google app script

Time:05-05

I have a google sheet "test" with the following data enter image description here

Each user mail has a name associated to it. I would like to merge on one line the emails that are repeated and concatenate the first names associated with them to obtain the following result on column C and D like that : enter image description here

I tried this code below because I think that the solution must be solved with a 2D array because the google sheet can receive a large number of emails and different first names.

function myFunction() {
var test = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("test");
const [,...value] = test.getDataRange().getValues(); // Get the values of the arrays

const arrayValue = value.flatMap(([a1,v1]) =>
value.map(([a2,v2]) => {
  const verif = a1==a2 && v1!=v2 ? a2.concat(',' v1 ',' v2):'no';
  const name = verif == a2.concat(',' v1 ',' v2) ?  v1.concat (',' v2):''; 
  const mail = name != '' ? a2:''; 
    return [mail,name];
  })
);
 test.getRange(1, 3, arrayValue.length,2).setValues(arrayValue);
}

Today with my code I only obtain this result

  • Related