【问题标题】:String Matching in Google Apps ScriptGoogle Apps 脚本中的字符串匹配
【发布时间】:2017-05-30 16:15:55
【问题描述】:

我一直在从事一个项目,该项目采用由 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.

所以我不确定该怎么做,正如我之前所说,我已将新鲜有机去骨去皮鸡胸肉细胞复制到价目表中,以确保它们完全匹配。

【问题讨论】:

    标签: javascript arrays string google-apps-script google-sheets


    【解决方案1】:

    您的记录器项目引用invoiceValue[j],而您的测试引用invoiceValue[0][j]。因此,虽然显示比较的记录器条目表明它应该可以工作,但您并没有比较该值。将 Logger 更改为invoiceValue[0][j],您将看到。您是否使用过Debugger 来查看数组的值?

    还有: 更改此行:

    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 循环。

    【讨论】:

    • 我会修复它并运行它,但我不知道调试器。谢谢!
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-12
      相关资源
      最近更新 更多