Google Apps脚本中的字符串匹配

时间:2017-05-30 16:15:56

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

我一直致力于一个项目,该项目采用由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.

以下是日志:

setInterval

所以我不知道该怎么做,正如我之前所说,我已将Fresh Organic Boneless Skinless Chicken Breast细胞复制到价目表中,以确保它们完全匹配。

2 个答案:

答案 0 :(得分:0)

当您的测试引用invoiceValue[j]时,您的记录器项目引用了invoiceValue[0][j]。因此,虽然显示比较的记录器条目表明它应该有效,但您没有比较该值。将记录器更改为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循环。

答案 1 :(得分:0)

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