diff --git a/readme.md b/readme.md index 254d5a9..3a35f3c 100644 --- a/readme.md +++ b/readme.md @@ -8,7 +8,7 @@ Small lib to parse/understand .ods and .xsls files in the browser and node.js ### Install ```sh -npm i github:DavidBruant/ods-xlsx#v0.4.0 +npm i github:DavidBruant/ods-xlsx#v0.5.0 ``` diff --git a/scripts/main.js b/scripts/main.js index 48c2890..af19b9d 100644 --- a/scripts/main.js +++ b/scripts/main.js @@ -3,11 +3,12 @@ let _DOMParser if(typeof DOMParser !== 'undefined' && Object(DOMParser) === DOMParser && DOMParser.prototype && typeof DOMParser.prototype.parseFromString === 'function'){ - console.info('[ods-xlsx] Already existing DOMParser. Certainly in the browser') + //console.info('[ods-xlsx] Already existing DOMParser. Certainly in the browser') + _DOMParser = DOMParser } else{ - console.info('[ods-xlsx] No native DOMParser. Certainly in Node.js') + //console.info('[ods-xlsx] No native DOMParser. Certainly in Node.js') const xmldom = await import('@xmldom/xmldom') _DOMParser = xmldom.DOMParser @@ -40,11 +41,22 @@ export function getXLSXTableRawContent(xlsxArrBuff){ export { - isRowNotEmpty, // table-level exports tableWithoutEmptyRows, tableRawContentToValues, tableRawContentToStrings, tableRawContentToObjects, + + // sheet-level exports + sheetRawContentToObjects, + sheetRawContentToStrings, + + // row-level exports + rowRawContentToStrings, + isRowNotEmpty, + + // cell-level exports + cellRawContentToStrings, + convertCellValue } from './shared.js' diff --git a/scripts/shared.js b/scripts/shared.js index ab1cedf..fd6dcf1 100644 --- a/scripts/shared.js +++ b/scripts/shared.js @@ -111,13 +111,12 @@ export async function _getXLSXTableRawContent(arrayBuffer, parseXML) { return new Map(await Promise.all(sheetDataPs)); } - /** * Converts a cell value to the appropriate JavaScript type based on its cell type. * @param {SheetCellRawContent} _ * @returns {number | boolean | string | Date} The converted value. */ -function convertCellValue({value, type}) { +export function convertCellValue({value, type}) { if(value === ''){ return '' } @@ -149,29 +148,8 @@ function convertCellValue({value, type}) { -/** - * - * @param {SheetRawContent} rawContent - * @returns {any[]} - */ -export function rawContentToObjects(rawContent){ - let [firstRow, ...dataRows] = rawContent - /** @type {string[]} */ - //@ts-expect-error this type is correct after the filter - const columns = firstRow.filter(({value}) => typeof value === 'string' && value.length >= 1).map(r => r.value) - return dataRows - .map(row => { - const obj = Object.create(null) - columns.forEach((column, i) => { - const rawValue = row[i] - obj[column] = rawValue ? convertCellValue(rawValue) : '' - }) - return obj - }) - -} /** @@ -191,6 +169,37 @@ export function tableRawContentToValues(rawContentSheets){ ) } +/** + * Convert values to strings + */ + +/** + * + * @param {SheetCellRawContent} rawContentCell + * @returns {string} + */ +export function cellRawContentToStrings(rawContentCell){ + return rawContentCell.value || '' +} + +/** + * + * @param {SheetRowRawContent} rawContentRow + * @returns {string[]} + */ +export function rowRawContentToStrings(rawContentRow){ + return rawContentRow.map(cellRawContentToStrings) +} + +/** + * + * @param {SheetRawContent} rawContentSheet + * @returns {string[][]} + */ +export function sheetRawContentToStrings(rawContentSheet){ + return rawContentSheet.map(rowRawContentToStrings) +} + /** * * @param {Map} rawContentSheets @@ -199,17 +208,45 @@ export function tableRawContentToValues(rawContentSheets){ export function tableRawContentToStrings(rawContentSheets){ return new Map( [...rawContentSheets].map(([sheetName, rawContent]) => { - return [ - sheetName, - rawContent - .map(row => row.map(c => (c.value || ''))) - ] + return [ sheetName, sheetRawContentToStrings(rawContent) ] }) ) } + + +/** + * Convert rows to objects + */ + +/** + * This function expects the first row to contain string values which are used as column names + * It outputs an array of objects which keys are + * + * @param {SheetRawContent} rawContent + * @returns {any[]} + */ +export function sheetRawContentToObjects(rawContent){ + let [firstRow, ...dataRows] = rawContent + + /** @type {string[]} */ + //@ts-expect-error this type is correct after the filter + const columns = firstRow.filter(({value}) => typeof value === 'string' && value.length >= 1).map(r => r.value) + + return dataRows + .map(row => { + const obj = Object.create(null) + columns.forEach((column, i) => { + const rawValue = row[i] + obj[column] = rawValue ? convertCellValue(rawValue) : '' + }) + return obj + }) + +} + /** * * @param {Map} rawContentSheets @@ -218,12 +255,18 @@ export function tableRawContentToStrings(rawContentSheets){ export function tableRawContentToObjects(rawContentSheets){ return new Map( [...rawContentSheets].map(([sheetName, rawContent]) => { - return [sheetName, rawContentToObjects(rawContent)] + return [sheetName, sheetRawContentToObjects(rawContent)] }) ) } + + +/** + * Emptiness + */ + /** * @param {SheetCellRawContent} rawCellContent * @returns {boolean}