Coder Social home page Coder Social logo

sheetrock's Introduction

Sheetrock

Build status NPM version

Sheetrock is a JavaScript library for querying, retrieving, and displaying data from Google Sheets. In other words, use a Google spreadsheet as your database! Load entire worksheets or leverage SQL-like queries to sort, group, and filter data. All you need is the URL of a public Google Sheet.

Sheetrock can be used in the browser or on the server (Node.js). It has no dependencies—but if jQuery is available, it will register as a plugin.

Basic retrieval is a snap but you can also:

  • Query sheets using the SQL-like Google Visualization query language (filters, pivots, sorting, grouping, and more)

  • Lazy-load large data sets (infinite scroll with ease)

  • Easily mix in your favorite templating system (Handlebars, Underscore, etc.)

  • Customize to your heart’s content with your own callbacks

Browser

Grab the latest version of Sheetrock for your project. Here’s an example request (using jQuery):

<table id="my-table"></table>
<script src="jquery.min.js"></script>
<script src="sheetrock.min.js"></script>
$("#my-table").sheetrock({
  url: "https://docs.google.com/spreadsheets/d/1qT1LyvoAcb0HTsi2rHBltBVpUBumAUzT__rhMvrz5Rk/edit#gid=0",
  query: "select A,B,C,D,E,L where E = 'Both' order by L desc"
});

For many more examples and accompanying jsFiddles, visit chriszarate.github.io/sheetrock.

Server

Sheetrock can also be used with Node.js:

npm install sheetrock
var sheetrock = require('sheetrock');

var myCallback = function (error, options, response) {
  if (!error) {
    /*
      Parse response.data, loop through response.rows, or do something with
      response.html.
    */
  }
};

sheetrock({
  url: "https://docs.google.com/spreadsheets/d/1qT1LyvoAcb0HTsi2rHBltBVpUBumAUzT__rhMvrz5Rk/edit#gid=0",
  query: "select A,B,C,D,E,L where E = 'Both' order by L desc",
  callback: myCallback
});

Version 1.0

In version 1.0, Sheetrock has introduced a few backwards-incompatible changes, although most basic requests will still work. These changes make it simpler to use; read the options below or the CHANGELOG for more details.

The previous 0.3.x branch is still available and maintained.

Expectations

Sheetrock is designed to work with any Google Sheet, but makes some assumptions about the format and availability.

  • Public. In order for others to access the data in your Sheet with Sheetrock, the Sheet must be public. (How do I make a spreadsheet public?) It is possible to use Sheetrock to access a private Sheet for your own use if you are logged in to your Google account in the same browser session, but this is not a supported use case.

  • One header row. Sheetrock expects a single header row of column labels in the first row of the Sheet. Any other configuration (e.g., no header row, multiple or offset header rows) can cause problems with the request and complicates templating. The header row values are used as keys in the cell object unless you override them using the labels option.

  • Plain text. Sheetrock doesn’t handle formatted text. Any formatting you’ve applied to your data—including hyperlinks—probably won’t show up.

Options

Sheetrock expects a hash map of options as a parameter, e.g.:

sheetrock({/* options */});

Your options override Sheetrock’s defaults on a per-request basis. You can also globally override defaults like this:

sheetrock.defaults.url = "https://docs.google.com/spreadsheets/d/1qT1LyvoAcb0HTsi2rHBltBVpUBumAUzT__rhMvrz5Rk/edit#gid=0";

url

  • Expects string

The URL of a public Google Sheet. (How do I make a spreadsheet public?) Make sure you include the #gid=X portion of the URL; it identifies the specific worksheet you want to use. If you want to access data from multiple worksheets, you will need to make multiple Sheetrock requests.

query

  • Expects string
  • Renamed from sql in 1.0.0

A Google Visualization API query string. Use column letters in your queries (e.g., select A,B,D).

target

  • Expects DOM element

A DOM element that Sheetrock should append HTML output to. In a browser, for example, you can use document.getElementById to reference a single element. If you are using Sheetrock with jQuery, you can use the jQuery plugin syntax (e.g., $('#my-table').sheetrock({/* options */})) and ignore this option.

When data has been loaded and markup appended to the target element, a custom sheetrock:loaded event will be triggered on the element. You can listen for this event if you'd like to perform an action after the DOM is updated:

$('#my-table')
  .sheetrock({/* options */})
  .on('sheetrock:loaded', function () {
    /* do something */
  });

fetchSize

  • Expects non-negative integer
  • Renamed from chunkSize in 1.0.0

