Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I make these strings into decimals using map (GAS)?

I’m trying to make sure that the resulting column will onlu contain numbers, but it keeps coming as string.

function moveColsWesbanco() {
  var ss = SpreadsheetApp.getActive();
  var sourceSheet = ss.getSheetByName('Sheet1');
  var destSheet = ss.getSheetByName('New Entry');
  var debitsCredits = sourceSheet.getRange('A7:D');
  var values = debitsCredits.getValues();
  let amounts2 = values.filter(o => o[0] != '').map(e => e[2] != '' ? e[3] = Number.parseFloat(e[2]) : Number.parseFloat(e[3]));
  let allTypes = [];
  for (val in amounts2) {
    allTypes.push(typeof val)
  };
  Logger.log('Types within it: ' + allTypes)
  return;
}

Here are the logs:
enter image description here

So…I wonder what I’m missing here. Thank you!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I think that when "for in" is used like for (val in amounts2) {}, val is the index. And, it’s the string type. I think that this is the reason of your issue. In this case, how about using "for of" as follows?

Modified script:

for (var val of amounts2) {
  allTypes.push(typeof val)
};

References:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading