Javacript issue.. concatenation instead of sum

I use the below simple code (partial code pasted here) to find a sum from a column in a different table. I am using EE 2.6.2

const rows1a = base.getRows(table1, view1);
rows1a.map((row, rowIndex, rows) => {
    const row3a = base.getRowById(table3, 'INxzVnB_T_6Z6Kp8vdFRtg');
    // calculate increment
    const currentNumber = row[column1a.name];
   
    if (!currentNumber) return;
    const previousRow = rows1a[rowIndex - 1];
    
    //if there is no previousRow, set previousNumber to 0
    const previousNumber = previousRow ? previousRow[column1a.name] : 0;
    
    const increaseCount = currentNumber + previousNumber;
      
    // set calculated value to row
    base.modifyRow(table3, row3a, {[column3a.name]: increaseCount}); }

The issue I see is that increaseCount concatenates currentNumber and previousNumber instead of calculating the sum. I have tried removing the currency format and comma separator on the columns and also using parseFloat and none of these worked.

Anything I am missing here?

I think your currentNumber is in text form here, as it’s the name of a row, is that correct?

currentNumber is the number value in that row under the column 1a. If I get the .name removed from [column1a.name], I get NaN in the result. Sample screenshot below (mm is the month number)…

I see…I wasn’t looking closely enough!

I have to say that the column values I am adding are of link formula type. Are there any limitations to doing JS if the columns values are from link formula or formula columns? I ask this because in a table where the columns values to add are not formula or link formula columns, the above JS works fine.

It shouldn’t be relevant.
Let’s say your records are stored in the rows array, what do you see as value of the link formula column when you type output.text(rows) ?

In this following example, the column LF is a link formula column

I am attaching the screenshots of a sample table to explain the issue… total1 displays correctly while total2 get a concatenated result.

Hi @jungfrau,
The problem effectively comes from the “type” of your curr3 variable : it’s a string ! The best way to verify the type of your data (in my opinion) is by using the typeof() function (see beelow in my script).

When you try to sum two objects, one being a string, the other is considered as a string as well, and they are concatenated instead of summed… To correct the problem, you can use parseFloat() (to get a float) or parseInt() (to get an integer) function depending of the type of data you are expecting.

Here is a simple script, very similar to yours, allowing you to see the problem and the solution :

const table = base.getTableByName('Table1');
const viewName = 'Default View';
const view = base.getViewByName(table, viewName);
const val1Col = base.getColumnByName(table, 'val1');
const val2Col = base.getColumnByName(table, 'val2');
const sumCol = base.getColumnByName(table, 'sum');

const rows = base.getRows(table, view);
rows.forEach((row) => {
  const curr1 = row[val1Col.name];
  output.text("curr1 :" + curr1.toString() + " type : " + typeof(curr1));
  const curr3 = row[sumCol.name];
  output.text("curr3 :" + curr3.toString() + " type : " + typeof(curr3));
  const total2 = curr1 + curr3;
  output.text("javscript calculated (wrong) sum : " + total2.toString())
  const total3 = curr1 + parseFloat(curr3);
  output.text("javscript calculated (correct) sum : " + total3.toString())
});

Hope this helps…
Bests,

Benjamin

3 Likes

Oops! Yes, it does! I was applying parseFloat as mentioned in my earlier post above, but to the wrong variables I suppose. Thank you for the suggestion. I am sure it will work for the link formula columns as well.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.