-
Notifications
You must be signed in to change notification settings - Fork 154
/
google-spreadsheet.js
126 lines (126 loc) · 4 KB
/
google-spreadsheet.js
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
Updated versions can be found at https://github.com/mikeymckay/google-spreadsheet-javascript
*/var GoogleSpreadsheet, GoogleUrl;
GoogleUrl = (function() {
function GoogleUrl(sourceIdentifier) {
this.sourceIdentifier = sourceIdentifier;
if (this.sourceIdentifier.match(/http(s)*:/)) {
this.url = this.sourceIdentifier;
try {
this.key = this.url.match(/key=(.*?)&/)[1];
} catch (error) {
this.key = this.url.match(/(cells|list)\/(.*?)\//)[2];
}
} else {
this.key = this.sourceIdentifier;
}
this.jsonCellsUrl = "http://spreadsheets.google.com/feeds/cells/" + this.key + "/od6/public/basic?alt=json-in-script";
this.jsonListUrl = "http://spreadsheets.google.com/feeds/list/" + this.key + "/od6/public/basic?alt=json-in-script";
this.jsonUrl = this.jsonCellsUrl;
}
return GoogleUrl;
})();
GoogleSpreadsheet = (function() {
function GoogleSpreadsheet() {}
GoogleSpreadsheet.prototype.load = function(callback) {
var intervalId, jsonUrl, safetyCounter, url, waitUntilLoaded;
url = this.googleUrl.jsonCellsUrl + "&callback=GoogleSpreadsheet.callbackCells";
$('body').append("<script src='" + url + "'/>");
jsonUrl = this.jsonUrl;
safetyCounter = 0;
waitUntilLoaded = function() {
var result;
result = GoogleSpreadsheet.find({
jsonUrl: jsonUrl
});
if (safetyCounter++ > 20 || ((result != null) && (result.data != null))) {
clearInterval(intervalId);
return callback(result);
}
};
intervalId = setInterval(waitUntilLoaded, 200);
if (typeof result != "undefined" && result !== null) {
return result;
}
};
GoogleSpreadsheet.prototype.url = function(url) {
return this.googleUrl(new GoogleUrl(url));
};
GoogleSpreadsheet.prototype.googleUrl = function(googleUrl) {
if (typeof googleUrl === "string") {
throw "Invalid url, expecting object not string";
}
this.url = googleUrl.url;
this.key = googleUrl.key;
this.jsonUrl = googleUrl.jsonUrl;
return this.googleUrl = googleUrl;
};
GoogleSpreadsheet.prototype.save = function() {
return localStorage["GoogleSpreadsheet." + this.type] = JSON.stringify(this);
};
return GoogleSpreadsheet;
})();
GoogleSpreadsheet.bless = function(object) {
var key, result, value;
result = new GoogleSpreadsheet();
for (key in object) {
value = object[key];
result[key] = value;
}
return result;
};
GoogleSpreadsheet.find = function(params) {
var item, itemObject, key, value, _i, _len;
try {
for (item in localStorage) {
if (item.match(/^GoogleSpreadsheet\./)) {
itemObject = JSON.parse(localStorage[item]);
for (key in params) {
value = params[key];
if (itemObject[key] === value) {
return GoogleSpreadsheet.bless(itemObject);
}
}
}
}
} catch (error) {
for (_i = 0, _len = localStorage.length; _i < _len; _i++) {
item = localStorage[_i];
if (item.match(/^GoogleSpreadsheet\./)) {
itemObject = JSON.parse(localStorage[item]);
for (key in params) {
value = params[key];
if (itemObject[key] === value) {
return GoogleSpreadsheet.bless(itemObject);
}
}
}
}
}
return null;
};
GoogleSpreadsheet.callbackCells = function(data) {
var cell, googleSpreadsheet, googleUrl;
googleUrl = new GoogleUrl(data.feed.id.$t);
googleSpreadsheet = GoogleSpreadsheet.find({
jsonUrl: googleUrl.jsonUrl
});
if (googleSpreadsheet === null) {
googleSpreadsheet = new GoogleSpreadsheet();
googleSpreadsheet.googleUrl(googleUrl);
}
googleSpreadsheet.data = (function() {
var _i, _len, _ref, _results;
_ref = data.feed.entry;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
cell = _ref[_i];
_results.push(cell.content.$t);
}
return _results;
})();
googleSpreadsheet.save();
return googleSpreadsheet;
};
/* TODO (Handle row based data)
GoogleSpreadsheet.callbackList = (data) ->*/