Skip to content
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: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Options
|---|---|---|
|verbose|true|throw additional exceptions, if `false` - then pass empty string in that places|
|formatting|true|should cells with combined formats be formatted or not|

|returnFormats|false|should formats be returned alongside values|
-------
```javascript
const XlsxStreamReader = require("xlsx-stream-reader");
Expand Down
39 changes: 21 additions & 18 deletions lib/worksheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

'use strict'

const Util = require('util')
const ssf = require('ssf')
const Stream = require('stream')
Expand Down Expand Up @@ -65,7 +64,7 @@ function XlsxStreamReaderWorkSheet (workBook, sheetName, workSheetId, workSheetS
value: {},
enumerable: true,
writable: true
},
},
'abortSheet': {
value: false,
writable: true
Expand All @@ -76,6 +75,14 @@ function XlsxStreamReaderWorkSheet (workBook, sheetName, workSheetId, workSheetS
}
Util.inherits(XlsxStreamReaderWorkSheet, Stream)

const tryFormat = function (format, workingVal) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to refactor a bit, complexity is beginning to pile up in the formatting code

try {
return ssf.format(format, workingVal)
} catch (e) {
return ''
}
}

XlsxStreamReaderWorkSheet.prototype._handleWorkSheetStream = function () {
var self = this

Expand Down Expand Up @@ -192,6 +199,9 @@ XlsxStreamReaderWorkSheet.prototype._handleWorkSheetNode = function (nodeData) {
}
self.workingRow.values = []
self.workingRow.formulas = []
if (self.options.returnFormats) {
self.workingRow.formats = []
}
break
}

Expand All @@ -205,7 +215,7 @@ XlsxStreamReaderWorkSheet.prototype._handleWorkSheetNode = function (nodeData) {
}

if (workingCell && workingCell.attributes && workingCell.attributes.r) {
self.currentCell = workingCell;
self.currentCell = workingCell
}

if (workingCell.name === 'c') {
Expand Down Expand Up @@ -247,35 +257,28 @@ XlsxStreamReaderWorkSheet.prototype._handleWorkSheetNode = function (nodeData) {
if (typeof formatId !== 'undefined') {
var format = self.workBook.formatCodes[formatId]
if (typeof format === 'undefined') {
try {
workingVal = ssf.format(Number(formatId), Number(workingVal))
} catch (e) {
workingVal = ''
}
workingVal = tryFormat(Number(formatId), Number(workingVal))
} else if (format !== 'General') {
try {
workingVal = ssf.format(format, Number(workingVal))
} catch (e) {
workingVal = ''
}
workingVal = tryFormat(format, Number(workingVal))
}
}
} else if (!isNaN(parseFloat(workingVal))) { // this is number
workingVal = parseFloat(parseFloat(workingVal)) // parse to float or int
}
}

self.workingRow.values[cellNum] = workingVal || ''

if (self.options.returnFormats) {
self.workingRow.formats[cellNum] = format || ssf.get_table()[formatId]
}
workingCell = {}
}
}
if (workingCell.name === 'v') {
var cellNum = self.getColumnNumber(self.currentCell.attributes.r)
var colNum = self.getColumnNumber(self.currentCell.attributes.r)

self.currentCell = {};
self.currentCell = {}

self.workingRow.values[cellNum] = workingPart || ''
self.workingRow.values[colNum] = workingPart || ''
}
} else {
if (self.sheetData[nodeData[0].name]) {
Expand Down
34 changes: 13 additions & 21 deletions lib/xlsx-stream-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,23 @@ const XlsxStreamReaderWorkBook = require(Path.join(__dirname, 'workbook'))

module.exports = XlsxStreamReader

function XlsxStreamReader (options) {
function XlsxStreamReader (userOptions) {
if (!(this instanceof XlsxStreamReader)) return new XlsxStreamReader()

if (!options || typeof options !== 'object') {
options = {}
const defaults = {
verbose: true,
formatting: true,
returnFormats: false
}

if (typeof options.verbose === 'undefined') {
options.verbose = true
const saxOptions = {
saxStrict: true,
saxTrim: true,
saxNormalize: true,
saxPosition: true,
saxStrictEntities: true
}

if (typeof options.formatting === 'undefined') {
options.formatting = true
}

const options = Object.assign(saxOptions, defaults, userOptions)
Object.defineProperty(this, 'options', {
value: {
saxStrict: true,
saxTrim: true,
saxNormalize: true,
saxPosition: true,
saxStrictEntities: true,
verbose: options.verbose,
formatting: options.formatting
},
value: options,
writable: true,
enumerable: true
})
Expand Down
Loading