Use this option to load a portion of the available rows. When set to 0 (the default), Sheetrock will fetch all available rows. When set to 10, it will fetch ten rows and keep track of how many rows have been requested. On the next request with the same query, it will pick up where it left off.

labels

  • Expects array of strings

Override the returned column labels with an array of strings. Without this option, if you use your own rowTemplate, you must reference column labels exactly as they are returned by Google’s API. If your sql query uses group, pivot, or any of the [manipulation functions][manip], you will notice that Google’s returned column labels can be hard to predict. In those cases, this option can prove essential. The length of this array must match the number of columns in the returned data.

rowTemplate

  • Expects function
  • Renamed from rowHandler in 1.0.0

By default, Sheetrock will output your data in simple HTML. Providing your own row template is an easy way to customize the formatting. Your function should accept a row object. A row object has four properties:

  • num: The row number (header = 0, first data row = 1, and so on).

  • cells: An object with properties named after the column labels from your header row or the labels option.

  • cellsArray: An array of values that matches the column order of your Sheet or your query option. Provided as an alternative to the cells object.

  • labels: An array of column labels in the same order as cellsArray that match the properties of the cells object.

Your function should return a DOM object or an HTML string that is ready to be appended to your target element. A very easy way to do this is to provide a compiled Handlebars or Underscore template (which is itself a function).

callback

  • Expects function
  • Renamed from userCallback in 1.0.0

You can provide a function to be called when all processing is complete. The function will be passed the following parameters, in this order:

  • Error (object): If the request failed, this parameter will be a JavaScript error; otherwise, it will be null. Always test for an error before using the other parameters.

  • Options (object): An object representing the options of the request. The user property will contain the options you originally provided (useful for identifying which request the callback is for) and a request property with information about the HTTP request to Google’s API.

  • Response (object): An object containing response data properties:

    • .attributes (object): An object containing useful information about the response data, its structure, and its format.

    • .raw (object): This is the raw response data from Google’s API.

    • .rows (array): An array of row objects (which are also passed individually to the rowTemplate, if one is provided).

    • .html (string): A string of HTML representing the final presentational output of the request (which is also appended to the target or jQuery reference, if one was provided).

reset

  • Expects Boolean

Reset request status. By default, Sheetrock remembers the row offset of a request, whether a request has been completely loaded already, or if it previously failed. Set to true to reset these indicators. This is useful if you want to reload data or load it in another context.

Caching

On large spreadsheets (~5,000 rows), the performance of Google’s API when using the query option can be sluggish and, in some cases, can severely affect the responsiveness of your application. At this point, consider caching the responses for reuse via a callback function.

Tips and troubleshooting

The best first step to troubleshooting problems with Sheetrock is to use a callback function to inspect any errors and response data. Here’s a simple example that logs all returned data to the console:

sheetrock({
  /* options */
  callback: function (error, options, response) {
    console.log(error, options, response);
  }
});

Projects using Sheetrock

Tell me about your project on the Wiki!

Change log

See CHANGELOG.md.

Credits and license

Sheetrock was written by Chris Zarate. It was inspired in part by Tabletop.js (which will teach you jazz piano). John Brecht came up with the name. Sheetrock is released under the MIT license.

sheetrock's People

Contributors

chriszarate avatar evenloooo avatar papoms avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sheetrock's Issues

Import SINGLE Cell?

