diff --git a/rollup.config.js b/rollup.config.js index cb15992..2ae2f72 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -39,7 +39,7 @@ export default { // If we're building for production (npm run build // instead of npm run dev), minify - production && terser() + //production && terser() ], watch: { clearScreen: false diff --git a/scripts/App.svelte b/scripts/App.svelte index 7529402..ac998f7 100644 --- a/scripts/App.svelte +++ b/scripts/App.svelte @@ -1,5 +1,5 @@

Import fichier .ods et .xslx

+

Import

+
+

Résultat

+ {#await tableObjectSheets} + (fichier en cours d'analyse) + {:then tableObjectSheets} + {#each [...tableObjectSheets] as [sheetName, data]} +
+ {sheetName} ({data.length} lignes) + + + + {#each Object.keys(data[0]) as column} + + {/each} + + + + {#each data as row} + + {#each Object.keys(data[0]) as column} + + {/each} + + {/each} + + +
{column}
{row[column]}
+
+ {/each} + {/await} + +
+ diff --git a/scripts/front-end.js b/scripts/front-end.js index a60da5c..d19ce7e 100644 --- a/scripts/front-end.js +++ b/scripts/front-end.js @@ -2,7 +2,5 @@ import App from './App.svelte'; const app = new App({ target: document.querySelector('.svelte-main'), - props: { - name: 'from Svelte' - } + props: {} }); diff --git a/scripts/getTableRawContentFromFile.js b/scripts/getTableRawContentFromFile.js index b74cdad..4b93a7d 100644 --- a/scripts/getTableRawContentFromFile.js +++ b/scripts/getTableRawContentFromFile.js @@ -15,7 +15,7 @@ function parseXML(str){ /** * @typedef TableCellRawContent - * @prop {string} value + * @prop {string | null | undefined} value * @prop {'float' | 'percentage' | 'currency' | 'date' | 'time' | 'boolean' | 'string' | 'b' | 'd' | 'e' | 'inlineStr' | 'n' | 's' | 'str'} type * */ @@ -24,39 +24,6 @@ const ODS_TYPE = "application/vnd.oasis.opendocument.spreadsheet"; const XLSX_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" -/** - * Converts a cell value to the appropriate JavaScript type based on its cell type. - * @param {string} value - The value of the cell. - * @param {TableCellRawContent['type']} cellType - The type of the cell. - * @returns {any} The converted value. - */ -function convertCellValue(value, cellType) { - if(value === ''){ - return '' - } - - switch (cellType) { - case 'float': - case 'percentage': - case 'currency': - case 'n': // number - return parseFloat(value); - case 'date': - case 'd': // date - return new Date(value); - case 'boolean': - case 'b': // boolean - return value === '1' || value === 'true'; - case 's': // shared string - case 'inlineStr': // inline string - case 'string': - case 'e': // error - case 'time': - default: - return value; - } -} - /** * Extracts raw table content from an ODS file. * @param {File} file - The ODS file. @@ -66,7 +33,6 @@ function convertCellValue(value, cellType) { */ async function getTableRawContentFromODSFile(file, unzip, parseXML) { const zip = await unzip(file); - console.log('zip', zip) const entries = zip.entries; // Extract the content.xml file which contains the spreadsheet data @@ -179,7 +145,7 @@ async function getTableRawContentFromXSLXFile(file, unzip, parseXML) { * @param {File} file * @returns {Promise>} */ -export default function getTableRawContentFromFile(file){ +export function getTableRawContentFromFile(file){ if(file.type === ODS_TYPE) return getTableRawContentFromODSFile(file, unzip, parseXML) @@ -190,3 +156,78 @@ export default function getTableRawContentFromFile(file){ } + +/** + * Converts a cell value to the appropriate JavaScript type based on its cell type. + * @param {TableCellRawContent} _ + * @returns {number | boolean | string | Date} The converted value. + */ +function convertCellValue({value, type}) { + if(value === ''){ + return '' + } + if(value === null || value === undefined){ + return '' + } + + switch (type) { + case 'float': + case 'percentage': + case 'currency': + case 'n': // number + return parseFloat(value); + case 'date': + case 'd': // date + return new Date(value); + case 'boolean': + case 'b': // boolean + return value === '1' || value === 'true'; + case 's': // shared string + case 'inlineStr': // inline string + case 'string': + case 'e': // error + case 'time': + default: + return value; + } +} + +/** + * + * @param {TableCellRawContent[][]} rawContent + * @returns {any[]} + */ +function rawContentToObjects(rawContent){ + let [firstRow, ...dataRows] = rawContent + + /** @type {string[]} */ + //@ts-expect-error trust me, this is true 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) + let empty = true + columns.forEach((column, i) => { + const rawValue = row[i] + obj[column] = rawValue ? convertCellValue(rawValue) : '' + empty = empty && (obj[column] === '') + }) + return empty ? undefined : obj + }).filter(x => x !== undefined) // remove empty rows + +} + + +/** + * + * @param {Map} rawContentSheets + * @returns {Map} + */ +export function tableRawContentToObjects(rawContentSheets){ + return new Map( + [...rawContentSheets].map(([sheetName, rawContent]) => { + return [sheetName, rawContentToObjects(rawContent)] + }) + ) +} +