Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e6e09a0361 | |||
| 3d98e41d2b | |||
| 7c87d3220d | |||
| 8b60ceb39c | |||
|
|
c677c7267b | ||
|
|
415a26e3f1 | ||
|
|
90b97e23e9 | ||
|
|
02d5338634 |
4
package-lock.json
generated
4
package-lock.json
generated
@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "@odfjs/odfjs",
|
"name": "@odfjs/odfjs",
|
||||||
"version": "0.29.0",
|
"version": "0.30.0",
|
||||||
"lockfileVersion": 2,
|
"lockfileVersion": 2,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@odfjs/odfjs",
|
"name": "@odfjs/odfjs",
|
||||||
"version": "0.29.0",
|
"version": "0.30.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@xmldom/xmldom": "^0.9.8",
|
"@xmldom/xmldom": "^0.9.8",
|
||||||
"@zip.js/zip.js": "^2.7.57",
|
"@zip.js/zip.js": "^2.7.57",
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@odfjs/odfjs",
|
"name": "@odfjs/odfjs",
|
||||||
"version": "0.29.0",
|
"version": "0.30.0",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"exports": "./exports.js",
|
"exports": "./exports.js",
|
||||||
"files": [
|
"files": [
|
||||||
@ -21,7 +21,7 @@
|
|||||||
"test": "ava"
|
"test": "ava"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"url": "https://github.com/odfjs/odfjs.git"
|
"url": "https://source.netsyms.com/PostalPortal/odfjs.git"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@rollup/plugin-commonjs": "^25.0.7",
|
"@rollup/plugin-commonjs": "^25.0.7",
|
||||||
|
|||||||
@ -18,7 +18,7 @@ Small lib to parse/understand .odf files (.odt, .ods) in the browser and node.js
|
|||||||
### Install
|
### Install
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
npm i https://github.com/odfjs/odfjs.git#v0.28.0
|
npm i https://github.com/odfjs/odfjs.git#v0.30.0
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
@ -158,4 +158,3 @@ npm run dev
|
|||||||
I hope to be credited for the work on this repo
|
I hope to be credited for the work on this repo
|
||||||
|
|
||||||
Everything written by me and contributors to this repo is licenced under **CC0 1.0 (Public Domain)**
|
Everything written by me and contributors to this repo is licenced under **CC0 1.0 (Public Domain)**
|
||||||
|
|
||||||
|
|||||||
@ -10,7 +10,11 @@ const stylesXml = `<?xml version="1.0" encoding="UTF-8"?>
|
|||||||
xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
|
xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
|
||||||
xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
|
xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
|
||||||
office:version="1.2">
|
office:version="1.2">
|
||||||
<office:styles/>
|
<office:styles>
|
||||||
|
<style:style style:name="boldcell" style:family="table-cell">
|
||||||
|
<style:text-properties fo:font-weight="bold"/>
|
||||||
|
</style:style>
|
||||||
|
</office:styles>
|
||||||
<office:automatic-styles/>
|
<office:automatic-styles/>
|
||||||
<office:master-styles/>
|
<office:master-styles/>
|
||||||
</office:document-styles>`;
|
</office:document-styles>`;
|
||||||
@ -27,7 +31,7 @@ const manifestXml = `<?xml version="1.0" encoding="UTF-8"?>
|
|||||||
* @param {Map<SheetName, SheetRawContent>} sheetsData
|
* @param {Map<SheetName, SheetRawContent>} sheetsData
|
||||||
* @returns {Promise<ArrayBuffer>}
|
* @returns {Promise<ArrayBuffer>}
|
||||||
*/
|
*/
|
||||||
export async function createOdsFile(sheetsData) {
|
export async function createOdsFile(sheetsData, currencyData = null) {
|
||||||
// Create a new zip writer
|
// Create a new zip writer
|
||||||
const zipWriter = new ZipWriter(new BlobWriter('application/vnd.oasis.opendocument.spreadsheet'));
|
const zipWriter = new ZipWriter(new BlobWriter('application/vnd.oasis.opendocument.spreadsheet'));
|
||||||
|
|
||||||
@ -45,7 +49,7 @@ export async function createOdsFile(sheetsData) {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
const contentXml = generateContentFileXMLString(sheetsData);
|
const contentXml = generateContentFileXMLString(sheetsData, currencyData);
|
||||||
zipWriter.add("content.xml", new TextReader(contentXml), {level: 9});
|
zipWriter.add("content.xml", new TextReader(contentXml), {level: 9});
|
||||||
|
|
||||||
zipWriter.add("styles.xml", new TextReader(stylesXml));
|
zipWriter.add("styles.xml", new TextReader(stylesXml));
|
||||||
@ -63,7 +67,7 @@ export async function createOdsFile(sheetsData) {
|
|||||||
* @param {Map<SheetName, SheetRawContent>} sheetsData
|
* @param {Map<SheetName, SheetRawContent>} sheetsData
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
function generateContentFileXMLString(sheetsData) {
|
function generateContentFileXMLString(sheetsData, currencyData) {
|
||||||
const doc = createDocument('urn:oasis:names:tc:opendocument:xmlns:office:1.0', 'office:document-content');
|
const doc = createDocument('urn:oasis:names:tc:opendocument:xmlns:office:1.0', 'office:document-content');
|
||||||
const root = doc.documentElement;
|
const root = doc.documentElement;
|
||||||
|
|
||||||
@ -75,6 +79,52 @@ function generateContentFileXMLString(sheetsData) {
|
|||||||
root.setAttribute('xmlns:fo', 'urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0');
|
root.setAttribute('xmlns:fo', 'urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0');
|
||||||
root.setAttribute('office:version', '1.2');
|
root.setAttribute('office:version', '1.2');
|
||||||
|
|
||||||
|
const styleNode = doc.createElement("office:automatic-styles");
|
||||||
|
|
||||||
|
var currencyStyleName = "currencyStyle";
|
||||||
|
if (currencyData != null) {
|
||||||
|
currencyStyleName = `currency${currencyData.currencyCode.toUpperCase()}`;
|
||||||
|
const numberStyle = doc.createElement("number:currency-style");
|
||||||
|
numberStyle.setAttribute("style:name", currencyStyleName);
|
||||||
|
|
||||||
|
const numberCurrencySymbolStyle = doc.createElement("number:currency-symbol");
|
||||||
|
numberCurrencySymbolStyle.setAttribute("number:language", "en");
|
||||||
|
numberCurrencySymbolStyle.setAttribute("number:country", currencyData.countryCode.toUpperCase());
|
||||||
|
numberCurrencySymbolStyle.textContent = currencyData.currencySymbol;
|
||||||
|
numberStyle.appendChild(numberCurrencySymbolStyle);
|
||||||
|
|
||||||
|
const numberCurrencyStyle = doc.createElement("number:number");
|
||||||
|
numberCurrencyStyle.setAttribute("number:min-integer-digits", "1");
|
||||||
|
numberCurrencyStyle.setAttribute("number:decimal-places", `${currencyData.decimalPlaces}`);
|
||||||
|
numberCurrencyStyle.setAttribute("number:min-decimal-places", `${currencyData.decimalPlaces}`);
|
||||||
|
numberCurrencyStyle.setAttribute("number:grouping", "true");
|
||||||
|
numberStyle.appendChild(numberCurrencyStyle);
|
||||||
|
|
||||||
|
styleNode.appendChild(numberStyle);
|
||||||
|
|
||||||
|
const currencyCellStyleNode = doc.createElement("style:style");
|
||||||
|
currencyCellStyleNode.setAttribute("style:name", "currencycell");
|
||||||
|
currencyCellStyleNode.setAttribute("style:family", "table-cell");
|
||||||
|
currencyCellStyleNode.setAttribute("style:data-style-name", currencyStyleName);
|
||||||
|
|
||||||
|
const currencyCellTableCellProperties = doc.createElement("style:table-cell-properties");
|
||||||
|
|
||||||
|
currencyCellStyleNode.appendChild(currencyCellTableCellProperties);
|
||||||
|
|
||||||
|
styleNode.appendChild(currencyCellStyleNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
const boldCellStyleNode = doc.createElement("style:style");
|
||||||
|
boldCellStyleNode.setAttribute("style:name", "boldcell");
|
||||||
|
boldCellStyleNode.setAttribute("style:family", "table-cell");
|
||||||
|
const boldCellTextPropsNode = doc.createElement("style:text-properties");
|
||||||
|
boldCellTextPropsNode.setAttribute("fo:font-weight", "bold");
|
||||||
|
boldCellStyleNode.appendChild(boldCellTextPropsNode);
|
||||||
|
styleNode.appendChild(boldCellStyleNode);
|
||||||
|
|
||||||
|
|
||||||
|
root.appendChild(styleNode);
|
||||||
|
|
||||||
const bodyNode = doc.createElement('office:body');
|
const bodyNode = doc.createElement('office:body');
|
||||||
root.appendChild(bodyNode);
|
root.appendChild(bodyNode);
|
||||||
|
|
||||||
@ -87,8 +137,30 @@ function generateContentFileXMLString(sheetsData) {
|
|||||||
tableNode.setAttribute('table:name', sheetName);
|
tableNode.setAttribute('table:name', sheetName);
|
||||||
spreadsheetNode.appendChild(tableNode);
|
spreadsheetNode.appendChild(tableNode);
|
||||||
|
|
||||||
const columnNode = doc.createElement('table:table-column');
|
var columnsWidthChars = {};
|
||||||
tableNode.appendChild(columnNode);
|
for (let r = 0; r < sheetData.length; r++) {
|
||||||
|
for (let c = 0; c < sheetData[r].length; c++) {
|
||||||
|
var len = ((sheetData[r][c].display ?? sheetData[r][c].value) + "").length;
|
||||||
|
if (typeof columnsWidthChars[c] == "undefined") {
|
||||||
|
columnsWidthChars[c] = len;
|
||||||
|
}
|
||||||
|
columnsWidthChars[c] = Math.max(columnsWidthChars[c], len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var prop in columnsWidthChars) {
|
||||||
|
var columnNode = doc.createElement('table:table-column');
|
||||||
|
columnNode.setAttribute("table:style-name", "colwidth" + columnsWidthChars[prop]);
|
||||||
|
tableNode.appendChild(columnNode);
|
||||||
|
|
||||||
|
var columnWidthNode = doc.createElement("style:style");
|
||||||
|
columnWidthNode.setAttribute("style:name", "colwidth" + columnsWidthChars[prop]);
|
||||||
|
columnWidthNode.setAttribute("style:family", "table-column");
|
||||||
|
const columnWidthPropsNode = doc.createElement("style:table-column-properties");
|
||||||
|
columnWidthPropsNode.setAttribute("style:column-width", `${columnsWidthChars[prop] * 0.26}cm`);
|
||||||
|
columnWidthNode.appendChild(columnWidthPropsNode);
|
||||||
|
styleNode.appendChild(columnWidthNode);
|
||||||
|
}
|
||||||
|
|
||||||
// Iterate through rows
|
// Iterate through rows
|
||||||
sheetData.forEach((row) => {
|
sheetData.forEach((row) => {
|
||||||
@ -101,6 +173,10 @@ function generateContentFileXMLString(sheetsData) {
|
|||||||
const cellType = convertCellType(cell.type);
|
const cellType = convertCellType(cell.type);
|
||||||
cellNode.setAttribute('office:value-type', cellType);
|
cellNode.setAttribute('office:value-type', cellType);
|
||||||
|
|
||||||
|
if (cell.style && cell.style == "bold") {
|
||||||
|
cellNode.setAttribute('table:style-name', "boldcell");
|
||||||
|
}
|
||||||
|
|
||||||
// Add value attribute based on type
|
// Add value attribute based on type
|
||||||
if (cell.value !== null && cell.value !== undefined) {
|
if (cell.value !== null && cell.value !== undefined) {
|
||||||
switch (cellType) {
|
switch (cellType) {
|
||||||
@ -111,6 +187,14 @@ function generateContentFileXMLString(sheetsData) {
|
|||||||
cellNode.setAttribute('office:value', cell.value.toString());
|
cellNode.setAttribute('office:value', cell.value.toString());
|
||||||
cellNode.setAttribute('office:value-type', 'percentage');
|
cellNode.setAttribute('office:value-type', 'percentage');
|
||||||
break;
|
break;
|
||||||
|
case 'currency':
|
||||||
|
cellNode.setAttribute('office:value', cell.value.toString());
|
||||||
|
cellNode.setAttribute('office:value-type', 'currency');
|
||||||
|
if (currencyData != null) {
|
||||||
|
cellNode.setAttribute("table:style-name", "currencycell");
|
||||||
|
cellNode.setAttribute('office:currency', currencyData.currencyCode.toUpperCase());
|
||||||
|
}
|
||||||
|
break;
|
||||||
case 'date':
|
case 'date':
|
||||||
cellNode.setAttribute('office:date-value', cell.value.toString());
|
cellNode.setAttribute('office:date-value', cell.value.toString());
|
||||||
break;
|
break;
|
||||||
@ -126,7 +210,11 @@ function generateContentFileXMLString(sheetsData) {
|
|||||||
|
|
||||||
if (cellType !== 'string') {
|
if (cellType !== 'string') {
|
||||||
const textNode = doc.createElement('text:p');
|
const textNode = doc.createElement('text:p');
|
||||||
textNode.textContent = cell.value.toString();
|
if (typeof cell.display != "undefined") {
|
||||||
|
textNode.textContent = cell.display.toString();
|
||||||
|
} else {
|
||||||
|
textNode.textContent = cell.value.toString();
|
||||||
|
}
|
||||||
cellNode.appendChild(textNode);
|
cellNode.appendChild(textNode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -33,7 +33,7 @@ function extraxtODSCellText(cell) {
|
|||||||
text += pChild.nodeValue; // Append text inside <text:p>
|
text += pChild.nodeValue; // Append text inside <text:p>
|
||||||
} else if (pChild.nodeName === 'text:line-break') {
|
} else if (pChild.nodeName === 'text:line-break') {
|
||||||
text += '\n'; // Append newline for <text:line-break />
|
text += '\n'; // Append newline for <text:line-break />
|
||||||
} else if (pChild.nodeName === 'text:a') {
|
} else if (pChild.nodeName === 'text:a' || pChild.nodeName === 'text:span') {
|
||||||
text += pChild.textContent
|
text += pChild.textContent
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
tests/fixtures/cellule avec style.ods
vendored
Normal file
BIN
tests/fixtures/cellule avec style.ods
vendored
Normal file
Binary file not shown.
@ -76,3 +76,13 @@ test('.ods cells with mails should be recognized', async t => {
|
|||||||
t.deepEqual(row3[0].value, 'Fanny')
|
t.deepEqual(row3[0].value, 'Fanny')
|
||||||
t.deepEqual(row3[1].value, 'lemaildeFanny@example.com')
|
t.deepEqual(row3[1].value, 'lemaildeFanny@example.com')
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('.ods cells with partially styled content should be recognized', async t => {
|
||||||
|
const odsFileWithStyle = (await readFile('./tests/fixtures/cellule avec style.ods')).buffer;
|
||||||
|
const table = await getODSTableRawContent(odsFileWithStyle);
|
||||||
|
|
||||||
|
const feuille1 = table.get('Feuille1');
|
||||||
|
|
||||||
|
const row1 = feuille1[0];
|
||||||
|
t.deepEqual(row1[0].value, 'Toto titi');
|
||||||
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user