officeparser 5.2.1 → 5.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/officeParser.js +24 -10
  2. package/package.json +1 -1
package/officeParser.js CHANGED
@@ -235,12 +235,19 @@ function parseExcel(file, callback, config) {
235
235
  && cNode.getElementsByTagName("v")[0].childNodes[0].nodeValue != ''
236
236
  }
237
237
 
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
+
241
243
  /** 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
+ });
244
251
 
245
252
  // Parse Sheet files
246
253
  xmlContentFilesObject.sheetFiles.forEach(sheetXmlContent => {
@@ -261,13 +268,14 @@ function parseExcel(file, callback, config) {
261
268
  /** Flag whether this node's value represents an index in the shared string array */
262
269
  const isIndexInSharedStrings = cNode.getAttribute("t") == "s";
263
270
  /** Find value nodes represented by v tags */
264
- const value = parseInt(cNode.getElementsByTagName("v")[0].childNodes[0].nodeValue, 10);
271
+ const value = cNode.getElementsByTagName("v")[0].childNodes[0].nodeValue;
272
+ const valueAsIndex = Number(value);
265
273
  // Validate text
266
- if (isIndexInSharedStrings && value >= sharedStrings.length)
274
+ if (isIndexInSharedStrings && (valueAsIndex != parseInt(value, 10) || valueAsIndex >= sharedStrings.length))
267
275
  throw ERRORMSG.fileCorrupted(file);
268
276
 
269
277
  return isIndexInSharedStrings
270
- ? sharedStrings[value]
278
+ ? sharedStrings[valueAsIndex]
271
279
  : value;
272
280
  }
273
281
  // Should not reach here. If we do, it means we are not filtering out items that we are not ready to process.
@@ -372,13 +380,19 @@ function parseOpenOffice(file, callback, config) {
372
380
  function traversal(node, xmlTextArray, isFirstRecursion) {
373
381
  if (!node.childNodes || node.childNodes.length == 0) {
374
382
  if (node.parentNode.tagName.indexOf('text') == 0 && node.nodeValue) {
383
+ // If the corresponding value is of type float, we take the value from office:value attribute.
384
+ // However, it is not on the parentNode but rather grandparentNode.
385
+ const value = node.parentNode.parentNode?.getAttribute('office:value-type') == 'float'
386
+ ? Number(node.parentNode.parentNode.getAttribute('office:value'))
387
+ : node.nodeValue;
388
+
375
389
  if (isNotesNode(node.parentNode) && (config.putNotesAtLast || config.ignoreNotes)) {
376
- notesText.push(node.nodeValue);
390
+ notesText.push(value);
377
391
  if (allowedTextTags.includes(node.parentNode.tagName) && !isFirstRecursion)
378
392
  notesText.push(config.newlineDelimiter ?? "\n");
379
393
  }
380
394
  else {
381
- xmlTextArray.push(node.nodeValue);
395
+ xmlTextArray.push(value);
382
396
  if (allowedTextTags.includes(node.parentNode.tagName) && !isFirstRecursion)
383
397
  xmlTextArray.push(config.newlineDelimiter ?? "\n");
384
398
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "officeparser",
3
- "version": "5.2.1",
3
+ "version": "5.2.2",
4
4
  "description": "A Node.js library to parse text out of any office file. Currently supports docx, pptx, xlsx, odt, odp, ods, pdf files.",
5
5
  "main": "officeParser.js",
6
6
  "files": [