2

I am trying to look at a list of names as defined by B2, B3, B4, B5 up to B99 and compare to another set name(walkname) If the walkname is found in the list, it is evaluated as true and nothing happens except browser popup. If it is evaluated as false, the new name will be added to the last row. The below code works, but I do not want to have to define up to B99. What is the quickest way to do this and use the || at the same time?

var B2 = sstracker.getRange('B2').getValue();
var B3 = sstracker.getRange('B3').getValue();
var B4 = sstracker.getRange('B4').getValue();
var B5 = sstracker.getRange('B5').getValue();

if (walkname == B2 || walkname == B3 || walkname == B4 || walkname == B5)
{Browser.msgBox(true)} 
else {var row1 = sstracker.getRange(sstrackerLastRow + 1, 2, 1, 1).setValue(walkname).setBackground('Red');}

I semi know how to use the following code and have tried to use it to do the same thing as above. The only problem is, it evaluates one at a time rather than looking at all the instances and using an OR statement. Essentially, the new name always gets added to the list unless it is first. I am a NOOB and realize there is probably a real easy answer to make the below code do the above code. I have searched the forums but am not sure what to even look for to get help on this. Help please! When you answer, try to keep it simple.

for(var i = 0; i < teacherarray.length; i++){
var teachname = teacherarray[i];
  if(teachname == walkname){break}
  else {var row1 = sstracker.getRange(sstrackerLastRow + 1, 2, 1, 1).setValue(walkname).setBackground('Red');}

2 Answers2

0

You can just use contains:

function contains(a, obj) {
    for (var i = 0; i < a.length; i++) {
        if (a[i] === obj) {
            return true;
        }
    }
    return false;
}

// ...

if (contains(teacherarray, walkname))
{
  // do something
}
else
{
 // do something else
}

EDIT: Copy&paste'd the contains function

Community
  • 1
  • 1
Domi
  • 22,151
  • 15
  • 92
  • 122
  • I ended up using the contains formula but had problems for about 45 minutes getting it to work. I had to change === to == in order for it to work. According to another post on here, === is true when two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions. The names I was comparing were identical, at least they look identical in non computer lingo. As soon as I changed to == it worked. Can you explain that? – HarrisCountyBOE Nov 29 '13 at 14:28
  • Maybe the return type of `getValue()` or the type of walkname is not string? See [here](http://stackoverflow.com/a/359509/2228771) for examples of the differences between the two. – Domi Nov 29 '13 at 14:35
0

I usually use string functions to prevent errors due to upperCase / lowerCase differences...

if (teacherarray.toString().toLowerCase().indexOf(walkname.toLowerCase())>-1){
  Logger.log('is already there');
  }else{
  Logger.log('must be created');
}

full test :

function test(){
  var walkname = 'XMan';
  var teacherarray = SpreadsheetApp.getActiveSheet().getRange('B1:B').getValues();
  if (teacherarray.toString().toLowerCase().indexOf(walkname.toLowerCase())>-1){
    Logger.log('is already there');
  }else{
    Logger.log('must be created');
  }
}
Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • Thank you for your answer. I ended up using the other answer below. Making sure case was not an issue was the next step in fixing up my code so I will be using part of your answer. – HarrisCountyBOE Nov 29 '13 at 14:32