indexOf 在 Google Apps 脚本中不起作用
indexOf not working in Google Apps Script
我正在制作一份 Google 表格文档,用于分析我的银行交易历史记录。我的许多交易描述都以相同的字母开头,特别是 "SIG"(正确的大小写)。我想统计这些交易的数量,但是我不能。
为了进行故障排除,我已确保 indexOf 在我只检查一个 cell/input 时有效。它 returns 在单元格中找不到 "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);
}
日志:
[18-01-28 15:30:54:630 PST] Funds Added (Donation)
[18-01-28 15:30:54:630 PST] -1.0
[18-01-28 15:30:54:631 PST] SIG POS purchase at Paypal
[18-01-28 15:30:54:631 PST] 0.0
[18-01-28 15:30:54:632 PST] PIN POS purchase cashback
[18-01-28 15:30:54:632 PST] -1.0
[18-01-28 15:30:54:632 PST] PIN POS purchase cashback
[18-01-28 15:30:54:633 PST] -1.0
[18-01-28 15:30:54:633 PST] SIG POS purchase at Paypal
[18-01-28 15:30:54:634 PST] 0.0
当列(例如:B1:B22)是自定义函数的输入时,范围将转换为二维数组并传递给函数。如果您输入一行(Ex B9:F9),它将被转换为一维数组。
在这种情况下,您将一列传递给函数,因此它被转换为二维数组。所以相应的示例数组应该如下所示:
var sample_array = [["Funds Added (Donation)"],
["SIG POS purchase at Paypal"],
["PIN POS purchase cashback"],
["PIN POS purchase cashback"],
["SIG POS purchase at Paypal"]]
您需要像这样提供第二个维度的索引descriptions[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;
}
请按照此 documentation 进一步参考。
最后注意:以上功能只有在选中一列或单个单元格时才有效。如果选择了一行,就会报错!
编辑:等效地,您可以使用这个内置函数来做同样的事情
=Arrayformula(sum(iferror(find("SIG",B1:B200),0)))
我正在制作一份 Google 表格文档,用于分析我的银行交易历史记录。我的许多交易描述都以相同的字母开头,特别是 "SIG"(正确的大小写)。我想统计这些交易的数量,但是我不能。
为了进行故障排除,我已确保 indexOf 在我只检查一个 cell/input 时有效。它 returns 在单元格中找不到 "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);
}
日志:
[18-01-28 15:30:54:630 PST] Funds Added (Donation)
[18-01-28 15:30:54:630 PST] -1.0
[18-01-28 15:30:54:631 PST] SIG POS purchase at Paypal
[18-01-28 15:30:54:631 PST] 0.0
[18-01-28 15:30:54:632 PST] PIN POS purchase cashback
[18-01-28 15:30:54:632 PST] -1.0
[18-01-28 15:30:54:632 PST] PIN POS purchase cashback
[18-01-28 15:30:54:633 PST] -1.0
[18-01-28 15:30:54:633 PST] SIG POS purchase at Paypal
[18-01-28 15:30:54:634 PST] 0.0
当列(例如:B1:B22)是自定义函数的输入时,范围将转换为二维数组并传递给函数。如果您输入一行(Ex B9:F9),它将被转换为一维数组。 在这种情况下,您将一列传递给函数,因此它被转换为二维数组。所以相应的示例数组应该如下所示:
var sample_array = [["Funds Added (Donation)"],
["SIG POS purchase at Paypal"],
["PIN POS purchase cashback"],
["PIN POS purchase cashback"],
["SIG POS purchase at Paypal"]]
您需要像这样提供第二个维度的索引descriptions[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;
}
请按照此 documentation 进一步参考。
最后注意:以上功能只有在选中一列或单个单元格时才有效。如果选择了一行,就会报错!
编辑:等效地,您可以使用这个内置函数来做同样的事情
=Arrayformula(sum(iferror(find("SIG",B1:B200),0)))