This is my first time using Stack Overflow. I am a beginner level JavaScript/Google App Script coder.
I have a list of products that my app reads from 'Product' sheet and a list of keywords coming from 'Model' sheet. When running the code the app finds 2004 as a match for the keyword '200' I have tried different solutions but so far no success. This is my original code
Code
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;
}
}
}
}
Example of list of products: 56040206 2002 Dodge Ram Truck (Gas) 5.9L Plug & Play ECM PCM | 56040206AC 56040205 2002 Dodge Ram Truck (Gas) 5.9L Plug & Play ECM PCM | 56040205AC 56044477 2004 Jeep Wrangler 4.0L Plug & Play ECM PCM | 56044477AD 56044419 2004 Jeep Wrangler 4.0L Plug & Play ECM PCM | 56044419AF
Example of list of Models Suburban Tahoe 200 300 Wrangler Ram Truck (Gas)
Result 200 200 200 200
What I want Ram Truck (Gas) Ram Truck (Gas) Wrangler Wrangler
I don't want "200" to be considered as a match with for example "2004"