Skip to content

Commit 269b502

Browse files
committed
Fixed #66 where order of text would get messed up when part of a text in a cell differs in formatting.
1 parent 457f088 commit 269b502

2 files changed

Lines changed: 13 additions & 6 deletions

File tree

officeParser.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,19 @@ function parseExcel(file, callback, config) {
235235
&& cNode.getElementsByTagName("v")[0].childNodes[0].nodeValue != ''
236236
}
237237

238-
/** Find text nodes with t tags in sharedStrings xml file. If the sharedStringsFile is not present, we return an empty array. */
239-
const sharedStringsXmlTNodesList = xmlContentFilesObject.sharedStringsFile != undefined ? parseString(xmlContentFilesObject.sharedStringsFile).getElementsByTagName("t")
240-
: [];
238+
/** Find text nodes with t tags in sharedStrings.xml file. If the sharedStringsFile is not present, we return an empty array. */
239+
const sharedStringsXmlSiNodesList = xmlContentFilesObject.sharedStringsFile != undefined
240+
? parseString(xmlContentFilesObject.sharedStringsFile).getElementsByTagName("si")
241+
: [];
242+
241243
/** Create shared string array. This will be used as a map to get strings from within sheet files. */
242-
const sharedStrings = Array.from(sharedStringsXmlTNodesList)
243-
.map(tNode => tNode.childNodes[0]?.nodeValue ?? '');
244+
const sharedStrings = Array.from(sharedStringsXmlSiNodesList)
245+
.map(siNode => {
246+
// Concatenate all <t> nodes within the <si> node
247+
return Array.from(siNode.getElementsByTagName("t"))
248+
.map(tNode => tNode.childNodes[0]?.nodeValue ?? '') // Extract text content from each <t> node
249+
.join(''); // Combine all <t> node text into a single string
250+
});
244251

245252
// Parse Sheet files
246253
xmlContentFilesObject.sheetFiles.forEach(sheetXmlContent => {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "officeparser",
3-
"version": "5.2.1",
3+
"version": "5.2.2",
44
"description": "A Node.js library to parse text out of any office file. Currently supports docx, pptx, xlsx, odt, odp, ods, pdf files.",
55
"main": "officeParser.js",
66
"files": [

0 commit comments

Comments
 (0)