-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSpreadsheets.js
More file actions
516 lines (436 loc) · 13.6 KB
/
Spreadsheets.js
File metadata and controls
516 lines (436 loc) · 13.6 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/***/
var fs = require("fs"), querystring = require('querystring'), url = require("url");
var path = require("path"), $ = require("jquery");
var core = require("./core"), chain = require("./chain"), fork = require("./fork");
// extend jQuery
(function($) {
$.fn.outerXml = function() {
var xml = this.wrap("<root/>").parent().html();
xml = xml.replace(/(<link[^\>]+>)/g, "$1</link>");
return xml;
};
})($);
module.exports = exports = spreadsheets;
function spreadsheets(auth, callback) {
if(auth && $.isFunction(callback))
clientLogin.call(this, auth, function(err, _auth) {
var sheet = !err && Spreadsheets(_auth);
callback(err, sheet);
});
}
function clientLogin(auth, callback) {
var self = this, _auth;
fork.call(self, function(cb) {
_clientLogin("wise", cb);
}, function(cb) {
_clientLogin("writely", cb);
}, function(err, args) {
if(!err) {
_auth = {}, _auth["wise"] = {}, _auth["writely"] = {};
_auth["wise"].Authorization = "GoogleLogin auth=" + args[0].Auth;
_auth["writely"].Authorization = "GoogleLogin auth=" + args[1].Auth;
}
callback.call(self, err, _auth);
});
function _clientLogin(service, _callback) {
chain.call(self, function(next) {
var data = querystring.stringify({
accountType: "GOOGLE",
service: service,
Email: auth.Email,
Passwd: auth.Passwd
});
core.authenticate.call(self, null, data, next);
}, function(info, next) {
var auth = {};
var i, line = info.split(/\n/);
for (i = line.length; i--;)
if(line[i]) {
var kv = line[i].split("=");
auth[kv[0]] = kv[1];
}
// _auth["wise"].Authorization = "GoogleLogin auth=" + auth.Auth;
next.call(self, null, auth);
}, _callback);
}
};
function Spreadsheets(auth) {
var self = this;
if(!(self instanceof Spreadsheets))
return new Spreadsheets(auth);
self._auth = auth, self._list = {};
}
Spreadsheets.prototype.list = function(callback) {
var self = this, opt = {
headers: self._auth["wise"]
};
chain.call(self, function(next) {
core.list.call(self, opt, "", next);
}, function(xml, next) {
var $entries = $("entry", xml), list = self._list = {};
$entries.each(function() {
var $this = $(this), $id = $("id", $this);
var sp = $id.text().split(/\//), key = sp[sp.length - 1];
var ss = Spreadsheet(key, self._auth);
ss._xml = $this.outerXml();
list[key] = ss;
});
next.call(null, null, list);
}, callback);
};
Spreadsheets.prototype.createSpreadsheet = function(settings, callback) {
if($.isFunction(settings))
callback = settings, settings = {};
var self = this, opt = {
headers: self._auth["writely"]
};
chain.call(self, function(next) {
var xml = $(fs.readFileSync("xml/docentry.xml").toString());
$("title", xml).text(settings.title || "untitled");
var data = xml.outerXml();
core.createSpreadsheet.call(self, opt, data, next);
}, function(xml, next) {
var $xml = $(xml);
var id = $("id", $xml).text();
var sp = id.split(/spreadsheet%3A/), key = sp[1];
var ss = Spreadsheet(key, self._auth);
ss._xml = $xml.outerXml();
self._list[key] = ss;
next.call(self, null, ss);
}, callback);
};
Spreadsheets.prototype.downloadSpreadsheet = function(key, format, out, callback) {
var self = this;
chain.call(self, function(next) {
self.getSpreadsheet(key, next);
}, function(ss, next) {
ss.download(format, out, next);
}, callback);
};
Spreadsheets.prototype.moveToTrash = function(key, callback) {
var self = this;
chain.call(self, function(next) {
self.getSpreadsheet(key, next);
}, function(ss, next) {
ss.trash(next);
}, callback);
};
Spreadsheets.prototype.deleteSpreadsheet = function(key, callback) {
var self = this;
chain.call(self, function(next) {
self.getSpreadsheet(key, next);
}, function(ss, next) {
ss["delete"](next);
}, callback);
};
Spreadsheets.prototype.getSpreadsheet = function(key, callback) {
var self = this, list = self._list, opt = {
path: "/feeds/default/private/full/" + key,
headers: self._auth["writely"]
};
chain.call(self, function(next) {
key in list ? next.call(null, null, list[key]): core.getSpreadsheet.call(self, opt, "", next);
}, function(xml, next) {
var ss = Spreadsheet(key, self._auth);
ss._xml = xml;
next.call(self, null, ss);
}, callback);
};
/**
* Spreadsheet
*/
function Spreadsheet(key, auth) {
var self = this;
if(!(self instanceof Spreadsheet))
return new Spreadsheet(key, auth);
self._key = key, self._auth = auth["wise"], self._docauth = auth["writely"];
self._list = {}, self._titles = {};
}
Spreadsheet.prototype.worksheet = function(callback) {
var self = this, opt = {
path: "/feeds/worksheets/" + self._key + "/private/full",
headers: self._auth
};
chain.call(self, function(next) {
core.worksheet.call(self, opt, "", next);
}, function(xml, next) {
var $entries = $("entry", xml);
var list = self._list = {}, titles = self._titles = {};
$entries.each(function() {
var $this = $(this);
var id = $("id", $this), name = $("title", $this).text();
var sp = id.text().split(/\//), key = sp[sp.length - 1];
var ws = Worksheet(key, name, self);
ws._xml = $this.outerXml();
// list[key] = ws;
// titles[name] = ws;
});
next.call(self, null, list);
}, callback);
};
Spreadsheet.prototype.addWorksheet = function(settings, callback) {
if($.isFunction(settings))
callback = settings, settings = {};
var self = this, opt = {
path: "/feeds/worksheets/" + self._key + "/private/full",
headers: self._auth
};
var xml = $(fs.readFileSync("xml/worksheetentry.xml").toString());// TODO
// async
$("title", xml).text(settings.title || "untitled");
$("gs\\:rowCount", xml).text(settings.row || 100);
$("gs\\:colCount", xml).text(settings.col || 20);
var data = xml.outerXml();
// TODO Any bug depending on jquery will lurk...
data = data.replace(/count/g, "Count");
chain.call(self, function(next) {
core.addWorksheet.call(self, opt, data, next);
}, function(xml, next) {
var $xml = $(xml);
var id = $("id", $xml).text(), name = $("title", $xml).text();
var sp = id.split(/\//), key = sp[sp.length - 1];
var ws = self._list[key] = Worksheet(key, name, self);
ws._xml = $xml.outerXml();
next.call(self, null, ws);
}, callback);
};
/**
* format values: [xls, csv, pdf, ods, tsv, html]
*/
Spreadsheet.prototype.download = function(format, out, callback) {
if("function" === typeof format)
callback = format, format = "xls", out = ".";
else if("function" === typeof out)
callback = out, out = ".";
var self = this;
var $content = $("content", self._xml).eq(0), src = $content.attr("src");
var _url = url.parse(src);
if("string" === typeof out)
out = _create(out);
var opt = {
host: _url["hostname"],
path: _url["pathname"] + "?" + _url["query"] + "&exportFormat=" + format,
headers: self._auth,
outputStream: out
};
core.download.call(self, opt, "", callback);
function _create(pathname) {
if(fs.existsSync(pathname))
if(fs.statSync(pathname).isDirectory())
return _create(path.join(pathname, $("title", self._xml).text() + "." + format));
else {
var s, splits = pathname.split(/\./), len = splits.length;
var index = 2 <= len ? len - 2: 0, fname = splits[index];
if(s = fname.match(/\((\d+)\)$/))
splits[index] = fname.replace(/\(\d+\)$/, "(" + (+s[1] + 1) + ")");
else
splits[index] += "(1)";
return _create(splits.join("."));
}
else
return fs.createWriteStream(pathname);
}
};
Spreadsheet.prototype.trash = function(callback) {
_delete.call(this, false, callback);
};
Spreadsheet.prototype["delete"] = function(callback) {
_delete.call(this, true, callback);
};
function _delete(flg, callback) {
var self = this, key = self._key;
var opt = {
path: "/feeds/default/private/full/" + key + "?delete=" + flg,
headers: $.extend({
"If-None-Match": "W/\"" + key + ".\""
}, self._docauth)
};
core.deleteSpreadsheet.call(self, opt, "", callback);
}
/**
* Worksheet
*/
function Worksheet(key, title, spreadsheet) {
var self = this;
if(!(self instanceof Worksheet))
return new Worksheet(key, title, spreadsheet);
self._key = key, self._title = title, self._parent = spreadsheet;
spreadsheet._list[key] = spreadsheet._titles[title] = self;
}
Worksheet.prototype.row = function(callback) {
var self = this, opt = {
path: "/feeds/list/" + self._parent._key + "/" + self._key
+ "/private/full",
headers: self._parent._auth
};
chain.call(self, function(next) {
core.row.call(self, opt, "", next);
}, function(xml, next) {
var $entries = $("entry", xml), list = self._list = {};
$entries.each(function() {
var $this = $(this), $id = $("id", $this);
var sp = $id.text().split(/\//), key = sp[sp.length - 1];
var row = Row(key, self);
row._xml = $this.outerXml();
list[key] = row;
});
next.call(self, null, list);
}, callback);
};
Worksheet.prototype.cell = function(query, callback) {
if($.isFunction(query))
callback = query, query = "";
if(typeof query !== "string" && !(query instanceof String))
query = querystring.stringify(query);
if(!!query && /^[^\?]/.test(query))
query = "?" + query;
var self = this, opt = {
path: "/feeds/cells/" + self._parent._key + "/" + self._key
+ "/private/full" + query,
headers: self._parent._auth
};
chain.call(self, function(next) {
core.cell.call(self, opt, "", next);
}, function(xml, next) {
var $entries = $("entry", xml), list = self._list = self._list || {};
$entries.each(function() {
var $this = $(this), $id = $("id", $this);
var name = $("title", $this).text(), val = $("content", $this).text();
var sp = $id.text().split(/\//), key = sp[sp.length - 1];
var cell = Cell(key, name, val, self);
cell._xml = $this.outerXml();
list[key] = cell;
});
next.call(self, null, list);
}, callback);
};
Worksheet.prototype.getCell = function(key, callback) {
var self = this;
var cell = Cell(key, null, null, self);
cell._get(callback);
};
var Style = require("./action9");
Worksheet.prototype.style = function(range, callback) {
var self = this;
callback.call(self, null, Style(range, self));
};
/**
* vals = [[row, col, inputValue], ...]
*/
Worksheet.prototype.updateCells = function(vals, callback) {
var self = this, p = self._parent;
var path = "/feeds/cells/" + p._key + "/" + self._key + "/private/full";
var id = "https://spreadsheets.google.com" + path;
var $feed;
chain.call(self, function(next) {
fs.readFile("xml/batchfeed.xml", "utf8", next);
}, function(buf, next) {
$feed = $(buf.toString());
$("id", $feed).text(id);
fs.readFile("xml/batchcellentry.xml", "utf8", next);
}, function(buf, next) {
var xml = buf.toString(), opt = {
path: path + "/batch",
headers: $.extend({
"If-None-Match": "W/\"" + p._key + ".\""
}, p._auth)
}, i, newCells = vals;
for (i = newCells.length; i--;) {
var $entry = $(xml), cell = newCells[i];
var _id = id + "/R" + cell[0] + "C" + cell[1];
$("batch\\:id", $entry).text("A" + i);
$("title", $entry).text("A" + i);
$("id", $entry).text(_id);
$("link", $entry).attr({
href: _id + "/1"
});
$("gs\\:cell", $entry).attr({
row: cell[0],
col: cell[1],
inputValue: cell[2]
});
$feed.append($entry);
}
core.updateCells.call(self, opt, $feed.outerXml(), next);
}, function(xml, next) {
var err = /error/i.test(xml) ? new Error(xml): null;
next.call(self, err);
}, callback);
};
/**
* TODO Row
*/
function Row(key, worksheet) {
var self = this;
if(!(self instanceof Row))
return new Row(key, worksheet);
self._key = key;
self._parent = worksheet;
}
/**
* Cell
*/
function Cell(key, title, val, worksheet) {
var self = this;
if(!(self instanceof Cell))
return new Cell(key, worksheet);
var rc = (new RegExp("^R(\\d+)C(\\d+)$")).exec(key);
self._key = key, self._row = +rc[1], self._col = +rc[2];
self._title = title, self._value = val, self._parent = worksheet;
}
Cell.prototype.style = function(callback) {
var self = this, range = {};
range.scol = range.ecol = self._col;
range.srow = range.erow = self._row;
self._parent.style(range, callback);
};
Cell.prototype.changeValue = function(val, callback) {
var self = this, p = self._parent, gp = p._parent;
var path = "/feeds/cells/" + gp._key + "/" + p._key + "/private/full/"
+ self._key;
chain.call(self, function(next) {
fs.readFile("xml/cellentry.xml", "utf8", next);
}, function(buf, next) {
var $xml = $(buf.toString());
var id = "https://spreadsheets.google.com" + path;
$("id", $xml).text(id);
$("link", $xml).attr({
href: id
});
$("gs\\:cell", $xml).attr({
row: self._row,
col: self._col,
inputValue: val
});
var data = $xml.outerXml(), opt = {
path: path,
headers: $.extend({
"If-None-Match": "W/\"" + gp._key + ".\""
}, gp._auth)
};
core.changeCell.call(self, opt, data, next);
}, function(xml, next) {
self._xml = $(xml).outerXml();
next.call(self, null, self);
}, callback);
};
Cell.prototype.getValue = function(callback) {
var self = this, p = self._parent, key = self._key;
chain.call(self, self._get, function(cell, next) {
callback.call(self, null, cell ? cell._value: null);
});
};
Cell.prototype._get = function(callback) {
var self = this, p = self._parent;
var query = {};
chain.call(self, function(next) {
query["min-row"] = query["max-row"] = self._row;
query["min-col"] = query["max-col"] = self._col;
p.cell(query, next);
}, function(cells) {
var key = self._key, cell = cells[key] = cells[key]
|| Cell(key, null, null, p);
p._list[key] = cell;
callback.call(self, null, cell);
});
};