Home > Blockchain >  How to fill a multiple string to filter a data in Google Apps Script
How to fill a multiple string to filter a data in Google Apps Script

Time:07-19

I'm going to build web to be used in search for label information My database is in Google Sheets, so I went looking for a Google Sheets fetch and gave it a try. I have no experience with Google App Scripts so I want to fill all and search data form database but it can run just one of them how to do it all (first is .gs second is Index.html) this is my reference.

  1. enter image description here

    CodePudding user response:

    To filter results based on the values you enter in the web form, pass all search elements to your search function and then check that that they are all either blank or match. This assumes that the search terms and the columns in your sheet are in the same order. Example:

    const MAPPING = [7, 10, 2, 13, 11];
    
    function processForm({ searchtext, searchtext2, searchtext3, searchtext4, searchtext5 }){ 
      let searchTerms = [searchtext, searchtext2, searchtext3, searchtext4, searchtext5];
      var result = "";
      if(searchTerms.find(Boolean)){
          result = search(searchTerms);
      }
      return result;
    }
    
    function search(searchTerms){
      var spreadsheetId   = '1B0hcELJcoLxiYXNCDx9iRl8JxijwgdFBkte90oPfCnY'; 
      var dataRange        = 'Master Barcode!A46:O1346';  
      var data = Sheets.Spreadsheets.Values.get(spreadsheetId, dataRange).values;
      var ar = [];
      data.forEach(function(f) {
        if (searchTerms.every((e, i) => !e || e === f[MAPPING[i]-1])) {
          ar.push(f);
        }
      });
      return ar;
    }
    
  • Related