Skip to content

Commit b530df2

Browse files
author
Sam
committed
Initial commit.
0 parents  commit b530df2

3 files changed

Lines changed: 205 additions & 0 deletions

File tree

node-google-spreadsheets/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
WIP.
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
var request = require("request");
2+
var xml2js = require("xml2js");
3+
var http = require("http");
4+
var querystring = require("querystring");
5+
6+
var FEED_URL = "https://spreadsheets.google.com/feeds/";
7+
8+
var getFeed = function(params, auth, query, cb) {
9+
var headers = {};
10+
var visibility = "public";
11+
var projection = "values";
12+
13+
if(auth) {
14+
headers.Authorization = "GoogleLogin auth=" + auth;
15+
visibility = "private";
16+
projection = "full";
17+
}
18+
params.push(visibility, projection);
19+
20+
var url = FEED_URL + params.join("/");
21+
if(query) {
22+
url += "?" + querystring.stringify(query);
23+
}
24+
25+
request.get({
26+
url: url,
27+
headers: headers
28+
}, function(err, response, body) {
29+
if(response.statusCode === 401) {
30+
cb(new Error("Invalid authorization key."));
31+
}
32+
33+
if(response.statusCode >= 400) {
34+
cb(new Error("HTTP error " + response.statusCode + ": " + http.STATUS_CODES[response.statusCode]));
35+
}
36+
37+
var parser = new xml2js.Parser();
38+
parser.on("end", function(result) {
39+
cb(null, result);
40+
});
41+
42+
parser.on("error", function(err) {
43+
cb(err);
44+
});
45+
46+
parser.parseString(body);
47+
});
48+
};
49+
50+
var Spreadsheets = module.exports = function(opts, cb) {
51+
if(!opts) {
52+
throw new Error("Invalid arguments.");
53+
}
54+
if(!opts.key) {
55+
throw new Error("Spreadsheet key not provided.");
56+
}
57+
58+
getFeed(["worksheets", opts.key], opts.auth, null, function(err, data) {
59+
if(err) {
60+
return cb(err);
61+
}
62+
63+
cb(null, new Spreadsheet(opts.key, data));
64+
});
65+
};
66+
67+
Spreadsheets.rows = function(opts, cb) {
68+
if(!opts) {
69+
throw new Error("Invalid arguments.");
70+
}
71+
if(!opts.key) {
72+
throw new Error("Spreadsheet key not provided.");
73+
}
74+
if(!opts.worksheet) {
75+
throw new Error("Worksheet not specified.");
76+
}
77+
78+
var query = {};
79+
if(opts.start) {
80+
query["start-index"] = opts.start;
81+
}
82+
if(opts.num) {
83+
query["max-results"] = opts.num;
84+
}
85+
86+
getFeed(["list", opts.key, opts.worksheet], opts.auth, query, function(err, data) {
87+
if(err) {
88+
return cb(err);
89+
}
90+
91+
var rows = [];
92+
var entries = data.entry;
93+
if(!Array.isArray(entries)) {
94+
entries = [entries];
95+
}
96+
97+
entries.forEach(function(entry) {
98+
rows.push(new Row(entry));
99+
});
100+
101+
cb(null, rows);
102+
});
103+
};
104+
105+
Spreadsheets.cells = function(opts, cb) {
106+
if(!opts) {
107+
throw new Error("Invalid arguments.");
108+
}
109+
if(!opts.key) {
110+
throw new Error("Spreadsheet key not provided.");
111+
}
112+
if(!opts.worksheet) {
113+
throw new Error("Worksheet not specified.");
114+
}
115+
116+
var query = {
117+
"return-empty": "true"
118+
};
119+
if(opts.range) {
120+
query["range"] = opts.range;
121+
}
122+
123+
getFeed(["cells", opts.key, opts.worksheet], opts.auth, query, function(err, data) {
124+
if(err) {
125+
return cb(err);
126+
}
127+
128+
129+
});
130+
};
131+
132+
var Spreadsheet = function(key, data) {
133+
this.key = key;
134+
this.title = data.title["#"];
135+
this.updated = data.updated;
136+
this.author = data.author;
137+
138+
this.worksheets = [];
139+
var worksheets = data.entry;
140+
if(!Array.isArray(worksheets)) {
141+
worksheets = [worksheets];
142+
}
143+
144+
worksheets.forEach(function(worksheetData) {
145+
this.worksheets.push(new Worksheet(worksheetData));
146+
}, this);
147+
};
148+
149+
var Worksheet = function(data) {
150+
this.rowCount = data["gs:rowCount"];
151+
this.colCount = data["gs:colCount"];
152+
this.title = data.title["#"];
153+
};
154+
155+
var Row = function(data) {
156+
Object.keys(data).forEach(function(key) {
157+
if(key.substring(0, 4) === "gsx:") {
158+
this[key.substring(4)] = data[key];
159+
}
160+
}, this);
161+
};

node-google-spreadsheets/test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
var util = require("util");
2+
var GoogleSpreadsheets = require("./lib/spreadsheets");
3+
var GoogleClientLogin = require('googleclientlogin').GoogleClientLogin;
4+
var googleAuth = new GoogleClientLogin({
5+
email: '',
6+
password: '',
7+
service: 'spreadsheets',
8+
accountType: GoogleClientLogin.accountTypes.google
9+
});
10+
11+
googleAuth.on(GoogleClientLogin.events.login, function(){
12+
/*GoogleSpreadsheets({
13+
key: "tkuXNV2Q8eOiToapbWUZsFQ",
14+
auth: googleAuth.getAuthId()
15+
}, function(err, spreadsheet) {
16+
console.log(util.inspect(spreadsheet, false, null));
17+
});*/
18+
19+
GoogleSpreadsheets.rows({
20+
key: "tkuXNV2Q8eOiToapbWUZsFQ",
21+
worksheet: 2,
22+
auth: googleAuth.getAuthId()
23+
}, function(err, rows) {
24+
console.log(util.inspect(rows, false, null));
25+
});
26+
});
27+
28+
googleAuth.on(GoogleClientLogin.events.error, function(e) {
29+
switch(e.message) {
30+
case GoogleClientLogin.errors.loginFailed:
31+
if (this.isCaptchaRequired()) {
32+
console.log("ERROR. captcha required :(");
33+
}
34+
break;
35+
case GoogleClientLogin.errors.tokenMissing:
36+
case GoogleClientLogin.errors.captchaMissing:
37+
throw new Error('You must pass the both captcha token and the captcha')
38+
break;
39+
}
40+
throw new Error('Unknown error');
41+
// damn..
42+
});
43+
googleAuth.login();

0 commit comments

Comments
 (0)