1

我在谷歌片制成的脚本:谷歌表脚本:传递函数作为参数

/** 
* @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'被解释为一个字符串。但如何将它作为函数传递?

+0

的公式不正确写入。尝试'= GetLastGoodValIn1DArray(D11:H11; ISNUMBER(something))',其中某些东西可能是值或单元格引用。 –

回答

2

根据此old google docs forumStack Overflow question,无法从脚本访问内部谷歌床单功能。

请注意,即使谷歌床单内部功能不能把功能作为参数。例如,内部函数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.