2

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).

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?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Nick Mischler
  • 175
  • 5
  • 22

1 Answers1

4

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.

enter image description here

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
dev
  • 3,969
  • 3
  • 24
  • 36
  • Thank you, this is a wonderful start. I'm a little more perceptive on using and adapting; less so on writing from scratch. While I've played around somewhat, I do have a few questions: - The HTML script call for the data is to be put on the webpage correct? Likewise, DataTable must also be called on the webpage, but does positioning matter? - The `cellEntries`, `importGSS`, and `$(document).ready` _(if I understand correctly from the DataTables)_ must be in the spreadsheet scripting, correct? – Nick Mischler Apr 16 '13 at 02:55
  • Sorry yes I should have pointed out the ordering does matter. The Google ` – dev Apr 16 '13 at 07:21
  • I think I'm getting it, but I'm afraid I have a few more questions: - "#Destination" in the code above is supposed to relate to a `
    ` wherever on the page I wish the table to appear, correct? - The flow of the document should be: header: `define JQuerry, define DataTables, define the above JS code`, body: `define div for table`, footer: `Google script`, correct? - The Google `
    – Nick Mischler Apr 16 '13 at 10:23
  • @vltech Ah, getting closer it appears... I'm getting the following message in the error console: `json.feed.gs$colCount is undefined`. I was able to get a copy of the [DataTables tutorial](http://www.datatables.net/blog/Getting_started_with_DataTables%3A_First_steps) running, but I'm still missing using from Google. [Here](http://interculturalresources.weebly.com/webtest.html) is my current progress; I'm sure I just have things out of order. - If I read things correctly, DataTables responds to the ID of the table, not a div. Would that be an issue? – Nick Mischler Apr 16 '13 at 10:37
  • @TerraFimeira There's 3 errors. 1) Sorry this is my fault the first line of your tablescript.js should be `function cellEntries(json, dest) {` .... 2) At the bottom of cellEntries function you have written `$('resourcetable').dataTable();` this should be `$(dest).dataTable();` ..... and 3)in the importGSS function you have written `cellEntries(json, 'resourcetable');` this should be `cellEntries(json, '#resourcetable');`... with the `#` to point it to an ID. – dev Apr 16 '13 at 14:46
  • @vltech First of all, I was playing with the search function on the example table I made and it is exactly what I was looking for. Many thanks! I think everything is lined up, but I'm still getting the error `json.feed.gs$colCount is undefined`. I went into the tablescript.jp and set that line to `var cols = 5;` to see if I could isolate the issue there, but I then get `entry.gs$cell is undefined - Line: 12`. It appears that something isn't being passed properly... – Nick Mischler Apr 17 '13 at 02:44
  • @TerraFimeira again my apologise. Depending on what you want to do with the json data depends on how you call it back. In the embedded script file. `http://spreadsheets.google.com/feeds/**list**/...` should be `http://spreadsheets.google.com/feeds/**cells**/...`. This basically just tells it whether to retrieve the data in rows or by cells. As I use both I forgot to select the right one. I've updated my answer to reflect this error – dev Apr 17 '13 at 07:36
  • @vltech Last question, I'm sure. The table now appears, but I get the following message: `DataTables warning: Attempted to initialise DataTables on a node which is not a table: DIV` DataTables appears to only react to the `` element, so the script created `
    ` element would need an id as well, say `destination_table`, and then the DataTables call point to that? I'll play around myself (and learn `document.createElement` in the process), but I'd like to leave any edits to your answer to you if I can. Again, thanks for all the help until now.
    – Nick Mischler Apr 18 '13 at 02:49
  • @TerraFimeira ah yes simple correction for this. `$(dest + ' table').dataTable();` I've updated my answer to show this – dev Apr 18 '13 at 11:20
  • @vltech Thanks for all your help; I finally have it working it appears. I did have to make a little adjustments to the naming of the `
    ` id and `` id, but that much I could figure out. Again, many thanks. ;3
    – Nick Mischler Apr 19 '13 at 08:31