0

I have an error while coding in google sheets.

It doesn't want to multiply the numbers, just displays them as floats without the suffix. This is my code:

    function nordicImport(a){
    var b = a;
    var c = b.substring(0,b.length-1);

if(b.contains == "M"){
    var c = parseFloat(b);
    var d = c * 1000000;
    return d;
}else if(b.contains == "K"){
    var c = parseFloat(b);
    var d = c * 1000;
    return d;
}else{
    var c = parseFloat(b);
    return c;
}

}
  • How do you think you should call the `String#contains` method? Usually methods are called with arguments. Also note: https://stackoverflow.com/q/1789945/9337071 – tehhowch Jul 10 '18 at 11:45

2 Answers2

0

I rearranged your code, since I think it is a little bit confusing.

Try this:

function nordicImport(a){

  var b = a.substring(0, a.length - 1) // This is the number
  var c = a.substring(a.length - 1)    // This is the letter
  
  if (c == "M"){
    Logger.log(b * 1000000)
    return b * 1000000
  }
  
  else if (c == "k"){
    Logger.log(b * 1000)
    return b * 1000
  }
}

When you use an if/elseif statement you must use "==" or "===" instead of "=" ("=" is an assignement!).

0

There is no contains method of string in GAS (that I know of). Also I don't see a need to copy b to a since a is a copy of the value passed as an argument to the function and can be used directly. Also I'm sure there are other mistakes that can be made inputing values but this gives you an idea.

function nordicImport(a){
  if( typeof a === "number" ) {
    return a;
  }
  else if( typeof a === "string" ) {
    if( a.lastIndexOf("M") === a.length-1 ) {
      return parseFloat(a.substring(0,a.length-1))*1000000;
    }
    else if( a.lastIndexOf("K") === a.length-1 ) {
      return parseFloat(a.substring(0,a.length-1))*1000;
    }
    else {
      return "unknown multiple";
    }
  }
  else {
    return "unknown type";
  }
}
TheWizEd
  • 7,517
  • 2
  • 11
  • 19