在JS中如何将阵列参数传递给气体,然后取回结果
在我的Google表中,我有一份列表,其中包含他们的详细信息。用户插入了他要寻找的标题,jQuery将标题传递给了气体功能,然后将匹配项(发现的书)返回给JS。
这是我的代码。
html
<form id="formD">
<input type="text" id="title" name="title" placeholder="Math">
<button id="submit">Search</button>
</form>
js
$("#submit").click(function()
{
var data = [];
data.push($("#title").val()); //title the user is looking for
google.script.run.withSuccessHandler(onSuccess).find(data);
});
function onSuccess(a)
{
alert(a); //example
}
气体
function doGet() {
return HtmlService.createTemplateFromFile('index')
.evaluate()
.addMetaTag('viewport', 'width=device-width, initial-scale=1');
}
function find(data) {
var sheet = SpreadsheetApp.openById("XXX").getSheetByName("XXX");
var range = sheet.getRange("A:G");
var values = range.getValues();
var result = [];
var title = data[0];
for(i=0; i < values.length; i++){
if(values[i][0] != "")
{
if(values[i][2].includes(title))
result.push(values[i]);
}
}
return result
}
我不知道如何解析和使用我传递给气体的阵列。单击按钮“搜索”时,将重新加载页面并将值附加到URL上。
In my Google Sheet I have a list of books with their details. The user inserts the title he's looking for, jQuery passes the title to a function in GAS and then it returns the matches (found books) to the JS.
Here my code.
HTML
<form id="formD">
<input type="text" id="title" name="title" placeholder="Math">
<button id="submit">Search</button>
</form>
JS
$("#submit").click(function()
{
var data = [];
data.push($("#title").val()); //title the user is looking for
google.script.run.withSuccessHandler(onSuccess).find(data);
});
function onSuccess(a)
{
alert(a); //example
}
GAS
function doGet() {
return HtmlService.createTemplateFromFile('index')
.evaluate()
.addMetaTag('viewport', 'width=device-width, initial-scale=1');
}
function find(data) {
var sheet = SpreadsheetApp.openById("XXX").getSheetByName("XXX");
var range = sheet.getRange("A:G");
var values = range.getValues();
var result = [];
var title = data[0];
for(i=0; i < values.length; i++){
if(values[i][0] != "")
{
if(values[i][2].includes(title))
result.push(values[i]);
}
}
return result
}
I don't know how to parse and use the array I pass to GAS. When the button "search" is clicked, the page is reloaded and the value appended to the URL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这样尝试
JS:
不需要数据作为数组
“ nofollow noreferrer”> client到服务器通信
Try it this way
js:
No need for data to be an array
client to server communications
这是一个问题的解决方案,即单击按钮重新加载时。
该代码在$(文档)之外。Dready(function(){})。
Here is the solution to the problem that when clicking the button the page reloads.
This piece of code was outside $(document).ready(function(){}).