Last active
August 29, 2015 13:55
-
-
Save ltk/8760902 to your computer and use it in GitHub Desktop.
Sheeeet | A work-in-progress Google Spreadsheet library for javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Pronounced like http://shiiiit.com/ | |
var Sheeeet = (function(_) { | |
var cells = []; | |
function Cell(data) { | |
this.content = data.content; | |
this.row = data.row; | |
this.column = data.col; | |
this.location = [this.column, this.row]; | |
this.id = 'c' + this.column + 'r' + this.row; | |
this.update = function (params) { | |
for (key in params) { | |
this[key] = params[key]; | |
} | |
} | |
cells.push(this); | |
} | |
function HtmlTable(headers, rows, options) { | |
this.headers = headers; | |
this.rows = rows; | |
this.options = options || {}; | |
this.toString = function() { | |
var html = '<table><thead><tr>'; | |
_.each(headers, function(header) { | |
html += '<th>' + header.content + '</th>'; | |
}); | |
html += '</tr></thead><tbody>'; | |
_.each(rows, function(row) { | |
html += '<tr>'; | |
_.each(row, function(cell) { | |
html += '<td>' + cell.content + '</td>'; | |
}); | |
html += '</tr>'; | |
}); | |
html += '</tbody></table>'; | |
return html; | |
} | |
} | |
function column(columnNumber) { | |
return _cellsWhere({column: columnNumber}, {sortBy: 'row'}); | |
} | |
function row(rowNumber) { | |
return _cellsWhere({row: rowNumber}, {sortBy: 'column'}); | |
} | |
function cellAt(coordinates) { | |
if ( Object.prototype.toString.call( coordinates ) === '[object Array]' ) { | |
return _findCellByArrayCoordinates(coordinates); | |
} else if (typeof coordinates === 'object') { | |
return _findCellByObjectCoordinates(coordinates); | |
} | |
} | |
function _cellsWhere(properties, options) { | |
var sortBy = options.sortBy; | |
var list = _.where(cells, properties); | |
if (sortBy) { | |
return _.sortBy(list, function(cell) { | |
return cell[sortBy]; | |
}); | |
} else { | |
return list; | |
} | |
} | |
function _findCellByObjectCoordinates(coordinates) { | |
var column, row; | |
column = coordinates.column || coordinates.col || coordinates.c; | |
row = coordinates.row || coordinates.r; | |
if (column && row) { | |
return _findCellByArrayCoordinates([column, row]); | |
} | |
} | |
function _findCellByArrayCoordinates(coordinates) { | |
var column, row; | |
column = coordinates.shift(); | |
row = coordinates.shift(); | |
if (column && row) { | |
return _findCellByCoordinates(column, row); | |
} | |
} | |
function _findCellByCoordinates(column, row) { | |
return _.findWhere(cells, {column: column, row: row}); | |
} | |
function _fillGaps() { | |
for (i = rowStart(); i <= rowCount(); i++) { | |
for (j = columnStart(); j <= columnCount(); j++) { | |
var cell = cellAt([j,i]); | |
if (typeof cell === 'undefined') { | |
var cell_data = { | |
col: j, | |
row: i, | |
content: '' | |
} | |
new Cell(cell_data); | |
} | |
} | |
} | |
} | |
function columnCount() { | |
return _.max(cells, function(cell) { return cell.column }).column; | |
} | |
function rowCount() { | |
return _.max(cells, function(cell) { return cell.row }).row; | |
} | |
function columnStart() { | |
return _.min(cells, function(cell) { return cell.column }).column; | |
} | |
function rowStart() { | |
return _.min(cells, function(cell) { return cell.row }).row; | |
} | |
function toHtml(options) { | |
var headers, rows; | |
_fillGaps(); | |
headers = row(rowStart()); | |
rows = []; | |
for (i = rowStart() + 1; i <= rowCount(); i++) { | |
rows.push(row(i)); | |
} | |
return new HtmlTable(headers, rows, options).toString(); | |
} | |
return { | |
Cell: Cell, | |
cells: cells, | |
row: row, | |
column: column, | |
cellAt: cellAt, | |
columnCount: columnCount, | |
rowCount: rowCount, | |
columnStart: columnStart, | |
rowStart: rowStart, | |
toHtml: toHtml | |
} | |
})(_); | |
var cell_1 = new Sheeeet.Cell({ row: 1, col: 1, content: 'Cell 1 Here' }); | |
var cell_2 = new Sheeeet.Cell({ row: 1, col: 2, content: 'Cell 2 Here' }); | |
var cell_3 = new Sheeeet.Cell({ row: 1, col: 3, content: 'Cell 3 Here' }); | |
var cell_4 = new Sheeeet.Cell({ row: 2, col: 1, content: 'Cell 4 Here' }); | |
var cell_5 = new Sheeeet.Cell({ row: 2, col: 2, content: 'Cell 5 Here' }); | |
var cell_6 = new Sheeeet.Cell({ row: 2, col: 3, content: 'Cell 6 Here' }); | |
var cell_7 = new Sheeeet.Cell({ row: 3, col: 1, content: 'Cell 7 Here' }); | |
var cell_8 = new Sheeeet.Cell({ row: 3, col: 2, content: 'Cell 8 Here' }); | |
var cell_9 = new Sheeeet.Cell({ row: 3, col: 3, content: 'Cell 9 Here' }); | |
var cell_10 = new Sheeeet.Cell({ row: 6, col: 24, content: 'Cell 10 Here' }); | |
console.log('Cell 5:'); | |
console.log(cell_5); | |
console.log('Cell 6 Location:'); | |
console.log(cell_6.location); | |
console.log('Sheeeet cells:'); | |
console.log(Sheeeet.cells); | |
console.log('Sheeeet cells in row 3:'); | |
console.log(Sheeeet.row(3)); | |
console.log('Sheeeet cells in column 1:'); | |
console.log(Sheeeet.column(1)); | |
console.log('Sheeeet cellAt [1,3]:'); | |
console.log(Sheeeet.cellAt([1,3])); | |
console.log('Sheeeet cellAt {r:2, c:3}:'); | |
console.log(Sheeeet.cellAt({r:2, c:3})); | |
console.log('Updating cell 1,3'); | |
Sheeeet.cellAt([1,3]).update({content: 'Updated Content! Woo hoo!'}); | |
console.log(Sheeeet.cellAt([1,3])); | |
var headers = Sheeeet.row(1); | |
_.each(headers, function(header) { | |
console.log(header.content); | |
}); | |
console.log('Row Count:'); | |
console.log(Sheeeet.rowCount()); | |
var tableHtml = Sheeeet.toHtml({ headers: { row: 1 } }); | |
$('body').append(tableHtml); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment