UNPKG

read-excel-file

Version:

Read `.xlsx` files in a web browser or in Node.js

157 lines (156 loc) 5.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getBaseStyles = getBaseStyles; exports.getCellStyles = getCellStyles; exports.getMergedCellCoordinates = getMergedCellCoordinates; exports.getNumberFormats = getNumberFormats; exports.getRelationships = getRelationships; exports.getSheets = getSheets; exports.getWorkbookProperties = getWorkbookProperties; exports.readCells = readCells; exports.readSharedStrings = readSharedStrings; exports.readSheetDimensions = readSheetDimensions; var _dom = require("./dom.js"); function readSharedStrings(document, onOpenTag, onCloseTag, onText, state) { var sst = document.documentElement; onOpenTag('sst', null, state); // An `<si/>` element can contain a `<t/>` (simplest case) or a set of `<r/>` ("rich formatting") elements having `<t/>`. // https://docs.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.sharedstringitem?redirectedfrom=MSDN&view=openxml-2.8.1 // http://www.datypic.com/sc/ooxml/e-ssml_si-1.html (0, _dom.map)(sst, 'si', function (si) { onOpenTag('si', null, state); var t = (0, _dom.findChild)(si, 't'); if (t) { onOpenTag('t', null, state); onText(t.textContent, state); onCloseTag('t', state); } else { (0, _dom.forEach)(si, 'r', function (r) { onOpenTag('r', null, state); onOpenTag('t', null, state); onText((0, _dom.findChild)(r, 't').textContent, state); onCloseTag('t', state); onCloseTag('r', state); }); } onCloseTag('si', state); }); onCloseTag('sst', state); } function readSheetDimensions(document, onOpenTag, onCloseTag, onText, state) { var worksheet = document.documentElement; onOpenTag('worksheet', null, state); var dimensions = (0, _dom.findChild)(worksheet, 'dimension'); if (dimensions) { onOpenTag('dimension', { ref: dimensions.getAttribute('ref') }, state); onCloseTag('dimension', state); } onCloseTag('worksheet', state); } function readCells(document, onOpenTag, onCloseTag, onText, state) { var worksheet = document.documentElement; onOpenTag('worksheet', null, state); var sheetData = (0, _dom.findChild)(worksheet, 'sheetData'); (0, _dom.forEach)(sheetData, 'row', function (row) { onOpenTag('row', null, state); (0, _dom.forEach)(row, 'c', function (cell) { var attributes = { r: cell.getAttribute('r'), t: cell.getAttribute('t'), s: cell.getAttribute('s') }; onOpenTag('c', attributes, state); // A cell could have either a value or an insline string value. // * Cell value is stored as text content of a `<v>...</v>` element. // * Inline string value is stored as text content of an `<is><t>...</t></is>` element. readCellValueElement(document, cell, onOpenTag, onCloseTag, onText, state); readCellInlineStringValue(document, cell, onOpenTag, onCloseTag, onText, state); onCloseTag('c', state); }); onCloseTag('row', state); }); onCloseTag('worksheet', state); } function getMergedCellCoordinates(document) { var worksheet = document.documentElement; var mergedCells = (0, _dom.findChild)(worksheet, 'mergeCells'); var mergedCellsInfo = []; if (mergedCells) { (0, _dom.forEach)(mergedCells, 'mergeCell', function (mergedCell) { mergedCellsInfo.push(mergedCell.getAttribute('ref')); }); } return mergedCellsInfo; } function readCellValueElement(document, element, onOpenTag, onCloseTag, onText, state) { var v = (0, _dom.findChild)(element, 'v'); if (v) { onOpenTag('v', null, state); if (typeof v.textContent === 'string') { onText(v.textContent, state); } onCloseTag('v', state); } } function readCellInlineStringValue(document, element, onOpenTag, onCloseTag, onText, state) { // It seems as if in some weirdly-output "*.xlsx" files // there're non-element nodes of some weird nature. // https://gitlab.com/catamphetamine/read-excel-file/-/issues/109 // This code filters out such weird non-element nodes. var firstElementChild = (0, _dom.getFirstElementChild)(element); if (firstElementChild && (0, _dom.getTagName)(firstElementChild) === 'is') { onOpenTag('is', null, state); var firstElementChildFirstElementChild = (0, _dom.getFirstElementChild)(firstElementChild); if (firstElementChildFirstElementChild && (0, _dom.getTagName)(firstElementChildFirstElementChild) === 't') { onOpenTag('t', null, state); if (typeof firstElementChildFirstElementChild.textContent === 'string') { onText(firstElementChildFirstElementChild.textContent, state); } onCloseTag('t', state); } onCloseTag('is', state); } } function getBaseStyles(document) { var styleSheet = document.documentElement; var cellStyleXfs = (0, _dom.findChild)(styleSheet, 'cellStyleXfs'); if (cellStyleXfs) { return (0, _dom.findChildren)(cellStyleXfs, 'xf'); } return []; } function getCellStyles(document) { var styleSheet = document.documentElement; var cellXfs = (0, _dom.findChild)(styleSheet, 'cellXfs'); if (!cellXfs) { return []; } return (0, _dom.findChildren)(cellXfs, 'xf'); } function getNumberFormats(document) { var styleSheet = document.documentElement; var numberFormats = []; var numFmts = (0, _dom.findChild)(styleSheet, 'numFmts'); if (numFmts) { return (0, _dom.findChildren)(numFmts, 'numFmt'); } return []; } function getWorkbookProperties(document) { var workbook = document.documentElement; return (0, _dom.findChild)(workbook, 'workbookPr'); } function getRelationships(document) { var relationships = document.documentElement; return (0, _dom.findChildren)(relationships, 'Relationship'); } function getSheets(document) { var workbook = document.documentElement; var sheets = (0, _dom.findChild)(workbook, 'sheets'); return (0, _dom.findChildren)(sheets, 'sheet'); } //# sourceMappingURL=xlsx.js.map