Matteo Marinari
Matteo Marinari

Reputation: 19

findIndex of a string

I'm trying to find the row of a cell in a specific column that coints the same data I am passing, it should be a string. Alredy tryied to use getDysplayValue in the definition of nomeVenditore, it dosn't work. I don't have any clue of what the error is about, the argument in the method findIndex is underlined with red.

var nomeVenditore = scadenziario.getRange(eRow,13).getValue();
var colonnaVenditore = 8 //for example

var arrayVenditori = pnl.getRange(5,colonnaVenditore,5,1).getValues();
var rigaVenditore = arrayVenditori.join().split(",").findIndex(nomeVenditore)+5;

pnl.getRange(1,1).setValue(rigaVenditore);

It always returns -1 + 5 = 4

Upvotes: 0

Views: 165

Answers (1)

Marios
Marios

Reputation: 27350

Check if findIndex returns -1. If it does, then start from row 5 otherwise start from index+5:

  var nomeVenditore = scadenziario.getRange(eRow,13).getValue();
  var colonnaVenditore = 8 //for example
  var arrayVenditori = pnl.getRange(5,colonnaVenditore,5,1).getValues();
  var index = arrayVenditori.join().split(",").findIndex(c=>c==nomeVenditore);
  var rigaVenditore = index == -1 ? 5 : index + 5;
  pnl.getRange(1,1).setValue(rigaVenditore);

I used template literals for the comparison to make the code smaller:

var rigaVenditore = index==-1?5:index+5;

Upvotes: 1

Related Questions