Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ lib-cov
*.out
*.pid
*.gz
*.tgz

pids
logs
Expand Down
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Creates SpreadsheetML (.xlsx) files in sequence with streaming interface.

* Installation

npm install xlsx-stream
npm install node-xlsx-stream

* Features

Expand All @@ -14,7 +14,7 @@ Creates SpreadsheetML (.xlsx) files in sequence with streaming interface.
* Usage

# coffee-script
xlsx = require "xlsx-stream"
xlsx = require "node-xlsx-stream"
fs = require "fs"

x = xlsx()
Expand Down Expand Up @@ -46,7 +46,3 @@ Creates SpreadsheetML (.xlsx) files in sequence with streaming interface.
sheet2.end()

x.finalize()

* Help Wanted

Comments
12 changes: 0 additions & 12 deletions a.js

This file was deleted.

78 changes: 49 additions & 29 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
var archiver, duplex, sheetStream, templates, through, utils, xlsxStream, _;
// Convert stream of Array to a xlsx file.

// * Usage

// out = fs.createWriteStream('out.xlsx')
// stream = xlsxStream()
// stream.pipe out

// stream.write(['aaa', 'bbb', 'ccc'])
// stream.write([1, 2, 3])
// stream.write([new Date, '090-1234-5678', 'これはテストです'])

// stream.end()
var _, archiver, duplex, sheetStream, templates, through, utils, xlsxStream;

through = require('through');

Expand All @@ -14,14 +27,13 @@ utils = require("./utils");

sheetStream = require("./sheet");

module.exports = xlsxStream = function(opts) {
var defaultRepeater, defaultSheet, index, item, proxy, sheets, styles, zip, _i, _len, _ref;
if (opts == null) {
opts = {};
}
module.exports = xlsxStream = function(opts = {}) {
var defaultRepeater, defaultSheet, i, index, item, len, proxy, ref, sheets, styles, zip;
// archiving into a zip file using archiver (internally using node's zlib built-in module)
zip = archiver.create('zip', opts);
defaultRepeater = through();
proxy = duplex(defaultRepeater, zip);
// prevent loosing data before listening 'data' event in node v0.8
zip.pause();
process.nextTick(function() {
return zip.resume();
Expand All @@ -39,10 +51,12 @@ module.exports = xlsxStream = function(opts) {
{
numFmtId: "0",
formatCode: ""
}, {
},
{
numFmtId: "1",
formatCode: "0"
}, {
},
{
numFmtId: "14",
formatCode: "m/d/yy"
}
Expand All @@ -51,64 +65,70 @@ module.exports = xlsxStream = function(opts) {
formatCodesToStyleIndex: {}
};
index = 0;
_ref = styles.cellStyleXfs;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
item = _ref[_i];
ref = styles.cellStyleXfs;
for (i = 0, len = ref.length; i < len; i++) {
item = ref[i];
styles.formatCodesToStyleIndex[item.formatCode || ""] = index;
index++;
}
// writing data without sheet() results in creating a default worksheet named 'Sheet1'
defaultRepeater.once('data', function(data) {
defaultSheet = proxy.sheet('Sheet1');
defaultSheet.write(data);
defaultRepeater.pipe(defaultSheet);
return defaultRepeater.on('end', proxy.finalize);
});
// Append a new worksheet to the workbook
proxy.sheet = function(name) {
var sheet;
index = sheets.length + 1;
sheet = {
index: index,
name: name || ("Sheet" + index),
rel: "worksheets/sheet" + index + ".xml",
path: "xl/worksheets/sheet" + index + ".xml",
name: name || `Sheet${index}`,
rel: `worksheets/sheet${index}.xml`,
path: `xl/worksheets/sheet${index}.xml`,
styles: styles
};
sheets.push(sheet);
return sheetStream(zip, sheet, opts);
};
// finalize the xlsx file
proxy.finalize = function() {
var buffer, func, name, obj, sheet, _j, _len1, _ref1, _ref2, _ref3;
var buffer, func, j, len1, name, obj, ref1, ref2, ref3, sheet;
// styles
zip.append(templates.styles(styles), {
name: "xl/styles.xml",
store: opts.store
});
_ref1 = templates.statics;
for (name in _ref1) {
buffer = _ref1[name];
ref1 = templates.statics;
// static files
for (name in ref1) {
buffer = ref1[name];
zip.append(buffer, {
name: name,
name,
store: opts.store
});
}
_ref2 = templates.semiStatics;
for (name in _ref2) {
func = _ref2[name];
ref2 = templates.semiStatics;
for (name in ref2) {
func = ref2[name];
zip.append(func(opts), {
name: name,
name,
store: opts.store
});
}
_ref3 = templates.sheet_related;
for (name in _ref3) {
obj = _ref3[name];
ref3 = templates.sheet_related;
// files modified by number of sheets
for (name in ref3) {
obj = ref3[name];
buffer = obj.header;
for (_j = 0, _len1 = sheets.length; _j < _len1; _j++) {
sheet = sheets[_j];
for (j = 0, len1 = sheets.length; j < len1; j++) {
sheet = sheets[j];
buffer += obj.sheet(sheet);
}
buffer += obj.footer;
zip.append(buffer, {
name: name,
name,
store: opts.store
});
}
Expand Down
28 changes: 15 additions & 13 deletions lib/sheet.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var sheetStream, template, through, utils, _;
var _, sheetStream, template, through, utils;

_ = require("lodash");

Expand All @@ -8,34 +8,35 @@ utils = require('./utils');

template = require('./templates').worksheet;

module.exports = sheetStream = function(zip, sheet, opts) {
module.exports = sheetStream = function(zip, sheet, opts = {}) {
var colChar, converter, nRow, onData, onEnd;
if (opts == null) {
opts = {};
}
// 列番号の26進表記(A, B, .., Z, AA, AB, ..)
// 一度計算したらキャッシュしておく。
colChar = _.memoize(utils.colChar);
// 行ごとに変換してxl/worksheets/sheet1.xml に追加
nRow = 0;
onData = function(row) {
var buf, col, i, val, _i, _j, _len, _len1, _ref;
var buf, col, i, j, k, len, len1, ref, val;
nRow++;
buf = "<row r='" + nRow + "'>";
buf = `<row r='${nRow}'>`;
if (opts.columns != null) {
_ref = opts.columns;
for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
col = _ref[i];
buf += utils.buildCell("" + (colChar(i)) + nRow, row[col], sheet.styles);
ref = opts.columns;
for (i = j = 0, len = ref.length; j < len; i = ++j) {
col = ref[i];
buf += utils.buildCell(`${colChar(i)}${nRow}`, row[col], sheet.styles);
}
} else {
for (i = _j = 0, _len1 = row.length; _j < _len1; i = ++_j) {
for (i = k = 0, len1 = row.length; k < len1; i = ++k) {
val = row[i];
buf += utils.buildCell("" + (colChar(i)) + nRow, val, sheet.styles);
buf += utils.buildCell(`${colChar(i)}${nRow}`, val, sheet.styles);
}
}
buf += '</row>';
return this.queue(buf);
};
onEnd = function() {
var converter;
// フッタ部分を追加
this.queue(template.footer);
this.queue(null);
return converter = colChar = zip = null;
Expand All @@ -45,6 +46,7 @@ module.exports = sheetStream = function(zip, sheet, opts) {
name: sheet.path,
store: opts.store
});
// ヘッダ部分を追加
converter.queue(template.header);
return converter;
};
Loading