开发者

Method search on google sheet data from one column using google script?

I had tried to search data like below flow picture and script to search data from google sheet using google app script but the script using is not working properly but can someone tell me how to setup search function to find data like flow on image? thanx

[Flow searching data][1]

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var menuEntries = [ {name: "Search", functionName: "searchRecord"} ];
  ss.addMenu("Commands", menuEntries);    
}

function searchRecord()
{
  var ss = SpreadsheetApp.getActiveSpreadsheet()
  var wsSearchingData = ss.getSheetByName("Searching Data")
  var wsDatabase = ss.getSheetByName("Database")
  var searchString = wsSearchingData.getRange("E4").getVal开发者_开发问答ue();

  var column =1; //column Index   
  var columnValues = wsDatabase.getRange(2, column, wsDatabase.getLastRow()).getValues(); //1st is header row
  var searchResult = columnValues.findIndex(searchString); //Row Index - 2

  var searchValue = wsDatabase.getRange("B2:B2041").getValues()
    
  var matchingDatabase = searchValue.map(searchColumn => {
    var matchColumn = columnValues.find(r => r[0] == searchColumn[0])

  return matchColumn = matchColumn ? [matchColumn[2]] : null
  })
  console.log(matchingDatabase)

  if(searchResult != -1)
  {
    //searchResult + 2 is row index.
    SpreadsheetApp.getActiveSpreadsheet().setActiveRange(sheet.getRange(searchResult + 1, 1))
  }
  
  Array.prototype.findIndex = function(search){
  if(search == "") return false;
  for (var i=0; i<this.length; i++)
    if (this[i] == search) return i;
    wsSearchingData.getRange("B11").setValue(search[0]);
    wsSearchingData.getRange("C11").setValue(search[1]);
    wsSearchingData.getRange("D11").setValue(search[2]);
    wsSearchingData.getRange("E11").setValue(search[3]);
    wsSearchingData.getRange("F11").setValue(search[4]);
  return;
  } 
}


  [1]: https://i.stack.imgur.com/HF9K8.png


  var searchResult = columnValues.findIndex(searchString); //Row Index - 2

replace the above code with:

var searchResult = columnValues.filter(r=>r[1]==searchString)

You can then put searchResult directly as output in the sheet. Make sure that [1] in the above contains the column index of Name in the columnValues Array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