Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created first standalone sample for pure node js env #32

Merged
merged 2 commits into from
Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ interface Person {
const author = importer.getAllItems<Person>(config.owner);

```
# Samples

Sample integration with `xlsx-import` are placed in [./samples](./samples) directory, Currently available:

* [NodeJS sample](./samples/nodejs/README.md) of **importing an invoice** - it is pure JS example which runs on nodejs.

# The configuration:

## `worksheet`
Expand Down
39 changes: 39 additions & 0 deletions samples/nodejs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Hello

This is an example how to use `xlsx-import` in a pure node javascript.

**LIB Version:** `2.3.0`

## Usage

```bash
# install
npm install

# execute
node index.js


# or execute and save into file
node index.js > result.json
```

## What happened?

1. Read spreadsheet file [Invoice.xlsx](invoice.xlsx)
2. Following config import invoice data
3. Map, return and display data _(should be same as [result.json](./result.json))_.

## What is worth to see here?

1. Study importer configs: [`invoiceConfig.js`](configs/invoiceConfig.js)
2. Usage package in [`importer.js`](importer.js)

## What later:

1. Study documentation: [docs](./../../README.md)
2. Start using `xlsx-import` in your project
3. Ask a lot, report bugs and request for help: https://github.com/Siemienik/xlsx-import/issues
4. [Sponsor `xlsx-import` project](https://github.com/sponsors/Siemienik)


45 changes: 45 additions & 0 deletions samples/nodejs/configs/invoiceConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const getInvoiceConfig = () => ({
seller: {
worksheet: 'Invoice',
type: 'object',
fields: [
{row: 2, col: 1, key: 'name'},
{row: 4, col: 2, key: 'taxIdNumber'},
{row: 3, col: 1, key: 'address'},
{row: 9, col: 4, key: 'accountNo'},
]
},

buyer: {
worksheet: 'Invoice',
type: 'object',
fields: [
{row: 2, col: 5, key: 'name'},
{row: 4, col: 6, key: 'taxIdNumber'},
{row: 3, col: 5, key: 'address'},
]
},

misc: {
worksheet: 'Invoice',
type: 'object',
fields: [
{row: 6, col: 5, key: 'date', mapper: (v)=>new Date(v)}, //todo mapper
{row: 7, col: 5, key: 'dueDate', mapper: (v)=>new Date(v)}, //todo mapper
]
},

items: {
worksheet: 'Invoice',
type: 'list',
rowOffset: 13,
columns: [
{ index: 2, key: 'item'},
{ index: 4, key: 'unitPrice', mapper: (v)=>Number(v)},
{ index: 5, key: 'quantity', mapper: (v)=>Number(v)},
{ index: 6, key: 'price', mapper: (v)=>Number(v)},
]
}
});

module.exports = {getInvoiceConfig};
28 changes: 28 additions & 0 deletions samples/nodejs/importer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const ImporterFactory = require('xlsx-import/lib/ImporterFactory').default;
const { getInvoiceConfig } = require('./configs/invoiceConfig');

const factory = new ImporterFactory();

const importInvoice = async (invoicePath) => {
const config = getInvoiceConfig();

const importer = await factory.From(invoicePath);

const {date, dueDate} = importer.GetAllItems(config.misc)[0];
const seller = importer.GetAllItems(config.seller)[0];
const buyer = importer.GetAllItems(config.buyer)[0];
const items = importer.GetAllItems(config.items);

const total = items.reduce((p, c) => p + c.price, 0);

return {
date,
dueDate,
seller,
buyer,
items,
total
}
}

module.exports = {importInvoice};
5 changes: 5 additions & 0 deletions samples/nodejs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const {importInvoice} = require("./importer");

importInvoice( __dirname+"/invoice.xlsx").then((invoice)=>{
console.log(invoice);
})
Binary file added samples/nodejs/invoice.xlsx
Binary file not shown.
Loading