在JS中如何将阵列参数传递给气体,然后取回结果

发布于 2025-02-01 05:03:24 字数 1254 浏览 5 评论 0原文

在我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南笙 2025-02-08 05:03:24

这样尝试

function find(data) {
  var sheet = SpreadsheetApp.openById("XXX").getSheetByName("XXX");
  var range = sheet.getRange("A1:G" + sheet.getLastRow());
  var values = range.getValues();
  var result = [];
  var title = data;
  for (let i = 0; i < values.length; i++) {
    if (values[i][0] != "") {
      if (values[i][2].includes(title))
        result.push(values[i]);
    }
  }
  return result;
}

function onSuccess(a) {
    alert(a.join('\n')); //example
}

JS:

不需要数据作为数组

$("#submit").click(function() {
  var data = $("#title").val(); 
  google.script.run.withSuccessHandler(onSuccess).find(data);
});

“ nofollow noreferrer”> client到服务器通信

Try it this way

function find(data) {
  var sheet = SpreadsheetApp.openById("XXX").getSheetByName("XXX");
  var range = sheet.getRange("A1:G" + sheet.getLastRow());
  var values = range.getValues();
  var result = [];
  var title = data;
  for (let i = 0; i < values.length; i++) {
    if (values[i][0] != "") {
      if (values[i][2].includes(title))
        result.push(values[i]);
    }
  }
  return result;
}

function onSuccess(a) {
    alert(a.join('\n')); //example
}

js:

No need for data to be an array

$("#submit").click(function() {
  var data = $("#title").val(); 
  google.script.run.withSuccessHandler(onSuccess).find(data);
});

client to server communications

拿命拼未来 2025-02-08 05:03:24

这是一个问题的解决方案,即单击按钮重新加载时。

$("#sumbit").click(function(){

}

该代码在$(文档)之外。Dready(function(){})。

Here is the solution to the problem that when clicking the button the page reloads.

$("#sumbit").click(function(){

}

This piece of code was outside $(document).ready(function(){}).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文