How to Exact Match a list of words in a list of strings Google Apps Script

541 Views Asked by At

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"

1

There are 1 best solutions below

0
Ping On

Here is an example on how you can work this out:

// 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
]
*/