How to Search Spreadsheet Using Google Visualization Query

前端 未结 1 1006
-上瘾入骨i
-上瘾入骨i 2021-01-16 04:52

I\'ve got this simple webpage which uses google.visualization.Query to pull the values of three specific cells from this spreadsheet, and then sets the values of three corre

1条回答
  •  孤独总比滥情好
    2021-01-16 04:59

    you can use Query.setQuery to set a SQL-like statement,
    which can be used to select certain columns and rows

    the following will select the Job & Hours columns where Name = Bill
    'select B, C where A = "Bill"'

    you can also search for partial text, this will select both Bill and Kim
    'select B, C where A like "%i%"'

    following is a working snippet, the inputs are given the same names as the Columns

    enter a full or partial name and click Search to see the results...

    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 B, C where A like "%' + 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++) {
              document.getElementById(datatable.getColumnLabel(i)).value =
                (datatable.getNumberOfRows() > 0) ? datatable.getValue(0, i) : '';
            }
    
            var chart = new google.visualization.Table(document.getElementById('table_div'));
            chart.draw(datatable);
          });
        }
      },
      packages:['table']
    });
    div {
      padding: 6px 6px 6px 6px;
    }
    
    

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