如何在Sheetrock中使用LIKE运算符

时间:2016-03-06 04:18:22

标签: javascript html sql google-sheets sheetrock

我试图在SQL语句中调用LIKE之后的Javascript变量elem,以便在那里使用输入文本。然而,我正在使用的方式不适用于我正在使用的Sheetrock库(http://chriszarate.github.io/sheetrock/)。

<!DOCTYPE html>
<html>
<body>

Enter Tracking Code: <input type="text" id="textbox_id">
<input type="button" value="Submit">

<table id="switch-hitters" class="table table-condensed table-striped"></table>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-sheetrock/1.0.1/dist/sheetrock.min.js"></script>


<script>

var mySpreadsheet = 'https://docs.google.com/spreadsheets/d/1_1elTo5zH1ew6KPYwoWtixX9hzFc8oxdRy5A0LWFkwg/edit#gid=0';
var elem =  document.getElementById('textbox_id').value;
$('#switch-hitters').sheetrock({
    url: mySpreadsheet,
    query: "select A,B,C,D,E where A LIKE %"+elem+"%"
});

</script>

</body>
</html>

1 个答案:

答案 0 :(得分:3)

既然你在这里更新了你的问题就更新了答案。检查工作jsfiddle:https://jsfiddle.net/r0sk7vtf/

  • 您需要处理提交按钮点击事件,然后致电服务

  • 虽然电子表格api理解为没有引号,但它不能通过sheetrock.js工作,因此您需要在查询中使用A like '9999%'

段:

var mySpreadsheet = 'https://docs.google.com/spreadsheets/d/1_1elTo5zH1ew6KPYwoWtixX9hzFc8oxdRy5A0LWFkwg/edit#gid=0';

var button = $('#btn'), elem = $('#textbox_id')

button.on('click', function(e){ 
  var v =  elem.val();
  $('#switch-hitters').sheetrock({
    url: mySpreadsheet,
    query: "select A,B,C,D,E where A like '" + v + "%'"
  });
 })