Skip to content

Add ability to style row data by passing in style index value #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/example/node_modules
/node_modules
*.xlsx
*.log
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: node_js
node_js:
- "0.10"
notifications:
email:
on_success: always
on_failure: always
7 changes: 7 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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 `
40 changes: 21 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -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) -
Expand All @@ -15,30 +16,31 @@ var templateXLSX = "UEsDBBQAAAAIABN7eUK9Z10uOQEAADUEAAATAAAAW0NvbnRlbnRfVHlwZXNd
var sheetFront = '<?xml version="1.0" encoding="utf-8"?><x:worksheet xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac"><x:sheetPr><x:outlinePr summaryBelow="1" summaryRight="1" /></x:sheetPr><x:sheetViews><x:sheetView tabSelected="0" workbookViewId="0" /></x:sheetViews><x:sheetFormatPr defaultRowHeight="15" />';
var sheetBack = '<x:printOptions horizontalCentered="0" verticalCentered="0" headings="0" gridLines="0" /><x:pageMargins left="0.75" right="0.75" top="0.75" bottom="0.5" header="0.5" footer="0.75" /><x:pageSetup paperSize="1" scale="100" pageOrder="downThenOver" orientation="default" blackAndWhite="0" draft="0" cellComments="none" errors="displayed" /><x:headerFooter /><x:tableParts count="0" /></x:worksheet>';


var sharedStringsFront = '<?xml version="1.0" encoding="UTF-8"?><x:sst xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main" uniqueCount="$count" count="$count">';
var sharedStringsBack = '</x:sst>';
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;
Expand Down Expand Up @@ -75,13 +77,13 @@ exports.execute = function(config){
row = '<x:row r="' + currRow +'" spans="1:'+ colsLength + '">';
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;
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "",
Expand Down
92 changes: 72 additions & 20 deletions test/main.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
});
});
28 changes: 28 additions & 0 deletions test/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac">
<fonts count="2">
<font />
<font>
<b />
</font>
</fonts>
<fills count="1">
<fill />
</fills>
<borders count="1">
<border />
</borders>
<cellStyleXfs count="1">
<xf />
</cellStyleXfs>
<cellXfs count="2">
<xf />
<xf fontId="1" />
</cellXfs>
<dxfs count="0" />
<tableStyles count="0" defaultTableStyle="TableStyleMedium2" defaultPivotStyle="PivotStyleLight16" />
<extLst>
<ext uri="{EB79DEF2-80B8-43e5-95BD-54CBDDF9020C}" xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main"><x14:slicerStyles defaultSlicerStyle="SlicerStyleLight1"/>
</ext>
</extLst>
</styleSheet>