@alosha/xlsx 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +191 -0
- package/dist/index.cjs +3704 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +825 -0
- package/dist/index.d.ts +825 -0
- package/dist/index.js +3659 -0
- package/dist/index.js.map +1 -0
- package/package.json +73 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 E.Y. Silva Navarrete
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
# @alosha/xlsx
|
|
2
|
+
|
|
3
|
+
> A modern TypeScript library for reading and writing XLSX spreadsheets — a fresh rewrite inspired by [exceljs](https://github.com/exceljs/exceljs).
|
|
4
|
+
|
|
5
|
+
[](https://github.com/avlisodraude/alosha-xlsx/actions/workflows/test.yml)
|
|
6
|
+
[](./LICENSE)
|
|
7
|
+
|
|
8
|
+
`@alosha/xlsx` reads and writes real `.xlsx` (OOXML) workbooks from a single, ESM-first
|
|
9
|
+
document model. The public surface is intentionally ExcelJS-shaped, so most code ports with
|
|
10
|
+
minimal changes — but the internals are a clean rewrite with a discriminated-union value model,
|
|
11
|
+
no per-cell class allocation, and a single runtime dependency ([`fflate`](https://github.com/101arrowz/fflate)).
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
pnpm add @alosha/xlsx
|
|
17
|
+
# or
|
|
18
|
+
npm install @alosha/xlsx
|
|
19
|
+
# or
|
|
20
|
+
yarn add @alosha/xlsx
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Requires Node.js **>= 20**. The package ships ESM + CJS builds and full type declarations.
|
|
24
|
+
|
|
25
|
+
## Write a workbook
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { Workbook } from "@alosha/xlsx";
|
|
29
|
+
|
|
30
|
+
const workbook = new Workbook();
|
|
31
|
+
workbook.creator = "Ada Lovelace";
|
|
32
|
+
|
|
33
|
+
const sheet = workbook.addWorksheet("Report");
|
|
34
|
+
|
|
35
|
+
// Address strings or (row, col) numbers both work.
|
|
36
|
+
sheet.getCell("A1").value = "Item";
|
|
37
|
+
sheet.getCell("B1").value = "Qty";
|
|
38
|
+
sheet.getCell("A2").value = "Widgets";
|
|
39
|
+
sheet.getCell("B2").value = 42;
|
|
40
|
+
|
|
41
|
+
// Number formats and formulas.
|
|
42
|
+
sheet.getCell("B2").numFmt = "#,##0";
|
|
43
|
+
sheet.getCell("B3").value = { formula: "SUM(B2:B2)", result: 42 };
|
|
44
|
+
|
|
45
|
+
// Column widths and merged cells.
|
|
46
|
+
sheet.getColumn(1).width = 24;
|
|
47
|
+
sheet.getCell("A4").value = "Total";
|
|
48
|
+
sheet.mergeCells("A4:B4");
|
|
49
|
+
|
|
50
|
+
// Serialize to bytes (browser-safe) …
|
|
51
|
+
const bytes: Uint8Array = await workbook.xlsx.writeBuffer();
|
|
52
|
+
|
|
53
|
+
// … or straight to disk (Node only).
|
|
54
|
+
await workbook.xlsx.writeFile("report.xlsx");
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
`writeBuffer()` returns a `Uint8Array` and never touches `node:fs`, so it runs in the browser,
|
|
58
|
+
a worker, or an edge runtime. `writeFile()` is the Node convenience wrapper.
|
|
59
|
+
|
|
60
|
+
## Read a workbook
|
|
61
|
+
|
|
62
|
+
`load` / `readFile` mutate the receiving workbook in place and return it (matching ExcelJS):
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import { Workbook } from "@alosha/xlsx";
|
|
66
|
+
|
|
67
|
+
// From bytes you already have (fetch, upload, buffer) …
|
|
68
|
+
const workbook = new Workbook();
|
|
69
|
+
await workbook.xlsx.load(bytes); // bytes: Uint8Array
|
|
70
|
+
|
|
71
|
+
// … or from a path (Node only).
|
|
72
|
+
await workbook.xlsx.readFile("report.xlsx");
|
|
73
|
+
|
|
74
|
+
const sheet = workbook.getWorksheet("Report");
|
|
75
|
+
console.log(sheet?.getCell("B2").value); // 42
|
|
76
|
+
console.log(sheet?.getCell("A1").text); // "Item"
|
|
77
|
+
|
|
78
|
+
sheet?.eachRow((row, rowNumber) => {
|
|
79
|
+
console.log(rowNumber, row.values);
|
|
80
|
+
});
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## The free-function core
|
|
84
|
+
|
|
85
|
+
The `workbook.xlsx.*` accessor is a thin ExcelJS-style wrapper over an environment-agnostic,
|
|
86
|
+
**synchronous** core. Reach for these when you want no magic accessor, tree-shakeable imports,
|
|
87
|
+
or to keep the model layer free of the serializer:
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
import { Workbook, writeWorkbookBuffer, readWorkbookBuffer } from "@alosha/xlsx";
|
|
91
|
+
|
|
92
|
+
const wb = new Workbook();
|
|
93
|
+
wb.addWorksheet("Sheet1").getCell("A1").value = "hi";
|
|
94
|
+
|
|
95
|
+
const bytes: Uint8Array = writeWorkbookBuffer(wb); // sync
|
|
96
|
+
const roundTripped: Workbook = readWorkbookBuffer(bytes); // sync
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Node-only file variants (`writeWorkbookFile`, `readWorkbookFile`) are also exported; they lazily
|
|
100
|
+
import `node:fs/promises` so browser bundles that only use the buffer functions never pull it in.
|
|
101
|
+
|
|
102
|
+
`writeWorkbookBuffer` accepts a `WriteOptions` (`useSharedStrings`, `useStyles`); the read
|
|
103
|
+
functions accept a `ReadOptions` (reserved for future use).
|
|
104
|
+
|
|
105
|
+
## Feature matrix
|
|
106
|
+
|
|
107
|
+
| Area | Read | Write | Notes |
|
|
108
|
+
| --- | :---: | :---: | --- |
|
|
109
|
+
| Cell values — string, number, boolean, date | ✅ | ✅ | 1900 and 1904 date systems |
|
|
110
|
+
| Formulas (with cached result) | ✅ | ✅ | shared/array formulas round-trip |
|
|
111
|
+
| Rich text runs | ✅ | ✅ | per-run fonts |
|
|
112
|
+
| Error values (`#REF!`, `#DIV/0!`, …) | ✅ | ✅ | |
|
|
113
|
+
| Shared strings | ✅ | ✅ | `useSharedStrings` toggle on write |
|
|
114
|
+
| Styles — fonts, fills, borders, alignment, number formats, protection | ✅ | ✅ | `useStyles` toggle on write |
|
|
115
|
+
| Merged cells | ✅ | ✅ | |
|
|
116
|
+
| Column width / styles / keys | ✅ | ✅ | |
|
|
117
|
+
| Row height / styles / outline levels | ✅ | ✅ | |
|
|
118
|
+
| Multiple worksheets, sheet state (visible / hidden / veryHidden) | ✅ | ✅ | |
|
|
119
|
+
| Workbook & sheet views (freeze/split, zoom, active tab) | ✅ | ✅ | round-trip as data |
|
|
120
|
+
| Workbook metadata (creator, dates, title, …) | ✅ | ✅ | |
|
|
121
|
+
| Hyperlink cells | ⚠️ | ⚠️ | round-trip as display **text** — see limitations |
|
|
122
|
+
| Streaming read/write | ❌ | ❌ | deferred |
|
|
123
|
+
| Drawings / images / charts | ❌ | ❌ | deferred |
|
|
124
|
+
| Data validation, conditional formatting, comments | ❌ | ❌ | deferred |
|
|
125
|
+
|
|
126
|
+
## Migrating from ExcelJS
|
|
127
|
+
|
|
128
|
+
The API is deliberately familiar — `new Workbook()`, `addWorksheet`, `getCell`, `getRow`,
|
|
129
|
+
`mergeCells`, and the `workbook.xlsx.writeBuffer / writeFile / load / readFile` accessor all
|
|
130
|
+
behave as you'd expect. Common differences to watch for:
|
|
131
|
+
|
|
132
|
+
- **Bytes, not Node `Buffer`.** `writeBuffer()` resolves to a `Uint8Array`. In Node a
|
|
133
|
+
`Uint8Array` is accepted anywhere a `Buffer` is (`fs.writeFile`, `res.end`, …); pass it
|
|
134
|
+
through, or wrap with `Buffer.from(bytes)` if an API insists on a `Buffer`.
|
|
135
|
+
- **`worksheets` is a dense, 0-based array.** No leading `undefined` / 1-based sparse hole —
|
|
136
|
+
`workbook.worksheets[0]` is the first sheet. Use `getWorksheet(idOrName)` for lookups by id or
|
|
137
|
+
name.
|
|
138
|
+
- **Clearer row inheritance.** Instead of ExcelJS's `'i' | 'o' | 'i+' | 'o+'` DSL, `addRow` /
|
|
139
|
+
`insertRow` take `{ inheritFrom: "above" | "below", includeEmpty?: boolean }`.
|
|
140
|
+
- **ESM-first.** The package is ESM with a CJS fallback; prefer `import` over `require`.
|
|
141
|
+
- **Hyperlinks round-trip as text** (see below).
|
|
142
|
+
|
|
143
|
+
Not everything ExcelJS does is implemented yet — check the feature matrix and limitations before
|
|
144
|
+
porting a workbook that leans on drawings, streaming, or data validation.
|
|
145
|
+
|
|
146
|
+
## Known limitations
|
|
147
|
+
|
|
148
|
+
- **Hyperlink cells round-trip as their display text.** Setting a `{ text, hyperlink }` value
|
|
149
|
+
writes the visible text; the clickable link relationship is not yet persisted, and reading a
|
|
150
|
+
workbook with hyperlinks yields the text only. Real hyperlink relationships are planned.
|
|
151
|
+
- **No streaming.** Reading and writing are buffer-based (the whole workbook lives in memory). A
|
|
152
|
+
streaming API for very large sheets is deferred.
|
|
153
|
+
- **No drawings, images, or charts.** These parts are ignored on read and not emitted on write.
|
|
154
|
+
- **No data validation, conditional formatting, or comments** yet.
|
|
155
|
+
|
|
156
|
+
See [`BENCHMARKS.md`](./BENCHMARKS.md) for write/read throughput and output-size numbers against
|
|
157
|
+
ExcelJS, and [`CHANGELOG.md`](./CHANGELOG.md) for release notes.
|
|
158
|
+
|
|
159
|
+
## Development
|
|
160
|
+
|
|
161
|
+
This project uses [pnpm](https://pnpm.io/), [tsup](https://tsup.egoist.dev/),
|
|
162
|
+
[Biome](https://biomejs.dev/), and [Vitest](https://vitest.dev/).
|
|
163
|
+
|
|
164
|
+
```sh
|
|
165
|
+
pnpm install # install dependencies
|
|
166
|
+
pnpm build # build ESM + CJS + type declarations
|
|
167
|
+
pnpm dev # rebuild on change
|
|
168
|
+
pnpm test # run the test suite once
|
|
169
|
+
pnpm test:watch # run tests in watch mode
|
|
170
|
+
pnpm lint # lint + format check with Biome
|
|
171
|
+
pnpm format # auto-format with Biome
|
|
172
|
+
pnpm typecheck # type-check with tsc
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### A note on `pnpm install` warnings
|
|
176
|
+
|
|
177
|
+
Installing dev dependencies prints a handful of `npm warn deprecated` lines
|
|
178
|
+
(`rimraf`, `inflight`, `lodash.isequal`, `glob`, `uuid`, `fstream`). These are
|
|
179
|
+
**transitive dependencies of `exceljs`**, which we pull in only as a
|
|
180
|
+
`devDependency` to verify our output: the round-trip test in
|
|
181
|
+
`tests/xlsx/write-workbook.test.ts` re-opens a generated workbook with ExcelJS
|
|
182
|
+
to confirm it loads without a repair prompt.
|
|
183
|
+
|
|
184
|
+
None of these ship in `@alosha/xlsx` — the published package has a single
|
|
185
|
+
runtime dependency, `fflate`. The warnings are ExcelJS's baggage, not ours;
|
|
186
|
+
in fact they're a tidy illustration of why this library exists. If ExcelJS is
|
|
187
|
+
not installed, that one round-trip test simply skips.
|
|
188
|
+
|
|
189
|
+
## License
|
|
190
|
+
|
|
191
|
+
[MIT](./LICENSE) © E.Y. Silva Navarrete
|