我正在尝试构建一个搜索选项,用户将在其中输入他们的 ID,他们将看到存储在 Google 电子表格中的相应 ID 的结果。
喜欢用户会输入: 1 他们会看到结果:Nitu 5 他们会看到结果:Dipon
<input id="id" type="text" placeholder="Your ID">
<input id="search" type="submit" value="Search">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
function displayContent(json) {
var string = "<table>";
var len = json.feed.entry.length;
for (var i=0; i<len; i++) {
var id = json.feed.entry[i].gsx$id.$t;
var name = json.feed.entry[i].gsx$name.$t;
string += '<tr><td>' + id + '</td><td>' + name + '</td></tr>';
}
string += "</table>";
$(string).appendTo('body');
}
</script>
<script src="http://spreadsheets.google.com/feeds/list/1WMvFqepPmQUVwTFFUZW_3SfSt1ZBr_AEy_XdUXZ72ng/od6/public/values?alt=json-in-script&callback=displayContent" type="text/javascript"></script>
我可以使用上面的代码片段从我的谷歌工作表中获取数据。但我不想一次获取所有数据。我只想要基于 ID 搜索的特定数据。
那么现在我怎样才能在 JavaScript 中集成这个搜索功能呢?
最佳答案
您可以使用Structured Query用于谷歌电子表格 API。
因此,如果使用正确的结构化查询发出 GET
请求,您将仅获得相关数据。在您的情况下添加 sq=id==5
作为查询参数。
因此,您应该创建一个函数来发出正确的 GET
请求并仅获取您需要的数据,而不是加载整个数据并随后对其进行解析。
var fetchName = function(ID, workSheetID){
var url = 'https://spreadsheets.google.com/feeds/list/'+workSheetID+'/od6/public/values?sq=id='+ID+'&alt=json-in-script&callback=?';
$.get( url, function( data ) {
//Parse and Do Your Stuff Here
});
}
希望这有帮助。干杯!!
关于javascript - 使用 javascript 进行 Google 电子表格搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35676596/