Find Cell Matching Value And Return Rownumber
Here the code
function rowOfEmployee(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = sheet.getDataRange().getValues();
var employeeName = sheet.getRange("C2").getValue();
for(var i = 0; i<data.length;i++){
if(data[i][1] == employeeName){ //[1] because column B
Logger.log((i+1))
return i+1;
}
}
}
When you want to perform this kind of lookup it is better to retrieve data with sheet.getDataRange().getValues() because in this case you will get data as a table of values this is faster. When you use the standard EmployeeSheet.getRange(2,3,1,1).getValue() in fact you retrieve an object which need more time to be processed and each time you query the spreadsheet.
In my exemple I made only one query to retrieve all data instead n query to retrieve one data each time.
Stéphane
I faced this problem today and I found a native method to do the job.
Here is the snippet to test if it works for you:
var spreadsheet = SpreadsheetApp.getActive();
var tosearch = "your text to search";
var tf = spreadsheet.createTextFinder(tosearch);
var all = tf.findAll();
for (var i = 0; i < all.length; i++) {
Logger.log('The sheet %s, cell %s, has the value %s.', all[i].getSheet().getName(), all[i].getA1Notation(), all[i].getValue());
}