title: How do I improve the performance of this matrix search function?
body: <p>I have data in the form of Sheets representing levels of taxonomy. Columns on each sheet are the members of a given (Phylum, Class, Order, etc.) the name of which is the column header.</p>
  <p>Headers are the Order, and column contents are the Families in that Order.
  I have this search function that searches the Order sheet for a given Family, and returns the Order that Family is a member of. But it's very slow.</p>
  <p>Can I improve this search algorithm, or my search approach to more quickly run this function in a number of cells in another sheet? This workbook has the reference sheets, and a Sample Foray sheet that shows the function in action I'm hoping to markedly improve the performance of.</p>
  <p><a href="https://docs.google.com/spreadsheets/d/1HvDpgWd6vhAF9UPiomwpmRLnzlduPU9c2ueWRwq3oZw/edit?usp=sharing" rel="nofollow noreferrer">https://docs.google.com/spreadsheets/d/1HvDpgWd6vhAF9UPiomwpmRLnzlduPU9c2ueWRwq3oZw/edit?usp=sharing</a></p>
  <pre><code>function findvalue(sheetName, searchVal) {
    if(searchVal.length === 0){
        throw 'Error. Incertae sedis.';
    };
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
    var rangeData = sheet.getDataRange();
    var lastColumn = rangeData.getLastColumn();
    var lastRow = rangeData.getLastRow();
    var searchRange = sheet.getRange(1,1, lastRow-1, lastColumn-1);
    // Get array of values in the search Range
    var rangeValues = searchRange.getValues();
    // Loop through array and if condition met
    for ( i = 0; i &lt; lastColumn - 1; i++){
      for ( j = 0 ; j &lt; lastRow - 1; j++){
        if(rangeValues[j][i] === searchVal){
          return rangeValues[0][i]
        };
      };
    };
  };
  </code></pre>