Home > front end >  Filter Google sheet value using case inclusive var
Filter Google sheet value using case inclusive var

Time:03-12

I am trying to keep content in a file that matches the file name. It is working but I have problems with cases and I cannot do what I usually do : make everything in lowercases since the File name should not be changed. For example filename is "iPhone X" and in the cells I will have values like "iPhone X", "IPHONE X", "iphone X" and even with a space after like "iPhone X "

Is there any way to do that ?

Here is my code

//Only keep data that contains the file name
  var name = spreadsheet.getName();
  let range = sheet.getDataRange(),
      maxRows = sheet.getMaxRows(),
      srchCol_1 = 2,
      srchPatt_1 = new RegExp(name)
      newRangeVals = range.getValues().filter(r => r[0] && srchPatt_1.exec(r[srchCol_1])),   
      numRows = newRangeVals.length;  
  range.clearContent();
  sheet.getRange(2,1, numRows, newRangeVals[0].length).setValues(newRangeVals);
  sheet.deleteRows(numRows   1, maxRows - numRows);

CodePudding user response:

Description

Here is an example of how to match strings containing the same characters in order but with space or no space.

Script

function test() {
  try {
    var name = "iPhone X Y Z";
    var names = ["iPhoneXYZ","iphonexyz","iphonexy","IPHONEX Y Z","IPHONEY"];
    name = name.replace(/\s/g,"").toLowerCase();
    console.log("name = " name);
    names.forEach( item => { if( item.replace(/\s/g,"").toLowerCase() === name ) console.log("matched " item) } );
  }
  catch(err) {
    console.log(err);
  }
}

9:11:27 AM  Notice  Execution started
9:11:28 AM  Info    name = iphonexyz
9:11:28 AM  Info    matched iPhoneXYZ
9:11:28 AM  Info    matched iphonexyz
9:11:28 AM  Info    matched IPHONEX Y Z
9:11:27 AM  Notice  Execution completed

Reference

  • Related