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

Time:04-28

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!

CodePudding user response:

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:

  • Related