Search code examples
google-apps-scripttypessumconcatenation

Apps Script summing up Query Data (original data is number) results in concatenation


I came across the following issue when I am trying to work on Query data (data resulting from query function in Google Sheet) using Apps Script:

  • Query funtion used: =Query('data source', "select ...sum(..)... where ... group by... pivot...")
  • Script used:
  var summe = 0;
  for (i=0;i<data.length;i++) {
    for (k=0;k<data[i].length;k++){
      summe = summe + data[i][k];
      console.log(...); # to track exactly which column's value becomes text 
      console.log(summe);
    }

The results in execution log show that from a certain point it starts to concate text instead of summing up values: enter image description here

Appreciate a lot in advance for any help or ideas out there!!!

Best regards, Xubo


Solution

  • You can avoid this problem by manually converting your data to numbers:

    summe = summe + Number(data[i][k]);
    

    See Number.