Skip to content

Commit

Permalink
Created first standalone sample for pure node js env without any othe…
Browse files Browse the repository at this point in the history
…rs technologies.

ref #6
  • Loading branch information
Siemienik committed Oct 8, 2020
1 parent 0071a71 commit c9a1570
Show file tree
Hide file tree
Showing 10 changed files with 865 additions and 1 deletion.
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
37 changes: 37 additions & 0 deletions samples/nodejs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Hello

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

## 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

0 comments on commit c9a1570

Please sign in to comment.