0

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

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

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);
}
SMAKSS
  • 9,606
  • 3
  • 19
  • 34
Namzlas
  • 1
  • 1
  • Does this answer your question? [How to find the sum of an array of numbers](https://stackoverflow.com/questions/1230233/how-to-find-the-sum-of-an-array-of-numbers) – TheMaster Apr 01 '20 at 17:56

1 Answers1

0

You can achieve this with this simple function. The idea is to parse the cell containing the numbers and reduce its value to their sum. I'll use two for loops to keep track of the row and column indexes and do the in-place replacement.

function reduceRowsValues() {
  let rows = SpreadsheetApp.getActiveSheet().getDataRange().getValues(); //Gets rows containing values
  for (let i=0; i<rows.length; i++) {
    for (let j=0; j<rows[i].length; j++) {
      if (rows[i][j] != "") {
        let reducedValue = rows[i][j].toString().split(",").reduce((a,b)=> a + parseInt(b), 0); //assuming you will use integers
        SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(i+1, j+1).setValue(reducedValue);
      }
    }
  }
}

Feel free to change the parseInt function to whatever function that better fits to your data type. As well as you can change the delimiter "," in the split function if you will use another symbol to represent your lists.

References:

setValue

JS reduce

Alessandro
  • 2,848
  • 1
  • 8
  • 16