0

我一直在研究一個項目,該項目需要一個由Google表單創建的電子表格,並將每行轉換爲單獨的新電子表格(基本上是一張發票)。我得到了這個工作,現在我需要將單元格內的字符串匹配到價格表並返回價格。 它會做一次,但接下來的項目,它不匹配。我在兩個陣列上使用了===運算符。我有myArray[1].toString(),沒有變化。我已經檢查過,以確保沒有額外的空白空間。我檢查了操作員在工作時正在返回true,如果沒有,則返回false。我真的很困惑,爲什麼它不工作。我把我想要匹配的單元格複製並粘貼到它應該匹配的單元格中。Google Apps腳本中的字符串匹配

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. 

所以我不知道該怎麼做,因爲我之前說的,我抄了新鮮的有機去骨去皮雞胸肉細胞,到價格一覽圖表只是爲了確保它們完全匹配。

回答

0

當您的測試參考invoiceValue[0][j]時,您的記錄器項目引用invoiceValue[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一旦作出了匹配。

+0

我會解決這個問題並運行它,我不知道調試器。謝謝! –

0

@Karl_S解決了它。我不明白該陣列的格式: invoiceValue picture 它需要是invoiceValue[j][0]。謝謝

相關問題