Prise en compte des valeurs dates et des cellules répétées dans les fichiers .ods

This commit is contained in:
David Bruant 2024-07-08 15:06:36 +02:00
parent 78fc692d95
commit ba2ca08b33
5 changed files with 65 additions and 6 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
node_modules/
build/*
.~lock*

View File

@ -32,12 +32,27 @@ export async function _getODSTableRawContent(arrayBuffer, parseXML) {
for (let cell of Array.from(cells)) {
const cellType = cell.getAttribute('office:value-type');
const cellValue = cellType === 'string' ? cell.textContent : cell.getAttribute('office:value');
let cellValue;
if (cellType === 'string') {
cellValue = cell.textContent;
} else if (cellType === 'date') {
cellValue = cell.getAttribute('office:date-value');
} else {
cellValue = cell.getAttribute('office:value');
}
const numberOfColumnsRepeated = cell.getAttribute('table:number-columns-repeated');
const repeatCount = numberOfColumnsRepeated ? parseInt(numberOfColumnsRepeated, 10) : 1;
if(repeatCount < 100){ // ignore excessive repetitions
for (let i = 0; i < repeatCount; i++) {
rowData.push({
value: cellValue,
type: cellType
});
}
}
}
sheetData.push(rowData);
}
@ -48,6 +63,7 @@ export async function _getODSTableRawContent(arrayBuffer, parseXML) {
return tableMap;
}
/**
* Extracts raw table content from an XLSX file.
* @param {ArrayBuffer} arrayBuffer - The XLSX file.

Binary file not shown.

Binary file not shown.

41
tests/ods-files.js Normal file
View File

@ -0,0 +1,41 @@
import {readFile} from 'node:fs/promises'
import test from 'ava';
import {getODSTableRawContent} from '../scripts/main.js'
test('.ods file with table:number-columns-repeated attribute in cell', async t => {
const repeatedCellFileContent = (await readFile('./tests/data/cellules-répétées.ods')).buffer
const table = await getODSTableRawContent(repeatedCellFileContent);
const feuille1 = table.get('Feuille 1')
console.log('yo repeated', feuille1)
t.deepEqual(feuille1[0].length, feuille1[1].length, `First and second row should have the same number of columns`)
});
test('.ods cells with dates should be recognized', async t => {
const odsFileWithDates = (await readFile('./tests/data/cellules avec dates.ods')).buffer
const table = await getODSTableRawContent(odsFileWithDates);
const feuille1 = table.get('Feuille1')
console.log('yo dates', feuille1)
const row1 = feuille1[0]
t.deepEqual(row1[0].value, 'Nom')
t.deepEqual(row1[1].value, 'Date de naissance')
const row2 = feuille1[1]
t.deepEqual(row2[0].value, 'Dav')
t.deepEqual(row2[1].type, 'date')
t.deepEqual(row2[1].value, '1987-03-08')
const row3 = feuille1[2]
t.deepEqual(row3[0].value, 'Fanny')
t.deepEqual(row3[1].type, 'date')
t.deepEqual(row3[1].value, '1986-06-01')
});