简体   繁体   中英

check google spreadsheet from a webpage

how do i validate data against a google spreadsheet from a html page using jquery or javascript so for example a user enters a value and clicks the button then the value of the input field is checked for in the spreadsheet if it exists return true

<input type="text" id="user"/>
<button id="get_data">validate</button>

<script>
$("#get_data").click(function(){
    var x = $("#user").text();
    spreadsheet_url = ""
    if(check_if_x_is_in_google_spreadsheet(spreadsheet_url, x)){
        alert("ok")
    }
});
</script>

You should use the API gapi.client.sheets.spreadsheets.values.get passing input parameters:

as described on method gapi.client.sheets.spreadsheets.values.get .

Before call the api you have to initialize the client library with apiKey and clientId as documented on this example .

So your check_if_x_is_in_google_spreadsheet(spreadsheet_url, x) function may be

  function check_if_x_is_in_google_spreadsheet(spreadsheetId, x) {
    gapi.client.sheets.spreadsheets.values.get({
      spreadsheetId: spreadsheetId,
      range: 'Class Data!A:E',
    }).then(function(response) {
      var range = response.result;
      if (range.values.length > 0) {

        for (i = 0; i < range.values.length; i++) {
          var row = range.values[i];
          // Print columns A and E, which correspond to indices 0 and 4.
          if (row[0] == x) { //0 is A column
           alert('got it') // some staff
           return true;
          }
        }
      } else {
        alert('No data found!!');
      }
    }, function(response) {
      alert('Error: ' + response.result.error.message);
    });
    return false;
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM