Javascript indexOf在Google应用程序脚本中不起作用
我正在制作一个谷歌单据,分析我的银行交易历史。我的许多事务描述都以相同的字母开头,特别是“SIG”(正确的大小写)。我想数一数这些交易的数量,但我不能 对于故障排除,我已确保indexOf在只检查一个单元格/输入时工作。在单元格中找不到“SIG”时返回“-1”,在单元格开头找到“SIG”时返回“0” 再次排除故障,我还确保正确循环通过一个数组(多个单元格),该数组只计算非空单元格的数量。这也行得通 当我试图把所有的东西都放在一起的时候,我无法让它工作,我也不知道为什么。下面是简短的函数。谢谢你的帮助Javascript indexOf在Google应用程序脚本中不起作用,javascript,arrays,google-apps-script,google-sheets,indexof,Javascript,Arrays,Google Apps Script,Google Sheets,Indexof,我正在制作一个谷歌单据,分析我的银行交易历史。我的许多事务描述都以相同的字母开头,特别是“SIG”(正确的大小写)。我想数一数这些交易的数量,但我不能 对于故障排除,我已确保indexOf在只检查一个单元格/输入时工作。在单元格中找不到“SIG”时返回“-1”,在单元格开头找到“SIG”时返回“0” 再次排除故障,我还确保正确循环通过一个数组(多个单元格),该数组只计算非空单元格的数量。这也行得通 当我试图把所有的东西都放在一起的时候,我无法让它工作,我也不知道为什么。下面是简短的函数。谢谢你的
function SIG_counter (descriptions) {
var SIG_total = 0;
var SIG_checker;
for (var i=0; i<descriptions.length; i++) {
var SIG_checker = descriptions[i].indexOf("SIG");
Logger.log(descriptions[i]);
Logger.log(SIG_checker);
if (SIG_checker == 0.0) {
SIG_total++;
}
}
return SIG_total;
}
var sample_array = ["Funds Added (Donation)",
"SIG POS purchase at Paypal",
"PIN POS purchase cashback",
"PIN POS purchase cashback",
"SIG POS purchase at Paypal"]
function trouble_shooter () {
SIG_counter(sample_array);
}
当列(例如:B1:B22)是自定义函数的输入时,范围将转换为2D数组并传递给函数。如果您输入一行(例如B9:F9),它将转换为1D数组。
在本例中,将一列传递到函数中,从而将其转换为二维数组。因此,Coresponding示例数组应如下所示:
var sample_array = [["Funds Added (Donation)"],
["SIG POS purchase at Paypal"],
["PIN POS purchase cashback"],
["PIN POS purchase cashback"],
["SIG POS purchase at Paypal"]]
您需要为第二个维度提供索引,如sodescriptions[i][0]
,其中[i]
表示第一个维度,[0]
表示第二个维度。
因此,对于示例数组
sample_array[0][0] = "Funds Added (Donation)"
sample_array[1][0] = "SIG POS purchase at Paypal"
等等
修改后的函数如下所示:
function SIG_counter (descriptions) {
var SIG_total = 0;
var SIG_checker="";
if(descriptions.map){ //Check if the arugment is array
//This code assumess it is always a 2D or a single value
for (var i=0; i<descriptions.length; i++) {
SIG_checker = descriptions[i][0].indexOf("SIG");
Logger.log(descriptions[i][0]);
Logger.log(SIG_checker);
if (SIG_checker == 0.0) {
SIG_total++;
}
} } else { //if arugment is not array, it refers to a single cell
if(descriptions.indexOf("SIG") == 0.0){
SIG_total = 1
}
}
return SIG_total;
}
您是否可以对一个数组进行硬编码,而不公开您的个人信息,并使用硬编码数组更新您的问题代码看起来应该可以工作,尽管您应该编写
==0
,因为indexOf()
返回一个数字,而不是字符串。如果您这样做console.log(descriptions[i],SIG_checker)
你看到了什么?我猜描述是一个二维数组,而描述[i]指的是一个一维数组。因此,descriptions[i].indexOf(“SIG”)
正在调用indexOf。哪个需要完善match@user6069863:我已经包括了一个示例数组。非常感谢!自定义代码和内置函数都起到了很好的作用。谢谢大家的帮助。
function SIG_counter (descriptions) {
var SIG_total = 0;
var SIG_checker="";
if(descriptions.map){ //Check if the arugment is array
//This code assumess it is always a 2D or a single value
for (var i=0; i<descriptions.length; i++) {
SIG_checker = descriptions[i][0].indexOf("SIG");
Logger.log(descriptions[i][0]);
Logger.log(SIG_checker);
if (SIG_checker == 0.0) {
SIG_total++;
}
} } else { //if arugment is not array, it refers to a single cell
if(descriptions.indexOf("SIG") == 0.0){
SIG_total = 1
}
}
return SIG_total;
}
=Arrayformula(sum(iferror(find("SIG",B1:B200),0)))