-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created first standalone sample for pure node js env without any othe…
…rs technologies. ref #6
- Loading branch information
Showing
10 changed files
with
865 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Oops, something went wrong.