I'm having the worst time understanding how to import a SINGLE cell's text. I want to do this at various locations around my page, but not next to the rest of the data and not in any sort of grouping. I literally want to display just the text from one single cell at a time. Can you please help me figure out how to do that? :-(

minor bug...

Sorry about this but now displaying a table doesn't show any headers (same html as previously) :)

How to set new Google Spreadsheet Row 1 as default Table Head Data

Hello - first of all this is a "schoolboy error" query.

I can not work out how to set in a new google spreadsheet the first row to be the table header. All I get is A, B, C, etc in web page

This is my code to not show A, B, C, in web table header row :

$('#programme').sheetrock({
url: mySpreadsheet,
sql: "select A,B,C",
headersOff: true
});

What I can not work out is how the sample baseball spreadsheet achieved the first row displaying as the Table Header

The documentation on Columns and Labels just confuses me

Assistance would be greatly appreciated
thanks, Col

PS: Want to keep this GS-to-Table simple and not use any Templating

using the data in a custom dataHandler

Hi again,

I'm trying to use sheetrock to load up some data that I want to reuse again later in the page. It's possible I'm still misunderstanding something important (my javascript is admittedly weak), but I think it's accurate to say that the values I assign under my custom dataHandler function don't survive even when I assign them to a global scope.

I couldn't figure out how to make a jsfiddle that recreates my situation, but the essence is something like this:

window.teamOne = new Object();
teamOne.img = "kitty.jpg";
$("#placeholder").empty().data('sheetrockLoaded', false).sheetrock({
    [...]
    dataHandler : myDataHelper
});

function myDataHelper (data){

    teamOne.img = "team.jpg";

}

console.log(teamOne.img); // "kitty.jpg"

And maddeningly enough, here's the result for that complete object in the same scope:

console.log(teamOne); // Object { img : "team.jpg" }

So it's like the object gets assigned its new properties, but they go away if I try to actually use them.

Again, my javascript is weak so it's probably that I'm misunderstanding something pretty basic about how scope works. But then I didn't get the same problem using non-sheetrock-involved functions, so I thought I'd ask.

Can you tell what's going on here?

Pull data from protected Google Spreadsheets

Is there a reason non-public spreadsheets are not supported?

If it's not a ton of work and if the project maintainers are interested in it, I was going to spend some time putting together a pull request to add this functionality.

I haven't looked at the code or Google API much yet, so let me know if I'm missing something big.

Row offset should be cached during options validation

Right now, row offset is cached after options validation in an asynchronous function. As a result, multiple immediate calls to Sheetrock using the same query do not reflect the correct row offset, e.g.:

var options = {
  [...],
  chunkSize: 5
};

$('#el').sheetrock(options);  // offset: 5
$('#el').sheetrock(options);  // offset: 5 :(

404 response from Google

Hi Chris,

Trying out a new sheet, and google is coming back with a 404 when sheetrock sends in its query. I haven't switched to "new" sheets yet (I don't think), but this spreadsheet does have the newer url construction like /spreadsheets/d//edit?... Instead of the familiar /spreadsheet/ccc/key=/edit....

Here's the URL that Sheetrock was looking for. And I did just upgrade to v0.1.10.

Is this an artifact of the new spreadsheets rollout? Do I need to do something on the google side to work around it?

Zach

Cells and data formats

Hi
Thanks a bunch for a easy to use script and a quick setup.
I have a question if anyone have managed to retrieve a cells data-format type.

For instance in the "G" column in Spreadsheet I use percentage format but it displays as decimal formated numbers when I use sheetrock in HTML.

Is there a way to keep the google cell formats or a simple way to convert them back to percentage?

I have created a example here where I can't get the column "G" to swow the numbers in %:
http://jsfiddle.net/xhmavwxf/14/

The public spreadsheet can be found here:
https://docs.google.com/spreadsheets/d/1d0wHdIXXHI_rfXY-Fqi0ehIVDuUVxGHfTO8sIZG4UOk/edit

I'm quite new to code so any help would be appreciated.

— Daniel

How to show value '0' when cell value in the spreadsheet equals to 0?

Hi,

Thanks for sharing this plugins.

I have a question if anyone can show the value '0' when the cells in the spreadsheet hold value 0. Take the "Hello, world" example in your website http://chriszarate.github.io/sheetrock/. When the value of cell in the spreadsheet equals to zero (for example, the value at row 11 and column I equals to zero in the spreadsheet 1986 NL Batting), it shows nothing in the table after using sheetrock. How can I show the value zero instead of nothing?
Really appreciate your help.

Elaine

Will Sheetrock work as a Github Page?

Sheetrock is such a useful tool. Thanks for making it available. Will it work as a Github Pages site? I've managed to get some of the examples working locally on Jekyll, but not others, and I know Pages have some restrictions to stand-alone Jekyll.

Trailing Zeros & Plain Text Formatting

I'm using Sheetrock to create a menu - however when I specify an even dollar amount (e.g. $1.00) the trailing "00" are removed. Formatting the cells as "Plain Text" would fix this, however it kills the entire import when I set these cells as plain text. Any advice? Thanks!

Select Worksheet

Hi there,
just found your library and it seems to be doing exactly what I need, apart from the fact that I do not see an option to select a certain worksheet.
How it would make sense to me would be something like

select A, B, C from Sheet2
Is that somehow currently possible? Or possible to add?

Formatting Time in Sheetrock output

Sheetrock is awesome Chris; thanks for making it available.

I'm struggling to get formatted times converted too. I checked out the Gist you linked to in a previous Issue, but how do I wrangle that into Sheetrock (which I've also wrangled into http://mixture.io)? I've set up a JSFiddle showing how the time is being output. I'd like to output it in the same format it is entered into the Google spreadsheet, which is 9:00 am, and 5:00 pm, etc.

