How to Search a Spreadsheet Using Google Visualization Query - Based on Mulitple Search Criteria

前端 未结 1 1935
名媛妹妹
名媛妹妹 2021-01-24 09:58

I\'m using Query.setQuery in the Google visualization query script below to search through this simple spreadsheet. The script looks though Column A for a name entered

相关标签:
1条回答
  • 2021-01-24 10:46

    you can use an asterisk to return all columns

    'select * where A = "' + searchText + '"'

    then use similar logic as before, to find the requested column

    see following working snippet...

    google.charts.load('current', {
      callback: function () {
        document.getElementById('Search').addEventListener('click', searchSheet, false);
        searchSheet();
    
        function searchSheet() {
          searchText = document.getElementById('Name').value;
    
          var queryWORK = new google.visualization.Query('https://docs.google.com/spreadsheet/ccc?key=1HpHMfoEnPgESb2XPVCgb7XyGwRAvrq3EoQj4WHj4vhA&sheet=QUERY');
          if (searchText !== '') {
            queryWORK.setQuery('select * where A = "' + searchText + '"');
          }
    
          queryWORK.send(function (response) {
            if (response.isError()) {
              console.log('Error in ID Validation Query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
              return;
            }
    
            var datatable = response.getDataTable();
            for (var i = 0; i < datatable.getNumberOfColumns(); i++) {
              var field = document.getElementById('Field').value;
              if (datatable.getColumnLabel(i) === field) {
                document.getElementById('Result').value = (datatable.getNumberOfRows() > 0) ? datatable.getValue(0, i) : '';
              }
            }
    
            var chart = new google.visualization.Table(document.getElementById('table_div'));
            chart.draw(datatable);
          });
        }
      },
      packages:['table']
    });
    div {
      margin: 6px 6px 6px 6px;
    }
    <script src="https://www.gstatic.com/charts/loader.js"></script>
    <div><label for="Name">Enter Name: </label><input id="Name" type="text" value="Bill" /></div>
    <div><label for="Field">Field: </label><input id="Field" type="text" value="Job" /></div>
    <div><input id="Search" type="button" value="Search" /></div>
    <div><label for="Result">Result: </label><input id="Result" type="text" /></div>
    <div id="table_div"></div>

    0 讨论(0)
提交回复
热议问题