-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgs-loader.js
executable file
·123 lines (98 loc) · 2.68 KB
/
gs-loader.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
var loadGS = (function () {
"use strict";
var jsonpcount = 0;
var sheets = {};
// Get AJAX using jsonp
function getSheetsJsonp(url) {
var callbackName = "__jsonpcallback" + jsonpcount++;
var promise = new Promise(function (resolve, reject) {
window[callbackName] = function jsonpCallback(data) {
resolve(data.feed.entry);
delete window[callbackName];
};
var script = document.createElement("script");
script.src = url + "?alt=json-in-script&callback=" + callbackName;
script.addEventListener("load", function () {
this.parentNode.removeChild(this);
}, false);
script.addEventListener("error", function (eventData) {
this.parentNode.removeChild(this);
delete window[callbackName];
reject(eventData);
}, false);
document.head.appendChild(script);
});
return promise;
}
function getContents(data) {
if ( !data ) {
return {};
}
// Step 1: Get all the data as a two-dimensional array
var cols = [];
data.forEach(function (cell) {
var cellInfo = cell.gs$cell;
var colIndex = cellInfo.col - 1;
var rowIndex = cellInfo.row - 1;
var text = cellInfo.$t;
if ( !text ) {
return;
}
var col = cols[colIndex] = cols[colIndex] || [];
col[rowIndex] = text;
});
// Step 2: Transform data into an object and clean it up
var lists = {};
cols.forEach(function (col) {
var header = col.shift();
if ( !header ) { return; }
lists[header] = col;
// Clean out empty values
var i;
while ( i < col.length ) {
if ( col[i] ) {
i++;
} else {
col.splice(i, 1);
}
}
});
return lists;
}
function getWorksheets(id) {
var url = "https://spreadsheets.google.com/feeds/worksheets/" + id + "/public/full";
var worksheetPromises = [];
var worksheets = {};
var contents = {};
// Step 1: Get a list of all the worksheets in the spreadsheet
return getSheetsJsonp(url).then(function (data) {
data.forEach(function (worksheet) {
var name = worksheet.title.$t;
var ws = worksheets[name] = {};
// Step 2: For each worksheet, parse its listfeed
worksheet.link.some(function (link) {
if ( link.rel.match(/cellsfeed/) ) {
worksheetPromises.push(
getSheetsJsonp(link.href).then(function (data) {
worksheets[name] = getContents(data);
})
);
}
});
});
return Promise.all(worksheetPromises).then(function () {
return worksheets;
});
});
}
// Cache results for each id
function load(id) {
sheets[id] = sheets[id] || getWorksheets(id);
sheets[id].catch(function () {
// If there's an error, remove the cache so we can try again
delete sheets[id];
});
return sheets[id];
}
return load;
}());