http://jsfiddle.net/4UWy9/3/

One to Many?

Aloha Chris and community,

Column A is 'Author Last Name'
Column B is 'Book Title'

There are 3 Authors with 6 Titles each

Any ideas on how to display it as this?

Author's 1 Last Name
• Title of Book 1
• Title of Book 2
• Title of Book 3
• Title of Book 4
• Title of Book 5
• Title of Book 6

Author's 2 Last Name
• Title of Book 1
• Title of Book 2
• Title of Book 3
• Title of Book 4
• Title of Book 5
• Title of Book 6

Author's 3 Last Name
• Title of Book 1
• Title of Book 2
• Title of Book 3
• Title of Book 4
• Title of Book 5
• Title of Book 6

Many thanks for any feedback - and happy holidays

jsfiddle isn't loading sheetrock js

The jsFiddle examples are awesome, but didn't work for me.

I went in to edit, and see that it is getting a 404 on
http://fiddle.jshell.net/7Gzjj/show/chriszarate.github.io/sheetrock/src/jquery.sheetrock.min.js

I removed that external reference and just set it to
http://chriszarate.github.io/sheetrock/src/jquery.sheetrock.min.js
and it worked.

Very cool stuff by the way!
I plan to use this for my students that are moving from basic web design to wanting a simple persistent store for their basic app. (todo/shopping list, simple quiz game, leader board, etc)

Two Questions On Usage

All the examples seem to dump the data in to tables, etc.

How would I query my table, find a row that matches the select criteria, and output the value of ONE or two cells into a span?

Secondly, is there a way to get a row count for the number of rows that match the select criteria?

Question: Can I SQL query Sheetrock using a JS date variable?

Hi Chris. I've a question, not an issue.

I'm wondering if I query Sheetrock based on today's date?

My columns in Google Sheet are named by day, so if today is Saturday, can I query Sheetrock sql: "[...] where Saturday contains ''X"?

Getting the date via JS is straightforward, but I don't know how to write that into the Sheetrock sql call.

Unable to read data??

Can you please help me look at it?
Why do I follow the example, the result is no information?
http://jsfiddle.net/szT5P/14/

In addition, the sample is chunkSize: 5, that is, 1-5, 6-10 to ask to read how to set?

If I want to return a total number of how much data, how to deal with it?

Update data without refreshing the page?

Thanks for Sheetrock! I'm not sure if this is obvious or an irrelevant question (I'm new to web dev), but how would I go about getting the data asynchronously?

When I edit and save my Google Spreadsheet, I have to refresh my browser to see the changes in the table. Is there a way to get the changes displayed in the table automatically, without refreshing the page? Sorry if this is asking too much or doesn't apply to Sheetrock.

Sheetrock does nothing in the browser

<html>
<head>
<script src="jquery.min.js"></script>
<script src="jquery.sheetrock.min.js"></script>
<script language="javascript" type="text/javascript">

// Define spreadsheet URL.
var mySpreadsheet = 'https://docs.google.com/spreadsheet/ccc?key=0AiuPNF4c6L2KdF9OQkc5U1pTR3Q3Q1JpdHc5NWR4R2c#gid=0';

// Load an entire sheet.
$('#statistics').sheetrock({
   url: mySpreadsheet
});


</script>
<style>
</style>
</head>
<body>
<table id="statistics"></table>
</body>
</html>

[07:23:57.838] SyntaxError: Using //@ to indicate source map URL pragmas is deprecated. Use //# instead @ file:///home/hendrikus/sheetrock/jquery.min.js:1
[07:23:57.870] "No element targeted or data handler provided."

Usage question and updating data on the fly

Hi,

First of all, sheetrock is amazing. I'm loving it. I just have run into something I can't figure out how to do, and since I didn't see it in the documentation, I thought I'd ask here. Hopefully that's OK. It's probably something fundamental that I'm misunderstanding, but maybe not.

What I'm trying to do is have some data in a sheetrock object in my DOM that changes based on other things that go on. Like, hit a button to select a player's name, and then "#playerdata" (in my case) gets updated with new contents based on a new query that selects for that player's name.

Works fine the first time through, but the second time triggers the "No more rows to load!" console message.

Is it possible to unload the first query and reattach it to the same #playerdata? Or should I be thinking of this differently altogether?

I thought about chunking, but if I'm not mistaken that would depend on knowing the row order, which wouldn't help if I'm trying to pull out unique record.

Thanks,
Zach

sheetrock and YUI compressor

