node-excel-module
Expose excel functions in a XLSX file as a JavaScript module.
master
dev
Introduction
- XLSX files are read and parsed by Exceljs.
- Formula evaluation is powered by hot-formula-parser.
- Expose specified cell values and functions via an object.
- The exported object is serializable; that is, the exported object can be serialized to strings through libraries like serialize-javascript.
- Merged cells and shared formulas are supported.
- The minimum raw data is included into the compiled context. It works like a charm even if a formula requires the result from another formula.
- Cross-sheet reference supported.
Installation
npm install excel-module
Usage
const excelModule =
API
excelModule.from()
- Parameters
- src
string
|Readable
- src
- Return
Workbook
Workbook is a descendant class inherited from Exceljs.Workbook.
.compile()
- Parameters
- spec
Record<string, CellSpec>
The keys of exported object will be the same as thespec
object, which are names of exported APIs.
- spec
- Return
APIFactory
the exported object
CellSpec
The type
should be one of the following constructors, Number
, Boolean
, String
and Function
.
APIFactory
When specifying cells, use excel syntax like A1
, $B$2
. Note that both are all treated as absolute coordinates.
Example
See integration tests for more details.
- sum.xlsx
row\col | A | B | C |
---|---|---|---|
1 | 1 | 2 | =SUM(A1:B1) |
2 | 3 | 4 | =SUM(A2:B2) |
3 | 5 | 6 | =SUM(A3:B3) |
4 | =SUM(A1:B1) |
- index.js
const SUM_XLSX = 'path/to/sum.xlsx' { const workbook = await excelModule const apiFactory = await workbook const api = // 1 + 2 = 3 // 3 + 4 = 7 // 1 + 2 + 3 + 4 + 5 + 6 = 21 // 5 + 6 + 7 + 8 + 9 + 10 = 45}
Each compiled function contains a context of raw data. The context of the example above is shown as follow.
License
GLWTPL base on https://github.com/me-shaon/GLWTPL.