谷歌表格自定义函数中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
相关产品推荐
相关产品推荐

