Add support for newline in .ods cell texts (#4)
This commit is contained in:
parent
08a2d5f63c
commit
c90359bc7b
@ -8,7 +8,7 @@ Small lib to parse/understand .ods and .xsls files in the browser and node.js
|
||||
### Install
|
||||
|
||||
```sh
|
||||
npm i https://github.com/DavidBruant/ods-xlsx.git#v0.7.0
|
||||
npm i https://github.com/DavidBruant/ods-xlsx.git#v0.8.0
|
||||
```
|
||||
|
||||
|
||||
|
||||
@ -3,6 +3,44 @@ import { unzip } from 'unzipit';
|
||||
|
||||
import './types.js'
|
||||
|
||||
// https://dom.spec.whatwg.org/#interface-node
|
||||
const TEXT_NODE = 3
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Element} cell
|
||||
* @returns {string}
|
||||
*/
|
||||
function extraxtODSCellText(cell) {
|
||||
let text = '';
|
||||
const childNodes = cell.childNodes;
|
||||
|
||||
for (const child of Array.from(childNodes)) {
|
||||
if (child.nodeType === TEXT_NODE) {
|
||||
// Direct text node, append the text directly
|
||||
text += child.nodeValue;
|
||||
} else if (child.nodeName === 'text:p') {
|
||||
if (text.length > 0) {
|
||||
// Add a newline before appending new paragraph if text already exists
|
||||
text += '\n';
|
||||
}
|
||||
const pNodes = child.childNodes;
|
||||
for (const pChild of Array.from(pNodes)) {
|
||||
if (pChild.nodeType === TEXT_NODE) {
|
||||
text += pChild.nodeValue; // Append text inside <text:p>
|
||||
} else if (pChild.nodeName === 'text:line-break') {
|
||||
text += '\n'; // Append newline for <text:line-break />
|
||||
}
|
||||
}
|
||||
} else if (child.nodeName === 'text:line-break') {
|
||||
text += '\n'; // Append newline for <text:line-break /> directly under <table:table-cell>
|
||||
}
|
||||
}
|
||||
|
||||
return text.trim();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Extracts raw table content from an ODS file.
|
||||
* @param {ArrayBuffer} arrayBuffer - The ODS file.
|
||||
@ -35,7 +73,7 @@ export async function _getODSTableRawContent(arrayBuffer, parseXML) {
|
||||
let cellValue;
|
||||
|
||||
if (cellType === 'string') {
|
||||
cellValue = cell.textContent;
|
||||
cellValue = extraxtODSCellText(cell)
|
||||
} else if (cellType === 'date') {
|
||||
cellValue = cell.getAttribute('office:date-value');
|
||||
} else {
|
||||
|
||||
BIN
tests/data/cellule avec sauts.ods
Normal file
BIN
tests/data/cellule avec sauts.ods
Normal file
Binary file not shown.
@ -35,4 +35,24 @@ test('.ods cells with dates should be recognized', async t => {
|
||||
t.deepEqual(row3[0].value, 'Fanny')
|
||||
t.deepEqual(row3[1].type, 'date')
|
||||
t.deepEqual(row3[1].value, '1986-06-01')
|
||||
});
|
||||
|
||||
|
||||
test('.ods file with new lines in content is ', async t => {
|
||||
const repeatedCellFileContent = (await readFile('./tests/data/cellule avec sauts.ods')).buffer
|
||||
|
||||
const table = await getODSTableRawContent(repeatedCellFileContent);
|
||||
|
||||
const feuille1 = table.get('Feuille1')
|
||||
|
||||
const expectedValue = `Deviens génial, deviens génial
|
||||
Tu n'sais pas encore l'enfer qui t'attend
|
||||
Le regard des uns, le rejet des autres
|
||||
Si t'es bizarre, si t'es pas marrant
|
||||
Deviens génial, deviens génial
|
||||
Deviens génial, deviens génial
|
||||
Pourquoi t'aimeraient-ils seulement comme tu es ? (hein)
|
||||
Si t'es pas comme eux quand t'es naturel`
|
||||
|
||||
t.deepEqual(feuille1[0][0].value, expectedValue)
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user