I am a novice so disregard if this doesn't make any sense.
I use sheetrock.js on Appery.io to build a mobile app. Appery uses YUI compressor to minimize javascript and I just recently started having issues. I got a "missing name after . operator" error b/c YUI Compressor doesn't like the use of reserved words. I was able to fix the issue by changing "new" to "new2" in lines 62 and and 624.

Guidance on how to access a private Google spreadsheet

Thanks for sharing this really powerful tool. It's almost exactly what I'm looking for. I'd like to be able to use Sheetrock to access a private Google Spreadhseet rather than a public one.

Any guidance on how to go about doing this, or whether this is already something on your radar, would be much appreciated. Thanks in advance.

Styling

Hi, thanks for the convenient code.
How do I style the table with css?
I know little about jquery and javascript :(.

Override table header without needing to write entire data handler?

I'm looking to add some custom classes to certain headers (to simplify some features when using Sheetrock with TableSorter). From what I understand of the Sheetrock documentation, I can override rows with rowHandler, but I don't see an equivalent for the headers.

To add those custom classes, do I need to override using dataHandler?

New Google Sheets?

Is there a way to make sheetrock.js work with the new versions of Google Spreadsheets? It seems that the API, URL scheme, publishing to the web, etc. have changed. There is a vague note about this in the release announcement on the Google Drive blog.

When I try to use Sheetrock.js with a new spreadsheet, I get an error about the location. (I don't recall the exact wording; I switched back to the old sheets for now.)

EDIT: Here's the actual list of removed features.

Fix use of jQuery promises.

Because I missed the point of promises, my use of them in Sheetrock is ineffectual.

My intention was to use promises to chain Google API calls (i.e., send requests one at a time, in sequence). There were two good reasons for this:

  1. Prevent race conditions when prefetching column labels.
  2. Avoid overburdening the API, which is already a tad pokey.

However, my implementation of promises didn't cut the mustard. This is a relatively easy and transparent fix, coming soon.

New API yet?

Hi, I tried looking around to see if a new API was created for Speadsheets yet. I found info but no dates, so I do not know if what I found is valid for Sheetrock? Do you know of an update yet?

Occasional bug response

Hi,

Sorry this is going to be a vague issue report but I wanted to get this down while I've noticed it. (I'm writing this in a meeting but I'll update with actual code later, assuming I can reproduce it.)

Thing is, sometimes I get an error from the sheetrock callback function as it connects to the spreedsheet. The log reports an invalid token, "<", which suggests to me that sheetrock is expecting JSON and getting HTML.

The spreedsheet is set to public and is also publishing to the web.

The error stopped and the sheet loaded normally after I logged in to google, and it stayed clear after I logged out. So I think what may be happening in that initial request is that Google knows who I am, but needs me to reauthenticate, and that reauthentication action is stepping into the Sheetrock callback. If so, that's not necessarily Sheetrock's problem, but I thought it would be worth mentioning.

It happened just now running Chromium 18.0.1025.151 (Developer Build 130497 Linux) Ubuntu 12.04, in case that matters.

Anyway, like I said I need to do some more testing and isolation when I get back to my office. Sorry for the vague issue report.

Zach

bug

Just an FYI there's a bug here:

// Get column labels from returned data.
var _labels = function(col) {
return (_has(obj, 'label')) ? col.label.replace(/ /g, '') : col.id;
}

Sometimes 'value' is null when no data is in a cell

I have a spreadsheet where some cells are blank, and this causes an uncaught error in sheetrock. I added to line 420 in jquery.sheetrock.js:

if (value == null) value = '';

...and this catches the null value before it gets passed to jquery. Really, this should have been caught earlier but I don't see why it wasn't. (On Chrome Version 36.0.1985.125)

Data Back as JSON?

I was wondering if it is possible to get the result back as a JSON array rather than formatted HTML and not display the result until I have manipulated it with the dataHandler:

This is what I have been experimenting with. Being a JavaScript novice I certainly appreciate any assistance. Thanks

    $("#topics").sheetrock({
            url: mySpreadsheet,
            sql: "select  A, B, C, MAX(D) group by A,B,C order by A asc, B asc, C asc",
            formatting: false,
            dataHandler: theData(this)
        });
    });
    function theData(theData) {
       //Do some work with the JSON here
    }

How to run a javascript function after the spreadsheet is loaded?

Dear Chris,

Will u please tell me how to run a javascript function only after the spreadsheet is loaded? i tried userCallback option. But it is not working. I wanted to show a div only if there is only content from google spreadsheet. If there is no content in that div I want to hide? How I will do?

Regards

Raman

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.