Search Operation on Google Sheets using AppScript

This tutorial explains to write an app script to search Key element in a sheet. This is useful in many scenarios. Please refer other tutorials to know the application of this operation.

 

var ss = SpreadsheetApp.openByUrl("Your Spread Sheet URL");
var sheet = ss.getSheetByName("Sheet1");

function doGet(e){
 return search(e) ;
}  

function doPost(e){
 return search(e) ;
}  


function search(e){
 var id = e.parameter.id;
  
  var values = sheet.getRange(2, 1, sheet.getLastRow(),sheet.getLastColumn()).getValues();
  
  for(var i = 0;i<values.length; i++){
    
    if(values[i][0] == id ){
      i=i+2;
      
      var name = sheet.getRange(i,2).getValue();
      return ContentService.createTextOutput(name).setMimeType(ContentService.MimeType.TEXT);
    }
  }
  return ContentService.createTextOutput("Id not found").setMimeType(ContentService.MimeType.TEXT);
  
}