Skip to content

Commit aac4e6c

Browse files
committed
feat & test: support rich text
* support rich text string * add rich text test case * add saxTrim option for keep blank in string or not
1 parent 4fce2ec commit aac4e6c

File tree

5 files changed

+30
-5
lines changed

5 files changed

+30
-5
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Options
2424
|---|---|---|
2525
|verbose|true|throw additional exceptions, if `false` - then pass empty string in that places|
2626
|formatting|true|should cells with combined formats be formatted or not|
27+
|saxTrim|true|whether or not to trim text and comment nodes|
2728

2829
-------
2930
```javascript

lib/workbook.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -313,12 +313,17 @@ XlsxStreamReaderWorkBook.prototype._parseSharedStrings = function (nodeData) {
313313
var nodeObjValue = nodeData.pop()
314314
var nodeObjName = nodeData.pop()
315315

316-
if (nodeObjName && nodeObjName.name === 't') {
317-
self.workBookSharedStrings.push(nodeObjValue)
318-
} else {
319-
if (nodeObjValue && typeof nodeObjValue === 'object' && nodeObjValue.hasOwnProperty('name') && nodeObjValue.name === 't') {
316+
var isSharedStringItem = !!nodeData.find((n) => n && n.name === 'si') // <si> tag
317+
if (isSharedStringItem) {
318+
if (nodeObjName && nodeObjName.name === 't') {
319+
self.workBookSharedStrings.push(nodeObjValue)
320+
} else {
320321
self.workBookSharedStrings.push('')
321322
}
323+
} else {
324+
if (nodeObjName && nodeObjName.name === 't') {
325+
self.workBookSharedStrings[self.workBookSharedStrings.length - 1] += nodeObjValue
326+
}
322327
}
323328
}
324329

lib/xlsx-stream-reader.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ function XlsxStreamReader (options) {
1919
options = {}
2020
}
2121

22+
if (typeof options.saxTrim === 'undefined') {
23+
options.saxTrim = true
24+
}
25+
2226
if (typeof options.verbose === 'undefined') {
2327
options.verbose = true
2428
}
@@ -30,10 +34,10 @@ function XlsxStreamReader (options) {
3034
Object.defineProperty(this, 'options', {
3135
value: {
3236
saxStrict: true,
33-
saxTrim: true,
3437
saxNormalize: true,
3538
saxPosition: true,
3639
saxStrictEntities: true,
40+
saxTrim: options.saxTrim,
3741
verbose: options.verbose,
3842
formatting: options.formatting
3943
},

test/richtext.xlsx

4.92 KB
Binary file not shown.

test/test.js

+15
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,19 @@ describe('The xslx stream parser', function () {
7272
workSheetReader.process()
7373
})
7474
})
75+
it('support rich-text', function (done) {
76+
const workBookReader = new XlsxStreamReader({saxTrim: false})
77+
fs.createReadStream(path.join(__dirname, 'richtext.xlsx')).pipe(workBookReader)
78+
const rows = []
79+
workBookReader.on('worksheet', function (workSheetReader) {
80+
workSheetReader.on('end', function () {
81+
assert(rows[0][1] === 'This is only one sentence.')
82+
done()
83+
})
84+
workSheetReader.on('row', function (r) {
85+
rows.push(r.values)
86+
})
87+
workSheetReader.process()
88+
})
89+
})
7590
})

0 commit comments

Comments
 (0)