read-excel-file
Version:
Read `.xlsx` files in a web browser or in Node.js
122 lines (120 loc) • 4.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getBaseStyles = getBaseStyles;
exports.getCellElements = getCellElements;
exports.getCellInlineStringValue = getCellInlineStringValue;
exports.getCellStyles = getCellStyles;
exports.getCellValueElement = getCellValueElement;
exports.getDimensions = getDimensions;
exports.getMergedCellCoordinates = getMergedCellCoordinates;
exports.getNumberFormats = getNumberFormats;
exports.getRelationships = getRelationships;
exports.getSharedStrings = getSharedStrings;
exports.getSheets = getSheets;
exports.getWorkbookProperties = getWorkbookProperties;
var _dom = require("./dom.js");
// Returns an array of cells,
// each element being an XML DOM element representing a cell.
function getCellElements(document) {
var worksheet = document.documentElement;
var sheetData = (0, _dom.findChild)(worksheet, 'sheetData');
var cells = [];
(0, _dom.forEach)(sheetData, 'row', function (row) {
(0, _dom.forEach)(row, 'c', function (cell) {
cells.push(cell);
});
});
return cells;
}
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 getCellValueElement(document, element) {
return (0, _dom.findChild)(element, 'v');
}
function getCellInlineStringValue(document, element) {
// 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') {
var firstElementChildFirstElementChild = (0, _dom.getFirstElementChild)(firstElementChild);
if (firstElementChildFirstElementChild && (0, _dom.getTagName)(firstElementChildFirstElementChild) === 't') {
return firstElementChildFirstElementChild.textContent;
}
}
}
function getDimensions(document) {
var worksheet = document.documentElement;
var dimensions = (0, _dom.findChild)(worksheet, 'dimension');
if (dimensions) {
return dimensions.getAttribute('ref');
}
}
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 getSharedStrings(document) {
// 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
var sst = document.documentElement;
return (0, _dom.map)(sst, 'si', function (string) {
var t = (0, _dom.findChild)(string, 't');
if (t) {
return t.textContent;
}
var value = '';
(0, _dom.forEach)(string, 'r', function (r) {
value += (0, _dom.findChild)(r, 't').textContent;
});
return value;
});
}
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