[英]Google Sheets Script: Passing function as a parameter
I've made a script in Google Sheets: 我在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 ...
}
When I'm trying to call this function with the next arguments: 当我尝试使用下一个参数调用此函数时:
=GetLastGoodValIn1DArray(D11:H11;ISNUMBER)
I'm getting the next error: 我收到下一个错误:
Error
错误
TypeError: #NAME?
TypeError:#NAME? is not a function, but a string.
不是函数,而是字符串。 (line 26).
(第26行)。
Obviously 'ISNUMBER' interpreted as a string. 显然,“ ISNUMBER”被解释为字符串。 But how to pass it as a function?
但是如何将其作为函数传递呢?
It's not is possible to access internal google sheets functions from a script according to this old google docs forum and this Stack Overflow question . 根据这个旧的google docs论坛以及此Stack Overflow问题,无法从脚本访问内部google sheet函数。
Note that even google sheets internal functions can't take functions as parameters. 请注意,即使Google Sheets内部函数也不能将函数作为参数。 For example, the internal function SUBTOTAL , instead of taking the function directly, takes in a numerical code that corresponds to the desired function.
例如,内部函数SUBTOTAL而不是直接采用该函数,而是采用与所需函数相对应的数字代码。
The best I can think of is to build your own WorksheetFunction object like excel has for vba. 我能想到的最好的方法是像excel一样为vba构建自己的WorksheetFunction对象。 That could be an object map that takes in an string and returns a function you wrote.
那可能是一个对象映射,它接受一个字符串并返回您编写的函数。 For example:
例如:
var worksheetFunction = {
isnumber: function (x) {
return !isNaN(x);
}
};
Another Note :
ISNUMBER
isn't passed as a string to your custom function.另一个注意事项 :
ISNUMBER
不会作为字符串传递给您的自定义函数。 Google sheets first calculates it as an error and then passes the string#NAME?
Google表格首先将其计算为错误,然后传递字符串
#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 functioncondition === "#NAME?"
您可以通过使用公式
=type(ISNUMBER)
看到此结果,该公式在您的自定义函数condition === "#NAME?"
也返回16(错误代码)condition === "#NAME?"
will evaluate to true.将评估为true。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.