繁体   English   中英

Google Apps脚本中的字符串匹配

[英]String Matching in Google Apps Script

我一直在从事一个项目,该项目需要一个由Google表单创建的电子表格,并将每一行变成一个单独的新电子表格(基本上是发票)。 我可以正常工作了,现在我需要将单元格中的字符串与价格表匹配并返回价格。 它将执行一次,但是在下一项上不匹配。 我在两个数组上使用===运算符。 我有myArray[1].toString() ,没有变化。 我已检查以确保没有多余的空白。 我已经检查过该运算符在工作时返回true在不工作时返回false 我真的很困惑为什么它不起作用。 我已将要匹配的单元格复制并粘贴到应该匹配的单元格中。

  for (j=0; j < invoiceValue.length; j++)//for as many columns as are in the invoice
{
  //Logger.log("In Second Loop J is at " + j + " invoiceValue " + invoiceValue[j]);
  for(k=0; k < priceListArray.length; k++)//compares the invoiceValue array to the priceListarray
  {
    Logger.log("3rd k loop " + k + " invoiceValue is ." + invoiceValue[j] + ". J "+ j + " PRICEARRAY ." + priceListArray[k][0] + ".");
    //the peroids are to check for white space at the beginning or end of the string
    matchCheck = invoiceValue[0][j] === priceListArray[k][0];// I decided to put the out to check if the boolean values where working
    Logger.log("IF loop matching " + matchCheck);

    if(invoiceValue[0][j]=== priceListArray[k][0])
       {  
         //if it matches, it adds the price of the item to the correct cell which so far has worked
         Logger.log("MATCHED " + invoiceValue[0][j]+ " " + j + " priceListA " + priceListArray[k][0] + " K is " + k + " price " +priceListArray[k][1]);
         price = priceListArray[k][1];
         currentInvoice.getRange("C"+(4+j)).setValue(price);//range I need 
         k=priceListArray.length;// once it is matched it is dropped out of loop- no longer checks the rest of the price sheet
        }

  }

以下是日志:

[17-05-30 12:03:06:440 EDT] 3rd k loop 0 invoiceValue is .Pastured Michigan Eggs- $3.25 a dozen. J 0 PRICEARRAY .Pastured Michigan Eggs- $3.25 a dozen.  
[17-05-30 12:03:06:440 EDT] IF loop matching true  
[17-05-30 12:03:06:441 EDT] MATCHED Pastured Michigan Eggs- $3.25 a dozen 0 priceListA Pastured Michigan Eggs- $3.25 a dozen K is 0 price 3.25  
[17-05-30 12:03:06:513 EDT] Drop out of J loop 0  
[17-05-30 12:03:06:514 EDT] 3rd k loop 0 invoiceValue is .Fresh Organic Boneless   Skinless Chicken Breast. J 1 PRICEARRAY .Pastured Michigan Eggs- $3.25 a dozen.  
[17-05-30 12:03:06:514 EDT] IF loop matching false  
[17-05-30 12:03:06:515 EDT] 3rd k loop 1 invoiceValue is .Fresh Organic Boneless Skinless Chicken Breast. J 1 PRICEARRAY .Organic, Soy Free Eggs, Pastured, Michigan- $5.75 a dozen.  
[17-05-30 12:03:06:515 EDT] IF loop matching false  
[17-05-30 12:03:06:516 EDT] 3rd k loop 2 invoiceValue is .Fresh Organic Boneless Skinless Chicken Breast. J 1 PRICEARRAY .Pastured Michigan Duck Eggs- $8 a dozen.  
[17-05-30 12:03:06:516 EDT] IF loop matching false  
[17-05-30 12:03:06:517 EDT] 3rd k loop 3 invoiceValue is .Fresh Organic Boneless Skinless Chicken Breast. J 1 PRICEARRAY .Fresh Organic Boneless Skinless Chicken Breast.  
[17-05-30 12:03:06:517 EDT] IF loop matching false  
[17-05-30 12:03:06:518 EDT] 3rd k loop 4 invoiceValue is .Fresh Organic Boneless Skinless Chicken Breast. J 1 PRICEARRAY .Fresh Organic bone less thigh $4.69 lb.

因此,我不确定该怎么做,就像我之前说过的那样,我已将新鲜有机无骨去皮鸡胸肉复制到价格表中,以确保它们完全匹配。

您的记录器项引用invoiceValue[j]而测试项引用invoiceValue[0][j] 因此,尽管显示比较结果的记录器条目建议它应该起作用,但您没有在比较该值。 将Logger更改为invoiceValue[0][j] ,您将看到。 您是否使用调试器查看了数组的值?

另外:更改此行:

k=priceListArray.length;// once it is matched it is dropped out of loop- no longer checks the rest of the price sheet

至:

break;       // <=== breaks out of the loop early

匹配后,它将在K上停止for循环。

@Karl_S解决了它。 我不了解数组的格式: invoiceValue图片它必须是invoiceValue[j][0] 谢谢

暂无
暂无

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

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