-
Notifications
You must be signed in to change notification settings - Fork 154
/
google-spreadsheet.coffee
86 lines (72 loc) · 2.73 KB
/
google-spreadsheet.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
###
Updated versions can be found at https://github.com/mikeymckay/google-spreadsheet-javascript
###
class GoogleUrl
constructor: (@sourceIdentifier) ->
if (@sourceIdentifier.match(/http(s)*:/))
@url = @sourceIdentifier
try
@key = @url.match(/key=(.*?)&/)[1]
catch error
@key = @url.match(/(cells|list)\/(.*?)\//)[2]
else
@key = @sourceIdentifier
@jsonCellsUrl = "https://spreadsheets.google.com/feeds/cells/" + @key + "/od6/public/basic?alt=json-in-script"
@jsonListUrl = "https://spreadsheets.google.com/feeds/list/" + @key + "/od6/public/basic?alt=json-in-script"
@jsonUrl = @jsonCellsUrl
class GoogleSpreadsheet
load: (callback) ->
url = @googleUrl.jsonCellsUrl + "&callback=GoogleSpreadsheet.callbackCells"
$('body').append("<script src='" +url+ "'/>")
jsonUrl = @jsonUrl
safetyCounter = 0
waitUntilLoaded = ->
result = GoogleSpreadsheet.find({jsonUrl:jsonUrl})
if safetyCounter++ > 20 or (result? and result.data?)
clearInterval(intervalId)
callback(result)
intervalId = setInterval( waitUntilLoaded, 200)
result if result?
url: (url) ->
this.googleUrl(new GoogleUrl(url))
googleUrl: (googleUrl) ->
throw "Invalid url, expecting object not string" if typeof(googleUrl) == "string"
@url = googleUrl.url
@key = googleUrl.key
@jsonUrl = googleUrl.jsonUrl
@googleUrl = googleUrl
save: ->
localStorage["GoogleSpreadsheet."+@key] = JSON.stringify(this)
GoogleSpreadsheet.bless = (object) ->
result = new GoogleSpreadsheet()
for key,value of object
result[key]=value
result
GoogleSpreadsheet.find = (params) ->
try
for item of localStorage
if item.match(/^GoogleSpreadsheet\./)
itemObject = JSON.parse(localStorage[item])
for key,value of params
if itemObject[key] == value
return GoogleSpreadsheet.bless(itemObject)
# Need this to handle differences in localStorage between chrome and firefox TODO make dry
catch error
for item in localStorage
if item.match(/^GoogleSpreadsheet\./)
itemObject = JSON.parse(localStorage[item])
for key,value of params
if itemObject[key] == value
return GoogleSpreadsheet.bless(itemObject)
return null
GoogleSpreadsheet.callbackCells = (data) ->
googleUrl = new GoogleUrl(data.feed.id.$t)
googleSpreadsheet = GoogleSpreadsheet.find({jsonUrl:googleUrl.jsonUrl})
if googleSpreadsheet == null
googleSpreadsheet = new GoogleSpreadsheet()
googleSpreadsheet.googleUrl(googleUrl)
googleSpreadsheet.data = (cell.content.$t for cell in data.feed.entry)
googleSpreadsheet.save()
googleSpreadsheet
### TODO (Handle row based data)
GoogleSpreadsheet.callbackList = (data) ->