0

I have been working on a new project and I need to create new sheets based on cell value.

function ign_list() {
  var nRange = SpreadsheetApp.getActive().getRangeByName("player")
  return nRange.getValues()
}


function sheet_list() {
  var out = new Array()
  var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
  for (var i=0 ; i<sheets.length ; i++) out.push( [ sheets[i].getName() ] )
  return out   
}

function new_sheet() {
  var ui = SpreadsheetApp.getUi();
  var sheet_names = sheet_list();
  var ign_names = ign_list();

  for (var i=0; i<ign_names.length; i++){
    for (var n=0; n<sheet_names.length; n++) {
      if (ign_names[i] !="") {
        if (ign_names[i] == sheet_names[n]) {
          ui.alert(ign_names[i]+" Equal "+sheet_names[n])
        } else {
          ui.alert(ign_names[i]+" Not Equal "+sheet_names[n])
        }
      }
    }
  }
}

I have a sheet called Raw Stats and in the range, I have a cell with the value Raw Stats but when comparing it says is not equal.

is there another way of comparing string?

Using indexOf

for (var i=0; i<ign_names.length; i++){
    if (sheet_names.indexOf(ign_names[i][0]) != -1) {
      SpreadsheetApp.getActiveSpreadsheet().insertSheet().setName(ign_names[i][0]);
    } else {
    }
  }

Always returning -1 even when as the same value

drone
  • 3
  • 4

1 Answers1

0

You are using the getValues() method. That returns an array of rows.

Even if you range is compromised of only one column, the result of that would be: [[A1],[A2],[A3],...]

You can fix this by flattening the array or by accounting for this with ign_names[i][0] instead.

Hope this helps

ZektorH
  • 2,680
  • 1
  • 7
  • 20
  • 1
    Thanks it worked and i was trying to figure this for 2 hours and was just that... – drone Oct 30 '19 at 11:47
  • It's quite a common mistake. Glad I was able to help! – ZektorH Oct 30 '19 at 11:48
  • I dont know if you can help a little more i saw that using indexOf is better for this type of verification but always give -1 even when the value is equal – drone Oct 30 '19 at 14:06
  • Hello @drone, please search StackOverflow before asking questions. [Here](https://stackoverflow.com/questions/3586775) is an existing question about string comparison in JavaScript. – ZektorH Oct 30 '19 at 14:18