[英]Google Sheets Javascript indexOf error
I'm have a crazy time trying to get around an error when using indexOf()
on a cell without the index criteria.. in this case a comma: indexOf(",") . 在没有索引条件的单元格上使用
indexOf()
时,我度过了疯狂的时间来尝试解决错误。在这种情况下,逗号是:indexOf(“,”)。 BTW, I get the same issue when trying to do a .split(",") on a cell with no commas. 顺便说一句,当我尝试在没有逗号的单元格上执行.split(“,”)时,我遇到了同样的问题。
Is there a way around this so if there are no commas, I can give a variable a value? 有没有办法解决这个问题,如果没有逗号,我可以给变量赋值吗?
Here's the code: 这是代码:
/**
@customfunction
*/
function INDEX_TEST(input){
var a = 0;
if (input.indexOf(",") > -1){
a = 999;
}
else {
a = 0;
}
return a;
}
Any help would be appreciated. 任何帮助,将不胜感激。
Here's a link to a google sheet 这是指向Google工作表的链接
You get the error when input
is a number not a string. 当
input
是数字而不是字符串时,会出现错误。 Numbers do not have indexOf
or any other string methods. 数字没有
indexOf
或任何其他字符串方法。 So you have to convert it to string: 因此,您必须将其转换为字符串:
if (input.toString().indexOf(",")>-1){a = 999;}
or 要么
if ((input + '').indexOf(",")>-1){a = 999;}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.