Writing google Javascript similar to vlookup

for-loop, google-apps-script, google-sheets, javascript

Solution

in its simplest form and to see the working principle you could try this :

function findinB() {
  var sh = SpreadsheetApp.getActiveSheet();
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var last=ss.getLastRow();
  var data=sh.getRange(1,1,last,2).getValues();// create an array of data from columns A and B
  var valB=Browser.inputBox('Enter value to search in B')
  for(nn=0;nn<data.length;++nn){
    if (data[nn][1]==valB){break} ;// if a match in column B is found, break the loop
      }
Browser.msgBox(data[nn][0]);// show column A
}

Problem

``` ColumnA ColumnB jhinz 115 tom 116 ``` The idea behind this code is someone enters a number (lets say 116), the computer looks it up in column B and returns the name in column A (tom) The only part I need help on for the code is the computer looking up the value in column 116. I was trying to do a for loop with a nested if statement but it wasn't working. Could someone help me?

Original source