DanCue
DanCue

Reputation: 772

Search for string between two characters if next to a specific string

I am in need of some modification to my function to allow for a search of two strings on one line of a value. I am trying to work through this on my own but I need some help. Here is an example of a cell value being looked at. Assume there are no leading or trailing newlines. Also, all the cells have the same format. same number of lines, same structure of membertype: last, first etc.

enter image description here

Say I want to see if this cell contains a team lead with the name of last2 or a Manager with the name first4. Both the type of employee and name would be user inputted.

I tried using the following that I created with the help of this.

indexOf(':(.*):')

It returns the position of the content between and including the colons. Then I tried the following:

flatUniqArr[0].search('Supervisor:')

This is where I'm stuck. It returns the index to the last digit of the first line.

My thought was to do a search of the user inputted name between the colons if they follow the user inputted member type. How can I accomplish this?

Clarifications: The end goal is to verify that the name and member type are on the same line and excluded from an array I am building for .setHiddenValues(). So if they are on the same line exclude from list.

Here is the function I will be adding it to:

var flatUniqArr = colValueArr.map(function(e){return e[0].toString();})
.filter(function(e,i,a){
  return (a.indexOf(e) == i && !(visibleValueArr.some(function(f){
    return e.search(new RegExp(f,'i')) + 1;
  })));
});
return flatUniqArr;

Where flatUniqArr is the list of hidden values. colValueArr is the array of values from a column. visibleValueArr is the name which is user inputted and memberType will be the member type.

Attempts using Liora's solution: (Updated... Works now)

var flatUniqArr = []
var lines = []
Logger.log(visibleValueArr)
Logger.log(memberType)
for (var i = 0; i < colValueArr.length; i++){
  lines = colValueArr[i].toString().split('\n');
  var found = false;
  for(var j = 0; j < lines.length; j++){
    var data = lines[j].toLowerCase().split(':')
    if(data[0] == memberType.toString().toLowerCase() && data[1].indexOf(visibleValueArr.toString().toLowerCase()) != -1){
      found = true;
    }
  }
  Logger.log(found)
  if(found == false){flatUniqArr.push(colValueArr[i])} 
}

return flatUniqArr;

It works now. It seems like a lot of code though. I'd be open to alternative solutions if they are faster and/or less lines of code.

Updated: Added .toString().toLowerCase() as the user may input lowercase values.

Upvotes: 1

Views: 78

Answers (2)

Liora Haydont
Liora Haydont

Reputation: 1283

I assume all the line have this format.

If you split each line with the separator ":"

var array = value.split(":")

Then you'd have

array[0] //the current role
array[1] //the list of name
array[2] //the email

And you can check each names then

if(array[0] == "Team Lead" && array[1].indexOf("last2") != -1)

An example with a linesplit:

var lines = value.toString().split("\n");
var found = false;
for(var i = 0; i < lines.length ; i++){
    var data = value.split(":")
    if(data[0] == "Team Lead" && data[1].indexOf("last2") != -1){
         found = true;
    }
}

Upvotes: 1

Aaron Leon
Aaron Leon

Reputation: 116

How about just building the regex using the user input?

function search(line, employeeType, employeeName) {
    var regexp = '/' + employeeType + ': ' + employeeName + '/'
    return line.search(regexp)
}

Or better yet, if it always occurs at the beginning of the string, just use startsWith()

Upvotes: 1

Related Questions