Skip to content

Commit 457f088

Browse files
committed
Fixed #69 where Excel numbers were parsed as Int even when they didn't represent index in sharedStrings array. Further, the extracted float numbers from openOffice files were not precise enough. Fixed that too.
1 parent d188e82 commit 457f088

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

officeParser.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,14 @@ function parseExcel(file, callback, config) {
261261
/** Flag whether this node's value represents an index in the shared string array */
262262
const isIndexInSharedStrings = cNode.getAttribute("t") == "s";
263263
/** Find value nodes represented by v tags */
264-
const value = parseInt(cNode.getElementsByTagName("v")[0].childNodes[0].nodeValue, 10);
264+
const value = cNode.getElementsByTagName("v")[0].childNodes[0].nodeValue;
265+
const valueAsIndex = Number(value);
265266
// Validate text
266-
if (isIndexInSharedStrings && value >= sharedStrings.length)
267+
if (isIndexInSharedStrings && (valueAsIndex != parseInt(value, 10) || valueAsIndex >= sharedStrings.length))
267268
throw ERRORMSG.fileCorrupted(file);
268269

269270
return isIndexInSharedStrings
270-
? sharedStrings[value]
271+
? sharedStrings[valueAsIndex]
271272
: value;
272273
}
273274
// Should not reach here. If we do, it means we are not filtering out items that we are not ready to process.
@@ -372,13 +373,19 @@ function parseOpenOffice(file, callback, config) {
372373
function traversal(node, xmlTextArray, isFirstRecursion) {
373374
if (!node.childNodes || node.childNodes.length == 0) {
374375
if (node.parentNode.tagName.indexOf('text') == 0 && node.nodeValue) {
376+
// If the corresponding value is of type float, we take the value from office:value attribute.
377+
// However, it is not on the parentNode but rather grandparentNode.
378+
const value = node.parentNode.parentNode?.getAttribute('office:value-type') == 'float'
379+
? Number(node.parentNode.parentNode.getAttribute('office:value'))
380+
: node.nodeValue;
381+
375382
if (isNotesNode(node.parentNode) && (config.putNotesAtLast || config.ignoreNotes)) {
376-
notesText.push(node.nodeValue);
383+
notesText.push(value);
377384
if (allowedTextTags.includes(node.parentNode.tagName) && !isFirstRecursion)
378385
notesText.push(config.newlineDelimiter ?? "\n");
379386
}
380387
else {
381-
xmlTextArray.push(node.nodeValue);
388+
xmlTextArray.push(value);
382389
if (allowedTextTags.includes(node.parentNode.tagName) && !isFirstRecursion)
383390
xmlTextArray.push(config.newlineDelimiter ?? "\n");
384391
}

0 commit comments

Comments
 (0)