简体   繁体   English

indexOf无法在Google Apps脚本中使用

[英]indexOf not working in Google Apps Script

I'm making a Google Sheets doc which analyzes my bank transaction history. 我正在制作一个Google表格文档,用于分析我的银行交易记录。 A number of my transaction descriptions begin with the same letters, specifically "SIG" (correct casing). 我的许多交易说明都以相同的字母开头,尤其是“ SIG”(正确的大小写)。 I want to count the number of these transactions, but I can't. 我想计算这些交易的数量,但不能。

For troubleshooting, I've ensured that indexOf works when I'm only checking one cell/input. 为了进行故障排除,我确保仅检查一个单元格/输入时indexOf可以工作。 It returns "-1" when it can't find "SIG" in the cell and "0" when it does find "SIG" at the beginning of the cell. 如果在单元格中找不到“ SIG”,则返回“ -1”;如果在单元格的开头找到“ SIG”,则返回“ 0”。

Again troubleshooting, I've also ensured that I'm correctly looping through an array (multiple cells) which just counts the number of non-null cells. 再次进行故障排除,我还确保我正确地循环了一个数组(多个单元格),该数组仅计算非空单元格的数量。 That also works. 那也行。

When I try to put everything together tho, I can't get it to work, and I don't know why. 当我尝试将所有内容放在一起时,我无法使其正常工作,我也不知道为什么。 The short function is below. 简短功能如下。 Thanks for the help. 谢谢您的帮助。

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);
}

Log: 日志:

[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

When a column(Ex: B1:B22) is an input to the custom functions, the range is converted into a 2D array and passed to the function. 当列(Ex:B1:B22)作为自定义函数的输入时,范围将转换为2D数组并传递给该函数。 In case you input a row(Ex B9:F9), it is converted into 1D array. 如果您输入一行(例如B9:F9),则会将其转换为一维数组。 In this case, your passing a column into the function and hence it is converted into a 2D array. 在这种情况下,您将一列传递到函数中,因此将其转换为2D数组。 So coressponding sample array should look like this: 因此,coressponding示例数组应如下所示:

var sample_array = [["Funds Added (Donation)"],
                    ["SIG POS purchase at Paypal"],
                    ["PIN POS purchase cashback"],
                    ["PIN POS purchase cashback"],
                    ["SIG POS purchase at Paypal"]]

And you will need to provide the index for the second dimension like so descriptions[i][0] where [i] represents the first dimension and [0] the second dimension. 并且您将需要为第二维提供索引,就像descriptions[i][0] ,其中[i]代表第一维, [0]代表第二维。 So, in case of your sample array 因此,如果您的示例数组

sample_array[0][0] = "Funds Added (Donation)"
sample_array[1][0] = "SIG POS purchase at Paypal"

and so on. 等等。

Your modified function would look like this: 您修改后的函数如下所示:

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;
}

Please follow this documentation for further reference. 请遵循此文档以获取更多参考。

Final Note: The above function will only work when a column or a single cell is selected. 最后说明:仅当选择了列或单个单元格时,以上功能才起作用。 If a row is selected, it will give an error! 如果选择了一行,将会报错!

Edit: Equivalently you can use this inbuilt function to do the same 编辑:等效地,您可以使用此内置函数来执行相同的操作

=Arrayformula(sum(iferror(find("SIG",B1:B200),0)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM