This is my first question here, so please pardon any posting mistakes.
I used this set up: Build Google App Script to Combine Similar Rows into One Row
to combine data with a similar row entry by column 1 and the combination resulted in an array
The image just shows the cells where the array has formed
While this was a great start for me, I would rather have those numbers in the array be added together into one value such as the image below:
This image is a new version of the first photo where the array data is summed
A written example: [14,15] to [29]
Below I'll put the script I used thanks to a user named Cooper
Thanks a lot in advanced!!
function rowMerge() {
var firstRow = 2;
var firstCol = 1;
var sht = SpreadsheetApp.getActiveSheet();
sht.getRange(firstRow, firstCol, sht.getLastRow() - firstRow + 1, sht.getLastColumn() - firstCol + 1).sort(1);
sht.appendRow(['***','***','Control-z one more time','***','***']); //need a throwaway row to get last data element out since I'm moving out element[i-1]
var datR = sht.getDataRange();
var lastRow = datR.getLastRow();
var lastCol = datR.getLastColumn();
var datA = datR.getValues();
sht.getRange(2,1,lastRow - firstRow + 1,lastCol - firstCol + 1).clearContent().setWrap(true);
var datoutA = [];
var k=1;
var n = 0;
for(var i = 0;i < datA.length; i++)
{
if(i > 1)
{
if(datA[i][0] == datA[i-1][0])
{
k++; //k is the number of consecutive matching values
}
else
{
datoutA[n] = [];
if(k == 1)
{
// if k = 1 the datA[i-1] row gets copied into output array
for(var c = 0;c < datA[i-1].length; c++)
{
datoutA[n][c]=datA[i-1][c];
}
}
else
{
//i-1 to i-k rows get merged and copied into output array
var firstTime = true;
for(var a = 1;a <= k;a++)//input rows
{
for(var b = 0;b < datA[i].length -1;b++)//input columns
{
if(a > 1 || b > 0) //no delimiter for first row or first column
{
datoutA[n][b] += ', ';
}
if(firstTime || b == 0)// straight assignment for first row and running sum after that same with first column because we only want one of them because they're all the same.
{
datoutA[n][b] = datA[i - a][b];
}
else
{
datoutA[n][b] += datA[i - a][b];
}
}
if(firstTime)//first assignment then running sums for last column
{
datoutA[n][datA[i].length - 1] = Number(datA[i - a][datA[i].length-1]);
}
else
{
datoutA[n][datA[i].length - 1] += Number(datA[i - a][datA[i].length-1]);
}
firstTime=false;
}
var end = 'is near';
}
k=1; //consecutive counter
n++; //datoutA index
}
}
}
var datoutR = sht.getRange(2, 1, datoutA.length , datoutA[0].length);
datoutR.setValues(datoutA);
var colwidth = 250;
sht.setColumnWidth(2, colwidth);
sht.setColumnWidth(3, colwidth);
sht.setColumnWidth(4, colwidth);
}