自定义函数不适用于ArrayFormula [英] Custom function doesn't work with ArrayFormula
问题描述
我关注了帖子
https://developers.google .com / apps-script / guides / sheets / functions
函数DOUBLE(input){
返回输入* 2;
}
并将非常简单的DOUBLE函数复制到我的应用程序脚本中。
= double(A1)
正在工作,并在'A1'中加倍我的整数
但是,当我填充列A的值,如
A1 = 2
A2 = 3
A3 = 4
....
在B1单元格中,我运行
= arrayformula(double(A1:A))
它返回错误结果不是数字,#NUM!
我不确定哪里出了问题。任何应用程序脚本大师都可以提供帮助吗?
谢谢
关于这个答案?
- 例如,当使用
= DOUBLE(A1)
时,DOUBLE(input)
的input
的值被检索为一个数字或一个字符串(在你的情况下,它是
- 例如,当使用
= DOUBLE(A1:B1)
时,<$ c $
$ b在确认
input
是数组后,需要计算它。上面反映的修改如下。
From:
return input * 2;
收件人:
return Array.isArray(input)? input.map(function(e){return e.map(function(f){return f * 2})}):input * 2;
注意:
示例使用if和for循环编写,如下所示。
if(Array.isArray(input) ){
var result = [];
for(var i = 0; i< input.length; i ++){
var temp = []; (var j = 0; j< input [i] .length; j ++){
temp.push(input [i] [j] * 2)的
;
}
result.push(temp);
}
返回结果;
} else {
return input * 2;
}
如果我误解了您的问题,我很抱歉。
i followed the post https://developers.google.com/apps-script/guides/sheets/functions
function DOUBLE(input) { return input * 2; }
and copied the very simple DOUBLE function into my app-script.
=double(A1)
is working and double my integer in 'A1' But when i filled column A with values, like
A1 = 2 A2 = 3 A3 = 4 ....
And in B1 cell, i run
=arrayformula(double(A1:A))
it returned error "Result was not a number", #NUM!
I am not sure what goes wrong. Could any app-script gurus helps?
Thanks
解决方案How about this answer?
- For example, when
=DOUBLE(A1)
is used, the value ofinput
ofDOUBLE(input)
is retrieved as a number or a string (in your case, it's a number.). - For example, when
=DOUBLE(A1:B1)
is used, the values ofinput
ofDOUBLE(input)
are retrieved as 2 dimensional array.
It is required to calculate after it confirmed whether
input
is array. The modification which reflected above is as follows.From :
return input * 2;
To :
return Array.isArray(input) ? input.map(function(e){return e.map(function(f){return f * 2})}) : input * 2;
Note :
When the above modified sample is written using "if" and "for loop", it becomes as follows.
if (Array.isArray(input)) { var result = []; for (var i = 0; i < input.length; i++) { var temp = []; for (var j = 0; j < input[i].length; j++) { temp.push(input[i][j] * 2); } result.push(temp); } return result; } else { return input * 2; }
If I misunderstand your question, I'm sorry.
这篇关于自定义函数不适用于ArrayFormula的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- For example, when