Google 从多个文件中搜索数据并显示多行的应用脚本

Google App Script to Search data from multiple files and displaying multiple row

我希望用户从 多个 sheet 文件 中搜索他们的信息并 显示多个结果框(行) 但是目前它正在从一个 sheet 中搜索并仅显示第一个结果(行)而不显示其余结果。

我需要在脚本中更改什么?

function submitData(obj){

  var ss = SpreadsheetApp.openById("SHEETID");
  var sheet = ss.getSheetByName("SHEETNAME");
  var  flag  =  1 ;
  var lr = sheet.getLastRow();
      for(var i = 1;i <= lr;i++){
  var id = sheet.getRange(i, 1).getValue();
      if(id == obj){
      flag = 0;
  var B = sheet.getRange(i, 2).getValue();      
  var C = sheet.getRange(i, 3).getDisplayValue();
  var data ="<div class="card"><h3>Info1:"+B+"</h3><p>Info2:"+C+"</p></div>";
  
  return data;

    }
   }
     if(flag==1){ 
       var data = '<div class="carderror">Data not found</div>';
      return data;

    } 

index.html

 <input id="id" class="form-control">
  <button type="submit" onclick="info()"> search</button>
  <div id="result"></div>
    <script>
      function info () {
        let obj = document.querySelector('#id').value;
        let updateLocation = document.querySelector('#result');
        updateLocation.innerHTML = "Searching...";

        function onFailure(error){
          let warning = error;
          updateLocation.innerHTML = warning;
        };
       
        function onSuccess(response){
          let result = response;
          updateLocation.innerHTML = result;
        };
        google.script.run.withFailureHandler(onFailure)
                         .withSuccessHandler(onSuccess)
                         .submitData(obj);
      };
    </script>
  1. 您在示例中只打开了一个 sheet 文件。如果您想搜索多个文件,您必须指定要打开哪些文件并循环遍历它们。我下面的示例假设所有文件都具有相同的 sheet 名称。
  2. 现在您正在 return 搜索第一个匹配结果,这会导致您的搜索功能过早退出。要显示多个结果,您可以将匹配的行推送到一个数组,然后在所有扫描完成后 return 该数组。
function submitData(obj) {
  //specify what sheet IDs you want to search
  var sheetNames = ['foo', 'bar', 'bla']
  //create an array to push results to
  var inject = []

  for (var i = 0; i < sheetNames.length; i++) {

    var sheetName = sheetNames[i]

    var ss = SpreadsheetApp.openById(sheetName);
    var sheet = ss.getSheetByName("SHEETNAME");
    var flag = 1;
    var lr = sheet.getLastRow();

    for (var i = 1; i <= lr; i++) {
      var id = sheet.getRange(i, 1).getValue();
      if (id == obj) {
        flag = 0;
        var B = sheet.getRange(i, 2).getValue();
        var C = sheet.getRange(i, 3).getDisplayValue();
        var data = "<div class='card'><h3>Info1:" + B + "</h3><p>Info2:" + C + "</p></div>";

        //push result to array
        inject.push(data)

      }
    }
  }

  if (flag == 1) {
    var data = '<div class="carderror">Data not found</div>';
    return data;
  } else {
    //return joined array as string
    return inject.join()
  }

}

Multi-spreadsheet 搜索

function MultiSearch() {
  const ss = SpreadsheetApp.getActive();
  const idsh = ss.getSheetByName("ssids");
  const rsh = ss.getSheetByName("Results");
  rsh.getRange(2,1,rsh.getLastRow() - 1, rsh.getLastColumn()).clearContent()
  const ids = idsh.getRange(2,1,idsh.getLastRow() - 1,idsh.getLastColumn()).getValues().filter(r => r[2] == "TRUE").map(r => r[1]);
  const r = SpreadsheetApp.getUi().prompt("Search Dialog","Enter Search String",SpreadsheetApp.getUi().ButtonSet.OK_CANCEL);
  if(r.getSelectedButton() == SpreadsheetApp.getUi().Button.OK && r.getResponseText().length > 0) {
    ids.forEach(id => {
      let ss = SpreadsheetApp.openById(id);
      let tf = ss.createTextFinder(r.getResponseText()).findAll();
      tf.forEach(f =>{
        rsh.appendRow([ss.getName,f.getSheet().getName(),f.getRow(),f.getColumn()]);
      })
    })
  }
}

ssids Sheet:

结果Sheet: