js-xlsx-gen
Simple declarative layer built on top of protobi/js-xlsx
which is a fork of SheetJS/js-xlsx
that adds styles.
Installation
npm install js-xlsx-gen
Usage
var xlsGen = defaultDateFormat: 'yyyy-mm-dd' // optional override of default date format of mm/dd/yyyy; // define some common styles// see: https://github.com/protobi/js-xlsx/blob/master/tests/test-style.js for examples of styling AND https://github.com/SheetJS/ssf/blob/master/ssf.js for examples of numFmtvar commonStyles = header: alignment: horizontal: 'center' font: bold: true positive: numFmt: '$#,##0.00;$(#,##0.00)' font: color: rgb: '00ffff' fill: fgColor: rgb: 'ffffff' ;// next define the spreadsheet with each key as the sheet namevar spreadsheet = sheetA: header: styles: row: commonStylesheader columns: 1: font: italic: true columns: v :'ColA' s: commonStylesheader v :'ColB' s: commonStylesheader data: rows: 1 2 2 3 sheetB: header: columns: 'Column A' 'Column B' 'Column C' 'Column D' data: styles: columns: 2: numFmt: '$#,##0.00;$(#,##0.00)' 3: font: color: rgb: '00ff00' rows: true v: s: numFmt: 'mm-dd-yyyy' 20 v: -002 s: numFmt: '$#,##0.00;$(#,##0.00)' font: color: rgb: 'ffffff' fill: fgColor: rgb: 'ff0000' // inline styling v: 3 s: commonStylespositive // inline styling to global styling 123 40 ;// build workbook with OPTIONAL default cell stylingvar workbook = xlsGen;// write out XLSXxlsGen;
API
generate ( spreadsheet, defaultStyle )
Generate a workbook from a spreadsheet definition with default cell styles.
Arguments
spreadsheet
- Spreadsheet definitiondefaultStyle
- OPTIONAL styling for headers and data
Example
var workbook = xlsGen;
write ( workbook, wopts )
Write workbook to Binary string. For more details see js-xlsx write()
writeFile ( workbook, filePath )
Write workbook to Binary string. For more details see js-xlsx writeFile()
Spreadsheet Definition
The Spreadsheet Definition contains multiple sheets:
var spreadsheet = sheetA: sheetB: ;
Each sheet has an optional header
section and a data
section:
var spreadsheet = sheetA: header: data: ;
The optional header
section has an optional style
section and columns
section:
var spreadsheet = sheetA: header: styles: columns: ;
The data
section has an optional style
section and data
section:
var spreadsheet = sheetA: data: styles: rows: ;
The styles
section under the header
and data
sections contains an optional row
section and an optional columns
section:
var spreadsheet = sheetA: header: styles: row: alignment: horizontal: 'center' font: bold: true columns 1: font: italic: true columns: data: styles: row: alignment: horizontal: 'right' font: bold: true italic: true sz: 14 columns 1: numFmt: '$#,##0.00;$(#,##0.00)' font: color: rgb: '00ffff' fill: fgColor: rgb: 'ffffff' rows: ;
Unfortunately, these styles aren't documented well, but examples can be found in the style test code of protobi/js-xlsx
.
The columns
array of the header
section contains cell data:
var spreadsheet = sheetA: header: columns: v :'ColA' s: commonStylesheader 'ColB' data: ;
Notice that the columns
array contains either objects that conform to Cell Objects in protobi/js-xlsx
OR raw data, i.e. number, date, string, boolean.
The rows
array of the data
section contains row data:
var spreadsheet = sheetA: data: rows: true v: s: numFmt: 'mm-dd-yyyy' 20 v: -002 s: numFmt: '$#,##0.00;$(#,##0.00)' font: color: rgb: 'ffffff' fill: fgColor: rgb: 'ff0000' v: 3 s: commonStylespositive 123 40 ;
Notice that the rows
array contains an array for each row which contains either objects that conform to Cell Objects in protobi/js-xlsx
OR raw data, i.e. number, date, string, boolean.