gpt4 book ai didi

javascript - 谷歌表格脚本 : Passing function as a parameter

转载 作者:行者123 更新时间:2023-11-30 15:55:57 25 4
gpt4 key购买 nike

我在 Google 表格中制作了一个脚本:

/**
* @param {array} input A row or a column.
* @param {function} condition A function returning bool.
* @return The last value in the input satisfying the condition.
* @customfunction
*/
function GetLastGoodValIn1DArray(input, condition) {
// realization ...
}

当我尝试使用下一个参数调用此函数时:

=GetLastGoodValIn1DArray(D11:H11;ISNUMBER)

我遇到下一个错误:

Error

TypeError: #NAME? is not a function, but a string. (line 26).

显然 'ISNUMBER' 被解释为一个字符串。但是如何将它作为函数传递呢?

最佳答案

根据此old google docs forum,不可能从脚本访问内部谷歌表格功能还有这个Stack Overflow question .

请注意,即使是 google 表格内部函数也不能将函数作为参数。例如内部函数SUBTOTAL ,而不是直接采用函数,而是采用与所需函数相对应的数字代码。

我能想到的最好的方法是构建您自己的 WorksheetFunction 对象,就像 excel 为 vba 所做的那样。这可能是一个接受字符串并返回您编写的函数的对象映射。例如:

var worksheetFunction = {
isnumber: function (x) {
return !isNaN(x);
}
};

Another Note: ISNUMBER isn't passed as a string to your custom function. Google sheets first calculates it as an error and then passes the string #NAME? to your custom function. You can see this by using the formula =type(ISNUMBER), which returns 16 (the code for an error) also in your custom function condition === "#NAME?" will evaluate to true.

关于javascript - 谷歌表格脚本 : Passing function as a parameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38544059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com