2024-06-16 14:31:57 +02:00
|
|
|
# ods-xlsx
|
2024-06-14 16:57:20 +02:00
|
|
|
|
2024-06-18 11:06:41 +02:00
|
|
|
Small lib to parse/understand .ods and .xsls files in the browser and node.js
|
2024-06-14 16:57:20 +02:00
|
|
|
|
|
|
|
|
|
2024-06-16 14:31:57 +02:00
|
|
|
## Usage
|
|
|
|
|
|
|
|
|
|
### Install
|
|
|
|
|
|
|
|
|
|
```sh
|
2024-10-23 21:07:46 +02:00
|
|
|
npm i https://github.com/DavidBruant/ods-xlsx.git#v0.10.0
|
2024-06-16 14:31:57 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Usage
|
|
|
|
|
|
2024-10-23 19:13:29 +02:00
|
|
|
#### Basic - reading an ods/xlsx file
|
2024-06-16 14:31:57 +02:00
|
|
|
|
|
|
|
|
```js
|
2024-06-18 11:06:41 +02:00
|
|
|
import {tableRawContentToObjects, tableWithoutEmptyRows, getODSTableRawContent} from 'ods-xlsx'
|
2024-06-16 14:31:57 +02:00
|
|
|
|
|
|
|
|
/**
|
2024-06-18 11:06:41 +02:00
|
|
|
* @param {File} file - an .ods file like the ones you get from an <input type=file>
|
2024-06-16 14:31:57 +02:00
|
|
|
* @return {Promise<any[]>}
|
|
|
|
|
*/
|
|
|
|
|
async function getFileData(file){
|
2024-06-18 11:06:41 +02:00
|
|
|
return tableRawContent
|
|
|
|
|
.then(tableWithoutEmptyRows)
|
|
|
|
|
.then(tableRawContentToObjects)
|
2024-06-16 14:31:57 +02:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
The return value is an array of objects where
|
|
|
|
|
the **keys** are the column names in the first row and
|
|
|
|
|
the **values** are automatically converted from the .ods or .xlsx files (which type numbers, strings, booleans and dates)
|
|
|
|
|
to the appropriate JavaScript value
|
|
|
|
|
|
|
|
|
|
|
2024-10-23 19:13:29 +02:00
|
|
|
#### Basic - creating an ods file
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
import {createOdsFile} from 'ods-xlsx'
|
|
|
|
|
|
|
|
|
|
const content = new Map([
|
|
|
|
|
[
|
|
|
|
|
'La feuille',
|
|
|
|
|
[
|
|
|
|
|
[
|
|
|
|
|
{value: '37', type: 'float'},
|
|
|
|
|
{value: '26', type: 'string'}
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
"L'autre feuille",
|
|
|
|
|
[
|
|
|
|
|
[
|
|
|
|
|
{value: '1', type: 'string'},
|
|
|
|
|
{value: '2', type: 'string'},
|
|
|
|
|
{value: '3', type: 'string'},
|
|
|
|
|
{value: '5', type: 'string'},
|
|
|
|
|
{value: '8', type: 'string'}
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
]
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
const ods = await createOdsFile(content)
|
|
|
|
|
// ods is an ArrayBuffer representing an ods file with the content described by the Map
|
|
|
|
|
```
|
|
|
|
|
|
2024-10-23 21:06:01 +02:00
|
|
|
(and there is a tool to test file creation:
|
|
|
|
|
`node tools/create-an-ods-file.js > yo.ods`)
|
2024-10-23 19:13:29 +02:00
|
|
|
|
2024-06-16 14:31:57 +02:00
|
|
|
#### Low-level
|
|
|
|
|
|
2024-06-18 11:06:41 +02:00
|
|
|
See exports
|
2024-06-16 14:31:57 +02:00
|
|
|
|
|
|
|
|
### Demo
|
|
|
|
|
|
|
|
|
|
https://davidbruant.github.io/ods-xlsx/
|
2024-06-14 16:57:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
## Local dev
|
|
|
|
|
|
|
|
|
|
```sh
|
|
|
|
|
npm install
|
|
|
|
|
npm run dev
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Expectations and licence
|
|
|
|
|
|
2024-06-16 14:31:57 +02:00
|
|
|
I hope to be credited for the work on this repo
|
2024-06-14 16:57:20 +02:00
|
|
|
|
|
|
|
|
Everything written by me and contributors to this repo is licenced under **CC0 1.0 (Public Domain)**
|
|
|
|
|
|
|
|
|
|
|
2024-06-16 14:31:57 +02:00
|
|
|
## Dependencies
|
2024-06-14 16:57:20 +02:00
|
|
|
|
2024-06-16 14:31:57 +02:00
|
|
|
Svelte and rollup are **MIT**-licence
|