Custom Google Sheets function: Issues with logarithm implementation

Help needed with my Google Sheets custom function!

I’m trying to create a function to find the midpoint of a lot, but I’m running into an error. Here’s what I’m seeing:

#ERROR! TypeError: Math.Log is not a function (Line 11)

My code looks like this:

function FindLotCenter(Begin, Finish, Sum, Gradient) {
  let base = 2;
  let exponent = 1 + (Math.log(Gradient) / Math.log(base));
  let term1 = (Finish + 0.5) ** exponent;
  let term2 = (Begin - 0.5) ** exponent;
  let factor = 1 / (1 + (Math.log(Gradient) / Math.log(base)));
  
  return (factor * (term1 - term2) / Sum) ** (1 / (Math.log(Gradient) / Math.log(base)));
}

What am I doing wrong here? I thought Math.log() was a valid JavaScript function. Any ideas on how to fix this?

Hey there, I’ve actually run into a similar issue before when working with custom functions in Google Sheets. The problem isn’t with Math.log itself, but rather how Google Sheets handles JavaScript functions.

In my experience, Google Sheets can be a bit finicky with certain Math methods. What worked for me was explicitly defining the log function at the beginning of my custom function. Something like this:

function FindLotCenter(Begin, Finish, Sum, Gradient) {
  const log = Math.log;
  // rest of your function...
}

This seemed to solve the ‘not a function’ error for me. Also, double-check that all your input parameters are numbers and not strings. Google Sheets sometimes passes values as strings, which can cause unexpected behavior with math operations.

Hope this helps! Let me know if you need any more clarification.

The issue you’re encountering is likely due to case sensitivity in JavaScript. Instead of Math.Log, you should use Math.log (with a lowercase ‘l’). JavaScript’s built-in Math object uses lowercase for its methods. Modify your code to use Math.log consistently throughout the function. This should resolve the TypeError you are seeing. Additionally, note that Math.log provides the natural logarithm (base e), while your calculations involve base 2 logarithms. You might simplify your code by using Math.log2. Finally, ensure the function is called with the correct arguments.

yo, i had this problem too! its cuz google sheets is weird with javascript. try using Math.log10() instead of Math.log(). it worked for me. also, make sure ur inputs are numbers not strings. google sheets can mess that up sometimes. good luck!