as the title suggests I\'m trying to find a way to treat some data in a spreadsheet to google app. I think the best way is to directly set an example and thank you in advan
have a look at this post, he want to do almost the same. Modifying 2 lines of the code and it's done:
function myFunction() {
var objList={};
var ss = SpreadsheetApp.getActive().getActiveSheet();
var data = ss.getDataRange().getValues();
for(var i in data){
// for(var j in data[i]){
if(typeof objList[data[i][0]]=="undefined"){
objList[data[i][0]]=[]; // new array
objList[data[i][0]].push(data[i][1]); // push the value of the second column
}
else{
objList[data[i][0]].push(data[i][1]);
}
//}
}
var objTable=[];
for(var k in objList){
objTable.push([k,objList[k]]);
}
Logger.log(objTable);
ss.clear();
ss.getRange(1, 1, objTable.length, objTable[0].length).setValues(objTable);
}
Here is a code that does exactly what you expected using a simple loop and a comparison.
function merge_col() {
var ss = SpreadsheetApp.getActive().getSheetByName('sheet1');
var data = ss.getDataRange().getValues();
var header = data.shift();
var col1 = data[1][0];
var col2 = '';
var col3 = '';
var dataSheet2 = [header];
for(var i=0 ; i<data.length ; i++){
Logger.log(data[i][0]+' - '+col1+' - '+data[i][2])
if(col1==data[i][0]){
col1 = data[i][0];
col2+=data[i][1]+' | ';
col3+=data[i][2]+' | ';
}else{
dataSheet2.push([col1,col2,col3]);
var col1 = data[i][0];
col2 = data[i][1]+' | ';
col3 = data[i][2]+' | ';
}
}
dataSheet2.push([col1,col2,col3]);
Logger.log(dataSheet2);
var sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('sheet2');
sheet2.clear();
sheet2.getRange(1, 1, dataSheet2.length, dataSheet2[0].length).setValues(dataSheet2);
}