From d342f8b0b200908a21f9345a5da58f7ed4714a0e Mon Sep 17 00:00:00 2001 From: Riyadh Al Nur Date: Wed, 19 Nov 2014 17:23:16 +0600 Subject: [PATCH 01/13] Add ability to style row data by passing in style index value ADDED: Row data will be styled according to the styleIndex param passed or will default to style index 0 which is referenced in the XML stylesheet. FIXED: Fixed indentation in test file --- index.js | 4 ++-- test/main.js | 41 +++++++++++++++++++++-------------------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/index.js b/index.js index ee9b42e..c8e2d06 100644 --- a/index.js +++ b/index.js @@ -75,13 +75,13 @@ exports.execute = function(config){ row = ''; for (j=0; j < colsLength; j++) { - styleIndex = null; + styleIndex = data[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/test/main.js b/test/main.js index 19e7d2e..605bb73 100644 --- a/test/main.js +++ b/test/main.js @@ -2,27 +2,28 @@ var should = require('should'); var nodeExcel = require('../index'); - 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('d.xlsx', result, 'binary'); }); - }); + }); }); From b220f99d24d218230eeef2e3414441f3fe7ca7e3 Mon Sep 17 00:00:00 2001 From: Riyadh Al Nur Date: Wed, 19 Nov 2014 17:26:16 +0600 Subject: [PATCH 02/13] Ignore .log and .xlsx files UPDATED: Update .gitignore to ignore XLSX and LOG files --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) 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 From df8e46f1826780046a0877db7be7d9219b03a6ef Mon Sep 17 00:00:00 2001 From: Riyadh Al Nur Date: Wed, 19 Nov 2014 17:28:11 +0600 Subject: [PATCH 03/13] Updated README to indicate how to run tests UPDATED: Updated README file to indicate how to run the unit tests for the library --- Readme.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Readme.md b/Readme.md index 0048fac..9f07474 100644 --- a/Readme.md +++ b/Readme.md @@ -61,3 +61,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 From 1ac788ecfe42545eb2069a08a31df3d689474150 Mon Sep 17 00:00:00 2001 From: Riyadh Al Nur Date: Sun, 23 Nov 2014 10:37:27 +0600 Subject: [PATCH 04/13] Add travis build configuration file --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..87f8cd9 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,3 @@ +language: node_js +node_js: + - "0.10" \ No newline at end of file From 8af7a1ad2860e6abbf0e77f845d65e24ac91277c Mon Sep 17 00:00:00 2001 From: Riyadh Al Nur Date: Sun, 23 Nov 2014 10:46:59 +0600 Subject: [PATCH 05/13] Add test for bigger data set --- test/main.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/main.js b/test/main.js index 605bb73..29352b5 100644 --- a/test/main.js +++ b/test/main.js @@ -25,5 +25,22 @@ describe('Simple Excel xlsx Export', function() { fs = require('fs'); fs.writeFileSync('d.xlsx', result, 'binary'); }); + + it('returns xlsx for big data', function () { + + conf.col = [ + {caption:'Text', type:'string'}, + {caption:'Text 2', type:'string'} + ]; + + conf.rows = [ + for(var i=0; i<10000; i++) { + ['hello', 'world']; + } + ]; + + var result = nodeExcel.execute(conf), fs = require('fs'); + fs.writeFileSync('f.xlsx', result, 'binary'); + }); }); }); From c20d19b72ffb3b7272cffb7200df3ae367e9e3b0 Mon Sep 17 00:00:00 2001 From: Riyadh Al Nur Date: Sun, 23 Nov 2014 11:06:52 +0600 Subject: [PATCH 06/13] Fixed error in test case --- test/main.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/test/main.js b/test/main.js index 29352b5..a5224ec 100644 --- a/test/main.js +++ b/test/main.js @@ -27,17 +27,20 @@ describe('Simple Excel xlsx Export', function() { }); it('returns xlsx for big data', function () { + var conf = {}; - conf.col = [ + conf.cols = [ {caption:'Text', type:'string'}, - {caption:'Text 2', type:'string'} + {caption:'Text 2', type:'string'}, + {caption:'Number', type:'number'}, + {caption:'Boolean', type:'bool'} ]; - conf.rows = [ - for(var i=0; i<10000; i++) { - ['hello', 'world']; - } - ]; + 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('f.xlsx', result, 'binary'); From 966b758e63ec7914a9fab7546a703dea2cf9a1cc Mon Sep 17 00:00:00 2001 From: Riyadh Al Nur Date: Sun, 23 Nov 2014 11:08:48 +0600 Subject: [PATCH 07/13] Update README to show build status --- Readme.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Readme.md b/Readme.md index 9f07474..d90471e 100644 --- a/Readme.md +++ b/Readme.md @@ -1,3 +1,6 @@ +https://travis-ci.org/newscred/Node-Excel-Export.svg?branch=master + + # excel-export # A simple node.js module for exporting data set to Excel xlsx file. From d8c679425b733b487ebe09e0fd3f29aaf9007ba5 Mon Sep 17 00:00:00 2001 From: Riyadh Al Nur Date: Sun, 23 Nov 2014 11:10:03 +0600 Subject: [PATCH 08/13] Markdown for build status in README --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index d90471e..92f3ad9 100644 --- a/Readme.md +++ b/Readme.md @@ -1,4 +1,4 @@ -https://travis-ci.org/newscred/Node-Excel-Export.svg?branch=master +[![Build Status](https://travis-ci.org/newscred/Node-Excel-Export.svg?branch=master)](https://travis-ci.org/newscred/Node-Excel-Export) # excel-export # From 01f34505a8fbe8065ddd7b0b6327b71adeeefb39 Mon Sep 17 00:00:00 2001 From: Riyadh Al Nur Date: Sun, 23 Nov 2014 16:23:55 +0600 Subject: [PATCH 09/13] Get style index for row data from respective column settings --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index c8e2d06..75e02ad 100644 --- a/index.js +++ b/index.js @@ -75,7 +75,7 @@ exports.execute = function(config){ row = ''; for (j=0; j < colsLength; j++) { - styleIndex = data[j].styleIndex || 0; + styleIndex = cols[j].styleIndex || 0; cellData = r[j]; cellType = cols[j].type; if (typeof cols[j].beforeCellWrite === 'function'){ From 70c1c44f9afbcc3879bc952f78de16c9b7cd6883 Mon Sep 17 00:00:00 2001 From: Riyadh Al Nur Date: Sun, 23 Nov 2014 16:36:29 +0600 Subject: [PATCH 10/13] Update test to test with style indexes --- test/main.js | 13 ++++++++----- test/styles.xml | 28 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 test/styles.xml diff --git a/test/main.js b/test/main.js index a5224ec..688b797 100644 --- a/test/main.js +++ b/test/main.js @@ -1,6 +1,7 @@ // test/main.js var should = require('should'); var nodeExcel = require('../index'); +var path = require('path'); describe('Simple Excel xlsx Export', function() { describe('Export', function() { @@ -26,14 +27,16 @@ describe('Simple Excel xlsx Export', function() { fs.writeFileSync('d.xlsx', result, 'binary'); }); - it('returns xlsx for big data', function () { + it('returns xlsx for big data set', function () { var conf = {}; + conf.stylesXmlFile = path.resolve(__dirname, 'styles.xml'); + conf.cols = [ - {caption:'Text', type:'string'}, - {caption:'Text 2', type:'string'}, - {caption:'Number', type:'number'}, - {caption:'Boolean', type:'bool'} + {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 = []; 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 From 77b8100a06b2676a12841dfa55041fb477baf522 Mon Sep 17 00:00:00 2001 From: Riyadh Al Nur Date: Mon, 24 Nov 2014 12:39:37 +0600 Subject: [PATCH 11/13] use async library to execute async task also updated travis.yml to send notifications to multiple email addresses on success and failure --- .travis.yml | 9 ++++++++- index.js | 36 +++++++++++++++++++----------------- package.json | 3 ++- test/main.js | 28 ++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 19 deletions(-) diff --git a/.travis.yml b/.travis.yml index 87f8cd9..38cb722 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,10 @@ language: node_js node_js: - - "0.10" \ No newline at end of file + - "0.10" +notifications: + email: + recipients: + - riyadh@newscred.com + - plansquad@newscred.com + on_success: always + on_failure: always \ No newline at end of file diff --git a/index.js b/index.js index 75e02ad..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; 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 688b797..345a2a0 100644 --- a/test/main.js +++ b/test/main.js @@ -49,4 +49,32 @@ describe('Simple Excel xlsx Export', function() { fs.writeFileSync('f.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('f.xlsx', result, 'binary'); + }); + }); }); From ea83d79c747023b4d11aacc2a6ed1539366e9614 Mon Sep 17 00:00:00 2001 From: Riyadh Al Nur Date: Wed, 26 Nov 2014 11:41:57 +0600 Subject: [PATCH 12/13] Change file names in test cases --- test/main.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/main.js b/test/main.js index 345a2a0..53fa099 100644 --- a/test/main.js +++ b/test/main.js @@ -24,7 +24,7 @@ describe('Simple Excel xlsx Export', function() { var result = nodeExcel.execute(conf), fs = require('fs'); - fs.writeFileSync('d.xlsx', result, 'binary'); + fs.writeFileSync('test1.xlsx', result, 'binary'); }); it('returns xlsx for big data set', function () { @@ -46,7 +46,7 @@ describe('Simple Excel xlsx Export', function() { } var result = nodeExcel.execute(conf), fs = require('fs'); - fs.writeFileSync('f.xlsx', result, 'binary'); + fs.writeFileSync('test2.xlsx', result, 'binary'); }); }); @@ -74,7 +74,7 @@ describe('Export Async', function() { should.exist(sheet); return sheet; }), fs = require('fs'); - fs.writeFileSync('f.xlsx', result, 'binary'); + fs.writeFileSync('test3.xlsx', result, 'binary'); }); }); }); From c54b55ef5586b16a65da7bce7076602834363216 Mon Sep 17 00:00:00 2001 From: Riyadh Al Nur Date: Wed, 26 Nov 2014 11:44:29 +0600 Subject: [PATCH 13/13] Remove email address from travis config --- .travis.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 38cb722..89856c9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,8 +3,5 @@ node_js: - "0.10" notifications: email: - recipients: - - riyadh@newscred.com - - plansquad@newscred.com on_success: always on_failure: always \ No newline at end of file