是否可以使用Google脚本创建公共数据库(电子表格)搜索? [英] Is it possible to create a public database (spreadsheet) search with Google Scripts?

查看:209
本文介绍了是否可以使用Google脚本创建公共数据库(电子表格)搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个网站,用户可以来找到一组资源,像门户网站或像JSTOR这样的数据库。我使用Weebly;这个网站最终会被转移给不熟悉计算机的人,所以我想让事情变得简单。(和免费,在哪里可以)

I'm trying to create a website where users can come and look for a set of resources, something like a portal, or a database like JSTOR. I am using Weebly; this website will eventually be turned over to someone who does not know computers well, so I'm trying to keep things simple (and free, where doable).

我的想法是使用Google电子表格/表单来处理每个资源(标题,作者,类型,主题,国家/地区等)的数据输入和存储。 然后找到一些创建可以放在网站上的搜索功能的方法。任何用户可以到达网站,放入他们想要查找的任何标准,并且数据库中的任何资源将被列出以供用户进一步调查。用户不会向电子表格中添加数据;只有查询它的数据。

My thought was to use Google Spreadsheets/Forms to handle the input and storage of the data for each individual resources (Title, Author, Type, Topic, Country, etc.), and then find some some method of creating a search function that could placed on the website. Any user could arrive at the site, put in whatever criteria they want to look for, and any resources in the database would be listed out for the user to further investigate. Users would not be adding data to the spreadsheets; only querying it for data.

我的第一个问题是这样的脚本/安排可能,可以嵌入到网站页面?我的第二个问题是最好的方法是什么?

My first question is such a script/arrangement possible and can it be embedded into a website page? My second question is what would the best approach be?

推荐答案

是的,这是可能的,但可以通过各种方式。

Yes this is certainly possible, but can achieved in a variety of ways.

您可以使用的一种方法是以JSON格式检索电子表格中的所有数据,并将其作为HTML表格添加到DOM中。然后,您可以使用一个很好的插件,如 dataTables ,它有一个很好的本地搜索功能。我将给出一个基本的例子如下。

One approach you could take with this is to retrieve all the data from the spreadsheet as JSON format and add it to the DOM as a HTML table. Then you can use a nice plugin like dataTables which has a pretty good native search function. I'll give a basic example below.

要检索数据,您可以使用 Googles电子表格JSON API 。一个基本的例子如下。

To retrieve the data you can use Googles spreadsheet JSON API. A basic example is below.

<script src="http://spreadsheets.google.com/feeds/cells/*ID*/*WS*/public/values?alt=json-in-script&amp;callback=*FN*"></script>




  • 其中 ID 是电子表格的长ID 。

  • 其中 WS 是工作表号码。 1,2,3等。

  • 其中 FN 是您要呼叫的功能。在我的下面的函数我使用importGSS

    • Where ID is the spreadsheet's long ID.
    • Where WS is the worksheet number e.g. 1,2,3 etc.
    • Where FN is the function you want to call. In my below function i use importGSS
    • 然后我写了下面的脚本,将数据添加到一个HTML表。它首先将第一行添加到< thead> 部分,然后将其余部分添加到< tbody> 部分。

      Then I've written the below script that adds the data to a HTML table. It first adds the first row to a <thead> section and then adds the rest to the <tbody> section.

      function cellEntries(json, dest) {
          var table = document.createElement('table');
          var thead = document.createElement('thead');
          var tbody = document.createElement('tbody');
          var thr;
          var tr;
          var entries = json.feed.entry;
          var cols = json.feed.gs$colCount.$t;
      
          for (var i=0; i <cols; i++) {
              var entry = json.feed.entry[i];
              if (entry.gs$cell.col == '1') {
                  if (thr != null) {
                      tbody.appendChild(thr);
                  }
                  thr = document.createElement('tr');
              }
              var th = document.createElement('th');
              th.appendChild(document.createTextNode(entry.content.$t));
              thr.appendChild(th);
          } 
          for (var i=cols; i < json.feed.entry.length; i++) {
              var entry = json.feed.entry[i];
              if (entry.gs$cell.col == '1') {
                  if (tr != null) {
                      tbody.appendChild(tr);
                  }
                  tr = document.createElement('tr');
              }
              var td = document.createElement('td');
              td.appendChild(document.createTextNode(entry.content.$t));
              tr.appendChild(td);
          } 
          $(thead).append(thr);
          $(tbody).append(tr);
          $(table).append(thead);
          $(table).append(tbody);
          $(dest).append(table);
          $(dest + ' table').dataTable();
      }
      

      然后可以使用...回调函数: #Destination 是您要添加HTML表的< div>

      You can then call back the function with ... where #Destination is the <div> you want to add the HTML table to.

      function importGSS(json){
         cellEntries(json, '#Destination');
      };
      

      完成后,您将看到如下面的截图,顶部的最终结果和底部原始电子表格。我编辑了一些信息。我希望这有一些帮助。

      Once all completed you'll see something like the below screenshot, the top the final results and the bottom the original spreadsheet. I've edited out some information. I hope this has been of some help.

      >

      这篇关于是否可以使用Google脚本创建公共数据库(电子表格)搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