如何精确匹配字符串列表中的单词列表Google Apps Script



这是我第一次使用Stack Overflow。我是一个初级水平的JavaScript/Google App Script编码员。

我有一个产品列表,我的应用程序从"产品"表读取和关键字列表来自"模型"表。当运行代码时,应用程序发现2004作为关键字"200"的匹配,我尝试了不同的解决方案,但到目前为止还没有成功。这是我的原始代码

代码
function onOpen(e) {
CreateMenu();
}
function CreatseMenu() {
// Add a custom menu to the spreadsheet.
SpreadsheetApp.getUi() // Or DocumentApp, SlidesApp, or FormApp.
.createMenu('Data Update')
.addItem('Find Year', 'findYear')
.addItem('Find Make', 'findMake')
.addItem('Find Model', 'findModel')
.addItem('Find Engine Size', 'findEngineSize')
.addToUi();
}
const globalConst = {
get ProductSpreadSheet() {
delete this.ProductSpreadSheet;
return (this.ProductSpreadSheet = SpreadsheetApp.openById('12HJqcc8sFghG3VM4YtFBNbTzzDdDnY4GNyDYQ37vBa8'));
},
get ProductSheet() {
delete this.ProductSheet;
return (this.ProductSheet = globalConst.ProductSpreadSheet.getSheetByName('Product'));
},
get ResultSheet() {
delete this.ResultSheet;
return (this.ResultSheet = globalConst.ProductSpreadSheet.getSheetByName('Result'));
},
get ProductSheetNumberOfRows() {
delete this.ProductSheetNumberOfRows;
return (this.ProductSheetNumberOfRows = globalConst.ProductSheet.getLastRow());
},
get ProductSheetValues() {
delete this.ProductSheetValues;
return (this.ProductSheetValues = globalConst.ProductSheet.getRange('A1:A' + globalConst.ProductSheetNumberOfRows).getValues());
},
};

// This Function will find the Model of each product and records it in the result sheet (Column C)
function findModel() {
var modelSheet = globalConst.ProductSpreadSheet.getSheetByName('Model');
var modelSheetNumberOfRows = countRows(modelSheet);
var modelSheetValues = modelSheet.getRange('A1:A' + modelSheetNumberOfRows).getValues();
var modelColumnNumber = 3;
this.getResults(modelSheetNumberOfRows, modelSheetValues, modelColumnNumber)  
}
// This function returns the number of the last row with value for a sheet 
function countRows(sheet) {
var numberOfRows = sheet.getLastRow();

return numberOfRows;
}
// This function gets the result for the searched keywords and returns them
function getResults(searchNumberOfRows, searchValues, searchColumnNumber) {
for (i = 0; i < globalConst.ProductSheetNumberOfRows; i++) {
for (j = 0; j < searchNumberOfRows; j++) {
var index = globalConst.ProductSheetValues[i].toString().indexOf(searchValues[j].toString());

if(index !== -1){
globalConst.ResultSheet.getRange(i + 2, searchColumnNumber).setValue(searchValues[j]);
break;
}
}
}
}
产品列表示例:2002道奇公羊卡车(汽油)5.9升即插即用ECM PCM | 56040206AC2002道奇公羊卡车(汽油)5.9升即插即用ECM PCM | 56040205AC2004吉普牧马人4.0升即插即用ECM PCM | 56044477 ad56044419 2004吉普牧马人4.0L即插即用ECM PCM | 56044419AF
模型列表示例郊区太浩200300牧人公羊卡车(燃气)
结果200200200
我想要的公羊车(气体)公羊车(气体)牧人

我不想要" 2000 "被认为与例如"2004">

匹配

下面是一个如何解决这个问题的例子:

// in case you have all data in plain text:
const input = "red apple for $2.00, ripe watermelon for $10.00, green melon for $5.00";
// you can split them in porducts:
const products = input.split(', ');
// check how it looks like now:
console.log(products);
/** output:
[
"red apple for $2.00",
"ripe watermelon for $10.00",
"green melon for $5.00"
]
*/
// than, iterate this array again with .map method to break it down once again:
const productBrokenDown = products.map(product => product.split(' '));
// check how it looks like now:
console.log(productBrokenDown);
/** output:
[
["red","apple","for","$2.00"],
["ripe","watermelon","for","$10.00"],
["green","melon","for","$5.00"]
]
*/
// as you can see, the values becomes a 2D array which each row contains an array of values, which matches a patten [Color,FruitType,"for",Price], which we can now check the 'FruitType' of each row to return the true / false result you expected:
const results = productBrokenDown.map(row => row[1] === 'melon');
// check the results:
console.log(results);
/** output:
[
false,
false,
true
]
*/

最新更新