Comparing version 0.3.0 to 0.4.0
@@ -8,9 +8,9 @@ 'use strict'; | ||
module.exports = { | ||
parse: function(mixed) { | ||
parse: function(mixed, options) { | ||
if(typeof mixed === 'string') mixed = fs.readFileSync(mixed); | ||
debug('parsed a %d-sized xml', mixed.length); | ||
return xlsx(mixed.toString('base64')); | ||
return xlsx(mixed.toString('base64'), options); | ||
}, | ||
build: function(object) { | ||
var data = xlsx(object); // [ 'base64', 'zipTime', 'processTime', 'href' ] | ||
build: function(object, options) { | ||
var data = xlsx(object, options); // [ 'base64', 'zipTime', 'processTime', 'href' ] | ||
if(!data.base64) return false; | ||
@@ -17,0 +17,0 @@ var buffer = new Buffer(data.base64, 'base64'); |
@@ -11,8 +11,15 @@ var JSZip = null | ||
//---------------------------------------------------------- | ||
function xlsx(file) { | ||
function xlsx(file, options) { | ||
'use strict'; // v2.3.0 | ||
var defaultFontName = 'Calibri'; | ||
var defaultFontSize = 11; | ||
options = options || {}; | ||
var defaultFontName = options.defaultFontName || 'Calibri'; | ||
var defaultFontSize = options.defaultFontSize || 11; | ||
var defaultFontColor = options.defaultFontColor || '000'; | ||
var defaultCellBorders = options.defaultCellBorders; | ||
var defaultHAlign = options.defaultHAlign; | ||
var defaultVAlign = options.defaultVAlign; | ||
var defaultTextRotation = options.defaultTextRotation; | ||
var result, zip = new JSZip(), zipTime, processTime, s, f, i, j, k, l, t, w, sharedStrings, styles, index, data, val, style, borders, border, borderIndex, fonts, font, fontIndex, | ||
@@ -180,3 +187,4 @@ docProps, xl, xlWorksheets, worksheet, contentTypes = [[], []], props = [], xlRels = [], worksheets = [], id, columns, cols, colWidth, cell, row, merges, merged, | ||
j = -1; k = data[i].length; | ||
s += '<row r="' + (i + 1) + '" x14ac:dyDescent="0.25">'; | ||
var height = options.forcedRowHeight && options.forcedRowHeight[i] >= 0 ? options.forcedRowHeight[i] : options.defaultRowHeight; | ||
s += '<row r="' + (i + 1) + '"' + (height >= 0 ? ' customHeight="1" ht="' + height + '"' : '') + ' x14ac:dyDescent="0.25">'; | ||
while (++j < k) { | ||
@@ -186,8 +194,10 @@ cell = data[i][j]; val = cell.hasOwnProperty('value') ? cell.value : cell; t = ''; | ||
style = { | ||
borders: cell.borders, | ||
hAlign: cell.hAlign, | ||
vAlign: cell.vAlign, | ||
borders: cell.borders || defaultCellBorders, | ||
hAlign: cell.hAlign || defaultHAlign, | ||
textRotation: cell.textRotation || defaultTextRotation, | ||
vAlign: cell.vAlign === 'bottom' ? '' : (cell.vAlign || defaultVAlign), // default is bottom | ||
bold: cell.bold, | ||
italic: cell.italic, | ||
fontName: cell.fontName, | ||
fontColor: cell.fontColor, | ||
fontSize: cell.fontSize, | ||
@@ -197,3 +207,3 @@ formatCode: cell.formatCode || 'General' | ||
colWidth = 0; | ||
if (val && typeof val === 'string' && !isFinite(val)) { | ||
if (val && typeof val === 'string' && !isFinite(val) || cell.forceString) { | ||
// If value is string, and not string of just a number, place a sharedString reference instead of the value | ||
@@ -205,3 +215,3 @@ val = escapeXML(val); | ||
if (index < 0) { | ||
index = sharedStrings[0].push(val) - 1; | ||
index = sharedStrings[0].push(val.replace(/\n/g, ' ')) - 1; | ||
} | ||
@@ -238,3 +248,3 @@ val = index; | ||
if (cell.autoWidth) { | ||
columns[j].autoWidth = true | ||
columns[j].autoWidth = true; | ||
} | ||
@@ -244,4 +254,23 @@ if (colWidth > columns[j].max) { | ||
} | ||
// store merges if needed and add missing cells. Cannot have rowSpan AND colSpan | ||
if (cell.colSpan > 1) { | ||
if (cell.colWidth) { | ||
columns[j].autoWidth = true; | ||
columns[j].max = cell.colWidth; | ||
} | ||
// store merges if needed and add missing cells. | ||
if (cell.colSpan > 1 && cell.rowSpan > 1) { | ||
merges.push([numAlpha(j) + (i + 1), numAlpha(j+cell.colSpan-1) + (i + cell.rowSpan)]); | ||
merged = [j, 0]; | ||
for (var m = 0; m < cell.colSpan-1; m++) { | ||
merged.push(cell); | ||
} | ||
data[i].splice.apply(data[i], merged); | ||
for (var n = 1; n < cell.rowSpan; n++) { | ||
merged = [j-1, 0]; | ||
for (var m = 0; m < cell.colSpan; m++) { | ||
merged.push(cell); | ||
} | ||
data[i+n].splice.apply(data[i+n], merged); | ||
} | ||
k += cell.colSpan-1; | ||
} else if (cell.colSpan > 1) { | ||
// horizontal merge. ex: B12:E12. Add missing cells (with same attribute but value) to current row | ||
@@ -268,3 +297,3 @@ merges.push([numAlpha(j) + (i + 1), numAlpha(j+cell.colSpan-1) + (i + 1)]); | ||
} | ||
if (cell.rowSpan > 1 ||cell.colSpan > 1) { | ||
if (cell.rowSpan > 1 || cell.colSpan > 1) { | ||
// deletes value, rowSpan and colSpan from cell to avoid refering it from copied cells | ||
@@ -286,6 +315,14 @@ delete cell.value; | ||
cols = [] | ||
for (i = 0; i < columns.length; i++) { | ||
if (columns[i].autoWidth) { | ||
cols.push('<col min="', i+1, '" max="', i+1, '" width="', columns[i].max, '" bestFit="1"/>'); | ||
if (options.forcedColWidth) { | ||
for (i = 0; i < options.forcedColWidth.length; i++) { | ||
if (options.forcedColWidth[i] >= 0) { | ||
cols.push('<col customWidth="1" min="', i+1, '" max="', i+1, '" width="', options.forcedColWidth[i], '"/>'); | ||
} | ||
} | ||
} else { | ||
for (i = 0; i < columns.length; i++) { | ||
if (columns[i].autoWidth) { | ||
cols.push('<col min="', i+1, '" max="', i+1, '" width="', columns[i].max, '" bestFit="1"/>'); | ||
} | ||
} | ||
} | ||
@@ -298,4 +335,6 @@ // only add cols definition if not empty | ||
s = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><worksheet xmlns="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">' | ||
+ '<dimension ref="A1:' + numAlpha(data[0].length - 1) + data.length + '"/><sheetViews><sheetView ' + (w === file.activeWorksheet ? 'tabSelected="1" ' : '') | ||
+ ' workbookViewId="0"/></sheetViews><sheetFormatPr defaultRowHeight="15" x14ac:dyDescent="0.25"/>' | ||
+ '<dimension ref="A1:' + numAlpha(data[0].length - 1) + data.length + '"/><sheetViews>' | ||
+ '<sheetView ' + (w === file.activeWorksheet ? 'tabSelected="1" ' : '') + ' workbookViewId="0"' | ||
+ (worksheet.view ? ' view="' + worksheet.view + '"' : '') | ||
+ '/></sheetViews><sheetFormatPr defaultRowHeight="15" x14ac:dyDescent="0.25"/>' | ||
+ cols | ||
@@ -312,3 +351,14 @@ + '<sheetData>' | ||
} | ||
s += '<pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/>'; | ||
var paper_size = 9; | ||
var orientation = 'portrait'; | ||
var margins = {}; | ||
var page_scale = 100; | ||
if (options.page) { | ||
if (options.page.margins) margins = options.page.margins; | ||
if (options.page.paper_size) paper_size = options.page.paper_size; | ||
if (options.page.orientation) orientation = options.page.orientation; | ||
if (options.page.page_scale) page_scale = options.page.page_scale; | ||
} | ||
s += '<pageMargins left="' + (margins.left || 0.7) + '" right="' + (margins.right || 0.7) + '" top="' + (margins.top || 0.75) + '" bottom="' + (margins.bottom || 0.75) + '" header="' + (margins.header || 0.3) + '" footer="' + (margins.footer || 0.3) + '"/>'; | ||
s += '<pageSetup paperSize="' + paper_size + '" orientation="' + orientation + '" scale="' + page_scale + '"/>' | ||
if (worksheet.table) { | ||
@@ -385,3 +435,3 @@ s += '<tableParts count="1"><tablePart r:id="rId1"/></tableParts>'; | ||
fontIndex = 0 | ||
if (style.bold || style.italic || style.fontSize || style.fontName) { | ||
if (style.bold || style.italic || style.fontSize || style.fontName || style.fontColor) { | ||
font = ['<font>'] | ||
@@ -395,3 +445,3 @@ if (style.bold) { | ||
font.push('<sz val="', style.fontSize || defaultFontSize, '"/>'); | ||
font.push('<color theme="1"/>'); | ||
font.push('<color rgb="', style.fontColor || defaultFontColor, '"/>'); | ||
font.push('<name val="', style.fontName || defaultFontName, '"/>'); | ||
@@ -415,3 +465,3 @@ font.push('<family val="2"/>', '</font>'); | ||
'" ', | ||
(style.hAlign || style.vAlign? 'applyAlignment="1" ' : ' '), | ||
(style.hAlign || style.vAlign || style.textRotation? 'applyAlignment="1" ' : ' '), | ||
(style.formatCode > 0 ? 'applyNumberFormat="1" ' : ' '), | ||
@@ -422,3 +472,3 @@ (borderIndex > 0 ? 'applyBorder="1" ' : ' '), | ||
]; | ||
if (style.hAlign || style.vAlign) { | ||
if (style.hAlign || style.vAlign || style.textRotation) { | ||
styles[i].push('<alignment'); | ||
@@ -431,2 +481,5 @@ if (style.hAlign) { | ||
} | ||
if (style.textRotation) { | ||
styles[i].push(' textRotation="', style.textRotation, '"'); | ||
} | ||
styles[i].push('/>'); | ||
@@ -440,3 +493,3 @@ } | ||
xl.file('styles.xml', '<?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">' | ||
+ t + '<fonts count="'+ fonts.length + '" x14ac:knownFonts="1"><font><sz val="' + defaultFontSize + '"/><color theme="1"/><name val="' + defaultFontName + '"/><family val="2"/>' | ||
+ t + '<fonts count="'+ fonts.length + '" x14ac:knownFonts="1"><font><sz val="' + defaultFontSize + '"/><color rgb="' + defaultFontColor + '"/><name val="' + defaultFontName + '"/><family val="2"/>' | ||
+ '<scheme val="minor"/></font>' + fonts.join('') + '</fonts><fills count="2"><fill><patternFill patternType="none"/></fill><fill><patternFill patternType="gray125"/></fill></fills>' | ||
@@ -443,0 +496,0 @@ + '<borders count="' + borders.length + '"><border><left/><right/><top/><bottom/><diagonal/></border>' |
@@ -5,3 +5,3 @@ { | ||
"license": "Microsoft Office Extensible File License", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Node.js excel parser & builder", | ||
@@ -8,0 +8,0 @@ "main": "index.js", |
@@ -0,1 +1,11 @@ | ||
**New Features Added:** | ||
* More customizable options like font name and color. | ||
* Limited support for colSpan AND rowSpan. | ||
* Forced row height and column width. | ||
* Added textRotation. | ||
* Added page scale and sheet view option. | ||
--- | ||
[node-xlsx](http://mgcrea.github.com/node-xlsx) [![Build Status](https://secure.travis-ci.org/mgcrea/node-xlsx.png?branch=master)](http://travis-ci.org/#!/mgcrea/node-xlsx) | ||
@@ -2,0 +12,0 @@ ================= |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
51348
610
94
0