Search spreadsheet by column, return rows
You may want to use the Google Apps Script textFinder class.
This example function will search a string and show the row of the first occurrence in an alert dialog:
function searchString(){
var sheet = SpreadsheetApp.getActiveSheet()
var search_string = "dog"
var textFinder = sheet.createTextFinder(search_string)
var search_row = textFinder.findNext().getRow()
var ui = SpreadsheetApp.getUi();
ui.alert("search row: " + search_row)
}
You can use the code below to search in a specific column. Code is self explanatory.
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [ {name: "Search", functionName: "onSearch"} ];
ss.addMenu("Commands", menuEntries);
}
function onSearch()
{
var searchString = "Test11";
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SheetName");
var column =4; //column Index
var columnValues = sheet.getRange(2, column, sheet.getLastRow()).getValues(); //1st is header row
var searchResult = columnValues.findIndex(searchString); //Row Index - 2
if(searchResult != -1)
{
//searchResult + 2 is row index.
SpreadsheetApp.getActiveSpreadsheet().setActiveRange(sheet.getRange(searchResult + 2, 1))
}
}
Array.prototype.findIndex = function(search){
if(search == "") return false;
for (var i=0; i<this.length; i++)
if (this[i] == search) return i;
return -1;
}