是否有可能建立与谷歌脚本公共数据库(电子表格)的搜索?(Is it possible to crea

2019-08-31 06:15发布

我试图创建一个网站,用户可以过来看看一组资源,像一个门户网站,或类似JSTOR数据库。 我使用Weebly; 本网站最终会被转交给了谁不知道电脑很好,所以我试图让事情变得简单(和自由,在可行的)。

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

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

Answer 1:

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

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.

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>
  • 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

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();
}

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.



文章来源: Is it possible to create a public database (spreadsheet) search with Google Scripts?