parseInt return NaN

60 views
Skip to first unread message

Edward Ulle

unread,
Oct 10, 2021, 11:40:08 AM10/10/21
to Google Apps Script Community
I have data in a spreadsheet that looks like this:

Category;"Energy"

01;22.696
02;20.565
03;21.983
04;21.606
05;21.716
06;20.026
07;19.975
08;18.934
09;16.098
10;22.229
11;21.008


I loop through the data and split each string into an integer and float.

    var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ScratchPad");
    var data = sh.getDataRange().getValues();
    temp = data[0][0];
    data[0] = temp.split(";");
    for( var i=1; i<data.length; i++ ) {
      temp = data[i][0];
      temp = temp.split(";");
      Logger.log(temp[0]);
      Logger.log(parseInt(temp[0]));
      data[i][0] = new Date(year,month,parseInt(temp[0]));
      data[i].push(parseFloat(temp[1]));
    }
    sh.getRange(1,1,data.length,2).setValues(data);

The first part is the day of the month.  But when I parseInt 08 and 09 return NaN.  All other numbers work properly.  Ideas?

Clark Lind

unread,
Oct 10, 2021, 2:30:23 PM10/10/21
to Google Apps Script Community
First, if there is a blank row between the headers and data, remove it, otherwise using a null value will cause it to error out. temp isn't declared anywhere, so I declared it.
Also, unless you have year and month declared as variables elsewhere, that date won't work. This worked for me once I handle those two things. Create the date separately:

function dataProblem() { //<--delete this line
var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ScratchPad");
    var data = sh.getDataRange().getValues();
    var temp = data[0][0];  //added 'var'
    data[0] = temp.split(";");

    for( var i=1; i<data.length; i++ ) {
      temp = data[i][0];
      temp = temp.split(";");
      Logger.log(temp[0]);
      Logger.log(parseInt(temp[0]));
      var date = `2021-10-${ parseInt(temp[0]) }`; //you will have to provide accurate year/month variables
      data[i][0] = new Date(date);
      data[i].push(parseFloat(temp[1]));
    }
    sh.getRange(1,1,data.length,2).setValues(data);
} //<-- delete this

Alan Wells

unread,
Oct 10, 2021, 6:33:14 PM10/10/21
to Google Apps Script Community
JavaScript has some really strange bugs and I'm wondering if that's what it is.
It looks like this issue has been around for a couple of decades.
I don't know if it's actually a bug or expected behavior.
In any case, you should use something else to convert to an integer.
Maybe:
Number("08")

Edward Ulle

unread,
Oct 11, 2021, 11:11:26 AM10/11/21
to Google Apps Script Community
Thanks for pointing me to the old bug report.  I thought about "08" and "09" being interpreted as invalid octal numbers but if you look at the https://www.w3schools.com/jsref/jsref_parseint.asp example "010" is interpreted as 10 not octal 8.  I guess i'll just have to figure a work around.

Edward Ulle

unread,
Oct 11, 2021, 11:15:01 AM10/11/21
to Google Apps Script Community
parsint(temp[0],10) works
Reply all
Reply to author
Forward
0 new messages