diff --git a/.gitignore b/.gitignore index 32b9cc9..860f50c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ /example/node_modules /node_modules +*.xlsx +*.log diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..89856c9 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,7 @@ +language: node_js +node_js: + - "0.10" +notifications: + email: + on_success: always + on_failure: always \ No newline at end of file diff --git a/Readme.md b/Readme.md index 0048fac..92f3ad9 100644 --- a/Readme.md +++ b/Readme.md @@ -1,3 +1,6 @@ +[![Build Status](https://travis-ci.org/newscred/Node-Excel-Export.svg?branch=master)](https://travis-ci.org/newscred/Node-Excel-Export) + + # excel-export # A simple node.js module for exporting data set to Excel xlsx file. @@ -61,3 +64,7 @@ Setup configuration object before passing it into the execute method. **cols** app.listen(3000); console.log('Listening on port 3000'); + +## Running tests ## +To run tests, +` npm test ` \ No newline at end of file diff --git a/index.js b/index.js index ee9b42e..fa37430 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ require('node-zip'); var fs = require('fs'), - SortedMap = require('collections/sorted-map'); + async = require('async'), + SortedMap = require('collections/sorted-map'); Date.prototype.getJulian = function() { return Math.floor((this / 86400000) - @@ -15,30 +16,31 @@ var templateXLSX = "UEsDBBQAAAAIABN7eUK9Z10uOQEAADUEAAATAAAAW0NvbnRlbnRfVHlwZXNd var sheetFront = ''; var sheetBack = ''; - var sharedStringsFront = ''; var sharedStringsBack = ''; var shareStrings, convertedShareStrings; -exports.executeAsync = function(config, callBack){ - return process.nextTick(function(){ - var r = exports.execute(config); - callBack(r); - }); +exports.executeAsync = function(config, callback){ + async.series([ + async.apply(exports.execute, config) + ], function (err, result) { + if (err) { return callback(err); } + return callback(null, result); + }); }; exports.execute = function(config){ var cols = config.cols, - data = config.rows, - colsLength = cols.length, - xlsx = new JSZip(templateXLSX, { base64: true, checkCRC32: false }), - sheet = xlsx.file("xl/worksheets/sheet.xml"), - sharedStringsXml = xlsx.file("xl/sharedStrings.xml"), - rows = "", - row ="", - colsWidth = "", - styleIndex, - k; + data = config.rows, + colsLength = cols.length, + xlsx = new JSZip(templateXLSX, { base64: true, checkCRC32: true }), + sheet = xlsx.file("xl/worksheets/sheet.xml"), + sharedStringsXml = xlsx.file("xl/sharedStrings.xml"), + rows = "", + row = "", + colsWidth = "", + styleIndex, + k; if (config.stylesXmlFile){ var path = config.stylesXmlFile; @@ -75,13 +77,13 @@ exports.execute = function(config){ row = ''; for (j=0; j < colsLength; j++) { - styleIndex = null; + styleIndex = cols[j].styleIndex || 0; cellData = r[j]; cellType = cols[j].type; if (typeof cols[j].beforeCellWrite === 'function'){ var e ={rowNum: currRow, styleIndex: null, cellType: cellType}; cellData = cols[j].beforeCellWrite(r, cellData, e); - styleIndex = e.styleIndex || styleIndex; + styleIndex = styleIndex || e.styleIndex; cellType = e.cellType; delete e; } diff --git a/package.json b/package.json index 5e552b9..d876d97 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,8 @@ "license": "BSD", "dependencies": { "collections": "^1.2.1", - "node-zip": "1.x" + "node-zip": "1.x", + "async": "~0.9.0" }, "devDependencies": { "mocha": "", diff --git a/test/main.js b/test/main.js index 19e7d2e..53fa099 100644 --- a/test/main.js +++ b/test/main.js @@ -1,28 +1,80 @@ // test/main.js var should = require('should'); var nodeExcel = require('../index'); - +var path = require('path'); describe('Simple Excel xlsx Export', function() { - describe('Export', function() { - it('returns xlsx', function() { - var conf ={}; - conf.cols = [ - {caption:'string', type:'string'}, - {caption:'date', type:'date'}, - {caption:'bool', type:'bool'}, - {caption:'number 2', type:'number'} - ]; - conf.rows = [ - ['pi', (new Date(Date.UTC(2013, 4, 1))).oaDate(), true, 3.14], - ["e", (new Date(2012, 4, 1)).oaDate(), false, 2.7182], - ["M&M<>'", (new Date(Date.UTC(2013, 6, 9))).oaDate(), false, 1.2], - ["null", null, null, null] - ]; + describe('Export', function() { + it('returns xlsx', function() { + var conf ={}; + + conf.cols = [ + {caption:'string', type:'string'}, + {caption:'date', type:'date'}, + {caption:'bool', type:'bool'}, + {caption:'number 2', type:'number'} + ]; + + conf.rows = [ + ['pi', (new Date(Date.UTC(2013, 4, 1))).oaDate(), true, 3.14], + ["e", (new Date(2012, 4, 1)).oaDate(), false, 2.7182], + ["M&M<>'", (new Date(Date.UTC(2013, 6, 9))).oaDate(), false, 1.2], + ["null", null, null, null] + ]; - var result = nodeExcel.execute(conf), - fs = require('fs'); - fs.writeFileSync('d.xlsx', result, 'binary'); + var result = nodeExcel.execute(conf), + fs = require('fs'); + fs.writeFileSync('test1.xlsx', result, 'binary'); + }); + + it('returns xlsx for big data set', function () { + var conf = {}; + + conf.stylesXmlFile = path.resolve(__dirname, 'styles.xml'); + + conf.cols = [ + {caption:'Text', type:'string', captionStyleIndex: 1, styleIndex: 2}, + {caption:'Text 2', type:'string', captionStyleIndex: 1, styleIndex: 2}, + {caption:'Number', type:'number', captionStyleIndex: 1, styleIndex: 2}, + {caption:'Boolean', type:'bool', captionStyleIndex: 1, styleIndex: 2} + ]; + + conf.rows = []; + + for(var i=0; i<10000; i++) { + conf.rows.push(['hello', 'world', 32000.4567, true]); + } + + var result = nodeExcel.execute(conf), fs = require('fs'); + fs.writeFileSync('test2.xlsx', result, 'binary'); + }); + }); + +describe('Export Async', function() { + it('returns xlsx for big data set', function () { + var conf = {}; + + conf.stylesXmlFile = path.resolve(__dirname, 'styles.xml'); + + conf.cols = [ + {caption:'Text', type:'string', captionStyleIndex: 1, styleIndex: 2}, + {caption:'Text 2', type:'string', captionStyleIndex: 1, styleIndex: 2}, + {caption:'Number', type:'number', captionStyleIndex: 1, styleIndex: 2}, + {caption:'Boolean', type:'bool', captionStyleIndex: 1, styleIndex: 2} + ]; + + conf.rows = []; + + for(var i=0; i<10000; i++) { + conf.rows.push(['hello', 'world', 32000.4567, true]); + } + + var result = nodeExcel.executeAsync(conf, function (err, sheet) { + should.not.exist(err); + should.exist(sheet); + return sheet; + }), fs = require('fs'); + fs.writeFileSync('test3.xlsx', result, 'binary'); }); - }); + }); }); diff --git a/test/styles.xml b/test/styles.xml new file mode 100644 index 0000000..1ea6513 --- /dev/null +++ b/test/styles.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file