You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

谷歌表格自定义函数中Math.Log无法运行的问题求助

解决Google Sheets自定义函数Midpoint的Math.Log错误
  • JavaScript是区分大小写的语言,Math对象的自然对数方法是**Math.log()**(小写log),你代码里写的Math.Log()(大写L)是不存在的,这就是触发TypeError: Math.Log is not a function的直接原因。
  • 函数定义的参数是Start和End,但计算逻辑里误用了First和Last,这会导致变量未定义的错误,需要统一参数名称。

修正后的完整代码:

/**
 * Find Lot Midpoint value. (Start, End, Total, Slope)
 * @param {number} Start The Start QTY of lot.
 * @param {number} End The Finish QTY of lot.
 * @param {number} Total The Total QTY of lot.
 * @param {number} Slope The Slope of the Curve.
 * @return The calculated cost change.
 * @customfunction 
 */
function Midpoint(Start, End, Total, Slope) {
  const logSlope = Math.log(Slope);
  const log2 = Math.log(2);
  const exponent = 1 + (logSlope / log2);
  return (((1 / exponent) * ((End + 0.5) ** exponent - (Start - 0.5) ** exponent)) / Total) ** (1 / (logSlope / log2));
}

另外,把重复计算的Math.log(Slope)/Math.log(2)提取成变量,既提升可读性也避免重复计算。

内容的提问来源于stack exchange,提问作者Shawn Gilmore

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 21:42:06