您可以通过添加一些这样的案例处理来使错误消息更加明确:
function in2mm(inNum) {
// Function to convert from INCHES to MILLIMETERS
var outNum = 0; // this will hold the answer
var factor = 25.4; // multiply input by this factor to get output
if (inNum == "") { // check to make sure input is a number
throw ("error: input must not be empty"); // throw an exception with the error message
}else if (typeof inNum != "number") { // check to make sure input is a number
throw ("error: input must be a number (now it is a "+typeof inNum+")"); // throw an exception with the error message
}
outNum = inNum * factor; // calculate the answer
return outNum; // return the answer to the cell which has the formula
}
但是该参数必须是“有效”才能由函数处理...在您的示例中,=in2mm(a)
a 被解释为命名区域,并且由于您没有名为“a”的区域,因此在尝试执行之前会出错功能。这就是为什么错误消息不是来自函数本身而是来自电子表格下的“引擎”的原因。
另一个示例=in2mm(*)
出于同样的原因返回 Parse 错误,参数无效,在这种情况下它也不能是一个范围......你也可以尝试 + 或 - ,它会尝试计算一些东西,但它不能并且错误消息再次来自电子表格,而不是来自您的函数。
尝试使用有效范围并更改目标单元格中的值,您将看到完全不同的结果。例如在A2
写=in2mm(A1)
和玩A1
希望我让事情变得(一点)更清楚;-)