Skip to content Skip to sidebar Skip to footer

Google Sheet Script Apparently Not Returning A Number

I'm writing my first custom function in Google Script. All works well in debug, my code runs (apparently) perfectly. There's a function already written (CONVERT_RACETIME_TO_SECOND

Solution 1:

Javascript and Google app-script does not provide a way to 'force' a return type to number for a custom function.

This answer gets close but return type is an array, not a number.

The answer for this question is correct but no examples for how to coerce the correct type for the return value.

This link has examples of converting Javascript strings into numbers and lists some of the pitfalls.

In order to 'force' the return to a number, add return Number(ratio); to the function:

  function SLOWEST_RATIO(fast, slow) {
      if (!(fast instanceof Array) || !(slow instanceof Array) ) {
      throw 'Invalid: range input required';
    }

    var fastest = Math.min.apply(null, fast.map(CONVERT_RACETIME_TO_SECONDS));
    var slowest = Math.max.apply(null, slow.map(CONVERT_RACETIME_TO_SECONDS));
    var ratio = slowest/fastest;

    var ratiotype = typeof ratio; 

    if (!(ratiotype == 'number')) {
      throw 'Invalid: ratiotype is ' + ratiotype;
    }

    return Number(ratio); 
  }

See my question & answer for a similar issue.


Post a Comment for "Google Sheet Script Apparently Not Returning A Number"