javascript-excel 2.0.0-alpha.2 → 2.0.0-alpha.4
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/README.md +151 -21
- package/dist/address-DUn7-5OS.js +75 -0
- package/dist/address-DZBHchLv.cjs +122 -0
- package/dist/{auto-width-2oBzjeNG.d.cts → auto-width-DRHsPKNk.d.cts} +1 -1
- package/dist/cells-CPXU722t.cjs +33 -0
- package/dist/cells-CrMF8h_t.js +28 -0
- package/dist/colors-B8Nf_xTx.js +114 -0
- package/dist/colors-oBpjx97l.cjs +119 -0
- package/dist/csv.d.cts +4 -1
- package/dist/csv.d.ts +4 -1
- package/dist/dataset-KeMzHCGl.js +664 -0
- package/dist/dataset-UUZCzsi3.cjs +687 -0
- package/dist/dates-BXcSWf9_.js +64 -0
- package/dist/dates-CA1dCizp.cjs +75 -0
- package/dist/dom.cjs +5 -2
- package/dist/dom.d.cts +5 -2
- package/dist/dom.d.ts +4 -1
- package/dist/dom.js +4 -1
- package/dist/{numfmt-Dv6AjopS.cjs → dos-time-FZ7-1tot.cjs} +0 -199
- package/dist/{numfmt-BBgB7iQA.js → dos-time-dD0z1YWU.js} +2 -177
- package/dist/format.cjs +1264 -0
- package/dist/format.d.cts +59 -0
- package/dist/format.d.ts +59 -0
- package/dist/format.js +1262 -0
- package/dist/import.cjs +10 -0
- package/dist/import.d.cts +10 -0
- package/dist/import.d.ts +10 -0
- package/dist/import.js +1 -0
- package/dist/index.cjs +24 -607
- package/dist/index.d.cts +32 -6
- package/dist/index.d.ts +31 -5
- package/dist/index.js +10 -594
- package/dist/{warnings-C_12PT4t.d.cts → model-0JikLfuM.d.cts} +1 -41
- package/dist/{warnings-DXJKe1lx.d.ts → model-0JikLfuM.d.ts} +1 -41
- package/dist/numfmt-BmEbm78Y.cjs +125 -0
- package/dist/numfmt-DCDrX9P1.js +114 -0
- package/dist/pipeline-BV2q-khO.cjs +1143 -0
- package/dist/pipeline-CctEa86r.d.cts +186 -0
- package/dist/pipeline-I8SLmV9S.d.ts +186 -0
- package/dist/pipeline-Snu8wBXd.js +1096 -0
- package/dist/{read-Ym1voUct.cjs → read-CZEWV4rK.cjs} +181 -17
- package/dist/{read-DDN5ZsPe.js → read-LWti17lz.js} +169 -5
- package/dist/read.cjs +1 -1
- package/dist/read.d.cts +78 -4
- package/dist/read.d.ts +78 -4
- package/dist/read.js +1 -1
- package/dist/render.cjs +751 -0
- package/dist/render.d.cts +215 -0
- package/dist/render.d.ts +215 -0
- package/dist/render.js +732 -0
- package/dist/{sheet-names-uT2EH5Ui.js → sheet-names-7p-joVsG.js} +2 -101
- package/dist/{sheet-names-qpXsvMpM.cjs → sheet-names-B8U5RAW0.cjs} +14 -167
- package/dist/warnings-B1v58nCZ.d.cts +41 -0
- package/dist/warnings-B1v58nCZ.d.ts +41 -0
- package/dist/worker.cjs +10 -2
- package/dist/worker.d.cts +10 -4
- package/dist/worker.d.ts +10 -4
- package/dist/worker.js +10 -2
- package/dist/{write-CYpLGuzl.d.ts → write-DM6ieuQY.d.ts} +2 -1
- package/dist/{write-DubvcG-x.js → write-DjgWy1PK.js} +5 -2
- package/dist/{write-C5JgeIar.d.cts → write-DtjfdIFm.d.cts} +2 -1
- package/dist/{write-BOh9jfu1.cjs → write-f9kcYf5K.cjs} +31 -28
- package/package.json +32 -1
- package/dist/read-C6L7Nyy3.d.cts +0 -78
- package/dist/read-Ddkks7qC.d.ts +0 -78
package/README.md
CHANGED
|
@@ -1,13 +1,33 @@
|
|
|
1
1
|
# javascript-excel
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
>
|
|
5
|
-
> The last 1.x release is `javascript-excel@1.0.3`. Keep using it until 2.0 is released.
|
|
3
|
+
Read and write `.xlsx` and `.csv` files in browsers, Web Workers and Node.js. TypeScript, no runtime dependencies, tree-shakable entry points.
|
|
6
4
|
|
|
5
|
+
> **Prerelease.** `2.0.0-alpha.x` is a from-scratch rewrite. Everything listed below ships today, and the API may still change before 2.0.0. The last 1.x release is `javascript-excel@1.0.3`.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install javascript-excel@next
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
| entry | for |
|
|
12
|
+
|---|---|
|
|
13
|
+
| `javascript-excel` | the workbook model, the `.xlsx` writer, dataset builders, dates, addresses, errors |
|
|
14
|
+
| `javascript-excel/read` | the `.xlsx` reader |
|
|
15
|
+
| `javascript-excel/csv` | CSV parse and serialize |
|
|
16
|
+
| `javascript-excel/dom` | HTML `<table>` to sheet, `download()` |
|
|
17
|
+
| `javascript-excel/worker` | run big jobs in a Web Worker |
|
|
18
|
+
| `javascript-excel/format` | show values as Excel does: number formats, dates, colours |
|
|
19
|
+
| `javascript-excel/render` | the render model for a sheet viewer: geometry, styles, merges, navigation, copy |
|
|
20
|
+
| `javascript-excel/import` | a user's `.xlsx` or `.csv` to `{ columns, data }`: header detection, types, locales, column mapping |
|
|
21
|
+
|
|
22
|
+
Each entry pulls in only what it needs: a writer-only app never ships the reader, the CSV code or the DOM code.
|
|
23
|
+
|
|
24
|
+
## Write an `.xlsx`
|
|
25
|
+
|
|
26
|
+
<!-- example: examples/snippets/src/write.ts -->
|
|
7
27
|
```ts
|
|
8
|
-
import { toUint8Array } from "javascript-excel";
|
|
28
|
+
import { toBlob, toUint8Array, type Workbook } from "javascript-excel";
|
|
9
29
|
|
|
10
|
-
const
|
|
30
|
+
const workbook: Workbook = {
|
|
11
31
|
sheets: [
|
|
12
32
|
{
|
|
13
33
|
name: "Report",
|
|
@@ -16,23 +36,23 @@ const bytes = await toUint8Array({
|
|
|
16
36
|
["Pens", 10, { value: 1.5, style: { numFmt: "#,##0.00" } }],
|
|
17
37
|
["Total", { formula: "SUM(B2:B2)", value: 10 }, new Date()],
|
|
18
38
|
],
|
|
39
|
+
view: { freeze: { rows: 1 } },
|
|
40
|
+
colProps: [{ width: "auto" }],
|
|
19
41
|
},
|
|
20
42
|
],
|
|
21
|
-
}
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
`toBlob()` returns the same file as a `Blob` for browser downloads.
|
|
43
|
+
};
|
|
25
44
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
45
|
+
const bytes: Uint8Array = await toUint8Array(workbook); // Node: writeFileSync("report.xlsx", bytes)
|
|
46
|
+
const blob = await toBlob(workbook); // browser: download it, upload it, ...
|
|
47
|
+
```
|
|
29
48
|
|
|
30
|
-
|
|
49
|
+
A workbook is plain data: sheets with `rows[r][c]`, where a cell is a value (`string`, `number`, `boolean`, `Date`, `{ error }`, `{ richText }`) or a `{ value, formula, style, hyperlink }` object.
|
|
31
50
|
|
|
32
|
-
|
|
51
|
+
## From a dataset (`{ columns, data }`)
|
|
33
52
|
|
|
53
|
+
<!-- example: examples/snippets/src/dataset.ts -->
|
|
34
54
|
```ts
|
|
35
|
-
import { fromDataset,
|
|
55
|
+
import { fromDataset, toDataset, toUint8Array } from "javascript-excel";
|
|
36
56
|
|
|
37
57
|
const sheet = fromDataset(
|
|
38
58
|
{
|
|
@@ -44,25 +64,135 @@ const sheet = fromDataset(
|
|
|
44
64
|
},
|
|
45
65
|
{ freezeHeader: true, autoFilter: true, totals: { label: "Total" } },
|
|
46
66
|
);
|
|
67
|
+
const bytes = await toUint8Array({ sheets: [sheet] });
|
|
68
|
+
|
|
69
|
+
// ...and back from a sheet that was read: { columns, data } with one object per row.
|
|
70
|
+
const { columns, data } = toDataset(sheet);
|
|
47
71
|
```
|
|
48
72
|
|
|
49
|
-
|
|
73
|
+
## Read an `.xlsx`
|
|
74
|
+
|
|
75
|
+
<!-- example: examples/snippets/src/read.ts -->
|
|
76
|
+
```ts
|
|
77
|
+
import { read } from "javascript-excel/read";
|
|
78
|
+
|
|
79
|
+
declare const file: Blob; // a File from <input type="file">, a Uint8Array or an ArrayBuffer
|
|
80
|
+
|
|
81
|
+
const { workbook, warnings } = await read(file);
|
|
82
|
+
for (const sheet of workbook.sheets) {
|
|
83
|
+
console.log(sheet.name, sheet.dimension, sheet.rows[0]?.map((cell) => cell?.value));
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## CSV
|
|
88
|
+
|
|
89
|
+
<!-- example: examples/snippets/src/csv.ts -->
|
|
90
|
+
```ts
|
|
91
|
+
import { parseCsv, readCsv, toCsv, toCsvBlob } from "javascript-excel/csv";
|
|
92
|
+
|
|
93
|
+
const text = toCsv([["name", "amount"], ["Ada", 1250.5], ["=1+1", -5]]); // "=1+1" is written as "'=1+1"
|
|
94
|
+
const blob = toCsvBlob([["name"], ["Ada"]]); // UTF-8 with a BOM, so Excel opens it correctly
|
|
95
|
+
|
|
96
|
+
const { rows, delimiter } = parseCsv("name;amount\nAda;1250,50\n"); // detects ";"
|
|
97
|
+
declare const upload: Blob;
|
|
98
|
+
const typed = await readCsv(upload, { inferTypes: true }); // numbers, TRUE/FALSE and ISO dates typed
|
|
99
|
+
```
|
|
50
100
|
|
|
51
|
-
|
|
101
|
+
## HTML tables and downloads
|
|
52
102
|
|
|
103
|
+
<!-- example: examples/snippets/src/dom.ts -->
|
|
53
104
|
```ts
|
|
54
105
|
import { toBlob } from "javascript-excel";
|
|
55
106
|
import { download, fromTable } from "javascript-excel/dom";
|
|
56
107
|
|
|
57
|
-
const
|
|
108
|
+
const table = document.querySelector("table")!;
|
|
109
|
+
const sheet = fromTable(table, { styles: "computed", freezeHeader: true, autoWidth: true });
|
|
58
110
|
download(await toBlob({ sheets: [sheet] }), "report.xlsx");
|
|
59
111
|
```
|
|
60
112
|
|
|
61
|
-
|
|
113
|
+
## Number formats
|
|
114
|
+
|
|
115
|
+
<!-- example: examples/snippets/src/format.ts -->
|
|
116
|
+
```ts
|
|
117
|
+
import { formatCell, formatValue } from "javascript-excel/format";
|
|
118
|
+
import { read } from "javascript-excel/read";
|
|
119
|
+
|
|
120
|
+
formatValue(1234.5, "#,##0.00"); // { text: "1,234.50", align: "right" }
|
|
121
|
+
formatValue(-12.5, "0.0;[Red]-0.0").color; // "FFFF0000"
|
|
122
|
+
formatValue(45306, "dddd, mmmm d, yyyy", { locale: "de-DE" }).text; // "Montag, Januar 15, 2024"
|
|
123
|
+
|
|
124
|
+
declare const file: Blob;
|
|
125
|
+
const { workbook } = await read(file, { dates: "serial" }); // serials: formatting does not depend on the time zone
|
|
126
|
+
const shown = formatCell(workbook.sheets[0]?.rows[0]?.[0], { locale: navigator.language, date1904: workbook.date1904 });
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Importing a user's file
|
|
130
|
+
|
|
131
|
+
`importDataset` finds the sheet and header row, flattens header groups, infers column types (with an explicit number and date locale), maps columns to your fields, and reports problems per row. Formulas are never evaluated, and the read limits stay on.
|
|
132
|
+
|
|
133
|
+
<!-- example: examples/snippets/src/import.ts -->
|
|
134
|
+
```ts
|
|
135
|
+
import { importDataset } from "javascript-excel/import";
|
|
136
|
+
|
|
137
|
+
declare const file: File; // from <input type="file"> or a drop
|
|
138
|
+
const { columns, data, issues, meta } = await importDataset(file, {
|
|
139
|
+
locale: { decimal: ",", group: "." }, // 1.234,56
|
|
140
|
+
schema: { fields: { email: { aliases: ["E-mail"], required: true }, joined: { type: "date" } } },
|
|
141
|
+
maxRows: 100_000,
|
|
142
|
+
});
|
|
143
|
+
console.log(meta.header, columns.map((c) => `${c.key}: ${c.type}`), data.length, issues);
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## In a Web Worker
|
|
147
|
+
|
|
148
|
+
<!-- example: examples/snippets/src/worker.ts -->
|
|
149
|
+
```ts
|
|
150
|
+
// excel.worker.ts (the app writes these two lines):
|
|
151
|
+
// import { exposeWorker } from "javascript-excel/worker";
|
|
152
|
+
// exposeWorker();
|
|
153
|
+
import type { Workbook } from "javascript-excel";
|
|
154
|
+
import { createWorkerClient } from "javascript-excel/worker";
|
|
155
|
+
|
|
156
|
+
declare const workbook: Workbook;
|
|
157
|
+
const excel = createWorkerClient(new Worker(new URL("./excel.worker.ts", import.meta.url), { type: "module" }));
|
|
158
|
+
const controller = new AbortController();
|
|
159
|
+
const bytes = await excel.write(workbook, { signal: controller.signal, onProgress: (p) => console.log(p.phase, p.ratio) });
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## What ships
|
|
163
|
+
|
|
164
|
+
- **Writer:** strings, numbers, booleans, dates (1900 and 1904 systems), errors, rich text, formulas with cached values and array formulas, and styled empty cells.
|
|
165
|
+
- **Styles:** the full style model (fonts, pattern and gradient fills, all border styles with a colour per side, alignment, protection, built-in and custom number formats), deduplicated.
|
|
166
|
+
- **Sheet features:** column widths (characters, pixels or `"auto"`), row heights, hidden rows and columns, outline levels, merges, autofilter, freeze panes, right-to-left sheets, zoom, tab colours, hyperlinks (external URLs limited to `http`, `https` and `mailto` by default), defined names and print setup.
|
|
167
|
+
- **Workbook:** document properties.
|
|
168
|
+
- **Validation:** input that can't be written faithfully throws a typed `ValidationError`.
|
|
169
|
+
- **Reader:** `.xlsx`, `.xlsm`, `.xltx` and `.xltm`, including ISO 29500 Strict files.
|
|
170
|
+
- Reads values, shared, inline and rich strings, and formula text with cached values (shared formulas expanded).
|
|
171
|
+
- Styles come back as shared frozen objects. Merges, column widths and row heights, views and panes, print setup, hyperlinks, defined names and document properties are read too.
|
|
172
|
+
- **Untrusted files:** bounded by limits on size, zip entries, compression ratio, XML depth, cells and shared strings. A `DOCTYPE` is rejected, nothing is ever fetched or executed, and file content never becomes an object key.
|
|
173
|
+
- **Rejected formats:** `.xls`, `.xlsb`, `.ods` and encrypted files throw `UNSUPPORTED_FORMAT`.
|
|
174
|
+
- **CSV:** RFC 4180 parsing with delimiter detection, BOM and CRLF/LF/CR handling, lenient or strict modes, and optional type inference. `readCsv` decodes bytes in any `TextDecoder` encoding. Serializing gives you quoting, a BOM for Excel, ISO dates and a formula-injection guard, which is on by default.
|
|
175
|
+
- **Datasets:** `fromDataset` and `fromDatasets` support `key`, `path` and function accessors, grouped headers, number formats, widths and a totals row. `toDataset` reads a sheet back into `{ columns, data }`.
|
|
176
|
+
- **DOM:** `fromTable` handles `colspan`/`rowspan` merges, `data-*` type hints and computed styles. `download()` needs no dependency, and the module is safe to import during SSR.
|
|
177
|
+
- **Worker:** progress, warnings and `AbortSignal` work across the boundary, and results are transferred, not copied.
|
|
178
|
+
- **Import:** `importDataset` turns a user's `.xlsx` or `.csv` into `{ columns, data }`. It detects title and header rows, including multi-row headers. It infers column types all-or-nothing, parses numbers and dates in an explicit locale (`1.234,56`, `15/01/2024`) and maps columns to your fields by name or alias. Problems are reported per row with their cell. Preview reads (`preview`) and `maxRows` stop reading early, and it also runs in the worker.
|
|
179
|
+
- **Not in 2.0:** `.xls`/`.xlsb`/`.ods`, charts, images, comments, pivot tables, conditional formatting and formula evaluation. These are planned for later releases, not ruled out.
|
|
180
|
+
|
|
181
|
+
## Runtime support
|
|
182
|
+
|
|
183
|
+
- Node.js 22 and later.
|
|
184
|
+
- Browsers with `CompressionStream('deflate-raw')`: Chrome and Edge 103+, Firefox 113+, Safari 16.4+.
|
|
185
|
+
- TypeScript 5.0–7, with `moduleResolution` `node16`, `nodenext` or `bundler`. Types ship with the package.
|
|
186
|
+
|
|
187
|
+
Performance is measured in CI at 10k, 100k and 1M cells (for example, writing 1M numbers takes 1.3 s and reading them back 1.25 s on a GitHub Actions runner). The full table is in the [repository README](https://github.com/securedeveloper/javascript-excel#performance).
|
|
188
|
+
|
|
189
|
+
## Documentation
|
|
62
190
|
|
|
63
|
-
|
|
191
|
+
- Guides with runnable examples: [Node](https://github.com/securedeveloper/javascript-excel/blob/main/docs/guides/node.md), [vanilla browser](https://github.com/securedeveloper/javascript-excel/blob/main/docs/guides/vanilla.md), [React](https://github.com/securedeveloper/javascript-excel/blob/main/docs/guides/react.md), [Vue 3](https://github.com/securedeveloper/javascript-excel/blob/main/docs/guides/vue.md).
|
|
192
|
+
- For React components on top of this library, see [react-data-export](https://github.com/securedeveloper/react-data-export).
|
|
193
|
+
- The design and full API: [docs/api.md](https://github.com/securedeveloper/javascript-excel/blob/main/docs/api.md).
|
|
64
194
|
|
|
65
|
-
Source
|
|
195
|
+
Source and issues: https://github.com/securedeveloper/javascript-excel
|
|
66
196
|
|
|
67
197
|
## License
|
|
68
198
|
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { o as ValidationError } from "./errors-CvGlfw0E.js";
|
|
2
|
+
// src/address.ts
|
|
3
|
+
/**
|
|
4
|
+
* A1 address helpers (docs/api.md §4.1). Indices are zero-based.
|
|
5
|
+
*/
|
|
6
|
+
/** Rows per sheet in the xlsx format. */
|
|
7
|
+
const MAX_ROWS = 1048576;
|
|
8
|
+
/** Columns per sheet in the xlsx format (A..XFD). */
|
|
9
|
+
const MAX_COLS = 16384;
|
|
10
|
+
function invalidRef(ref, why) {
|
|
11
|
+
return new ValidationError("INVALID_REF", `Invalid reference ${JSON.stringify(ref)}: ${why}.`, { details: { ref } });
|
|
12
|
+
}
|
|
13
|
+
function checkIndex(n, max, what) {
|
|
14
|
+
if (!Number.isInteger(n) || n < 0 || n >= max) throw new ValidationError("OUT_OF_BOUNDS", `${what} index ${n} is outside 0..${max - 1}.`, { details: {
|
|
15
|
+
index: n,
|
|
16
|
+
max: max - 1
|
|
17
|
+
} });
|
|
18
|
+
}
|
|
19
|
+
/** Column index to letters: 0 → 'A', 16383 → 'XFD'. */
|
|
20
|
+
function encodeCol(col) {
|
|
21
|
+
checkIndex(col, MAX_COLS, "Column");
|
|
22
|
+
let s = "";
|
|
23
|
+
for (let n = col + 1; n > 0; n = Math.floor((n - 1) / 26)) s = String.fromCharCode(65 + (n - 1) % 26) + s;
|
|
24
|
+
return s;
|
|
25
|
+
}
|
|
26
|
+
/** Column letters to index: 'A' → 0, 'XFD' → 16383. Case-insensitive. */
|
|
27
|
+
function decodeCol(letters) {
|
|
28
|
+
if (typeof letters !== "string" || !/^[A-Za-z]{1,3}$/.test(letters)) throw invalidRef(letters, "expected column letters");
|
|
29
|
+
let n = 0;
|
|
30
|
+
for (const ch of letters.toUpperCase()) n = n * 26 + (ch.charCodeAt(0) - 64);
|
|
31
|
+
if (n > 16384) throw invalidRef(letters, "beyond column XFD");
|
|
32
|
+
return n - 1;
|
|
33
|
+
}
|
|
34
|
+
/** { row: 0, col: 1 } → 'B1'. */
|
|
35
|
+
function encodeCell(a) {
|
|
36
|
+
checkIndex(a.row, MAX_ROWS, "Row");
|
|
37
|
+
return encodeCol(a.col) + String(a.row + 1);
|
|
38
|
+
}
|
|
39
|
+
const CELL_RE = /^\$?([A-Za-z]{1,3})\$?([1-9][0-9]{0,6})$/;
|
|
40
|
+
/** 'B1' or '$B$1' → { row: 0, col: 1 }. */
|
|
41
|
+
function decodeCell(ref) {
|
|
42
|
+
const m = typeof ref === "string" ? CELL_RE.exec(ref) : null;
|
|
43
|
+
if (!m) throw invalidRef(ref, "expected an A1 cell reference");
|
|
44
|
+
const row = Number(m[2]) - 1;
|
|
45
|
+
if (row >= 1048576) throw invalidRef(ref, "beyond row 1048576");
|
|
46
|
+
return {
|
|
47
|
+
row,
|
|
48
|
+
col: decodeCol(m[1])
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/** Range to 'A1:C3' (a single cell gives 'A1'). */
|
|
52
|
+
function encodeRange(r) {
|
|
53
|
+
const a = encodeCell(r.start);
|
|
54
|
+
const b = encodeCell(r.end);
|
|
55
|
+
return a === b ? a : `${a}:${b}`;
|
|
56
|
+
}
|
|
57
|
+
/** 'A1:C3' or 'A1' → range, normalised so start is top-left. */
|
|
58
|
+
function decodeRange(ref) {
|
|
59
|
+
if (typeof ref !== "string") throw invalidRef(ref, "expected an A1 range");
|
|
60
|
+
const parts = ref.split(":");
|
|
61
|
+
if (parts.length > 2) throw invalidRef(ref, "expected an A1 range");
|
|
62
|
+
const a = decodeCell(parts[0]);
|
|
63
|
+
const b = parts.length === 2 ? decodeCell(parts[1]) : a;
|
|
64
|
+
return {
|
|
65
|
+
start: {
|
|
66
|
+
row: Math.min(a.row, b.row),
|
|
67
|
+
col: Math.min(a.col, b.col)
|
|
68
|
+
},
|
|
69
|
+
end: {
|
|
70
|
+
row: Math.max(a.row, b.row),
|
|
71
|
+
col: Math.max(a.col, b.col)
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
export { decodeRange as a, encodeRange as c, decodeCol as i, MAX_ROWS as n, encodeCell as o, decodeCell as r, encodeCol as s, MAX_COLS as t };
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
const require_errors = require("./errors-DOPrrGS4.cjs");
|
|
2
|
+
// src/address.ts
|
|
3
|
+
/**
|
|
4
|
+
* A1 address helpers (docs/api.md §4.1). Indices are zero-based.
|
|
5
|
+
*/
|
|
6
|
+
/** Rows per sheet in the xlsx format. */
|
|
7
|
+
const MAX_ROWS = 1048576;
|
|
8
|
+
/** Columns per sheet in the xlsx format (A..XFD). */
|
|
9
|
+
const MAX_COLS = 16384;
|
|
10
|
+
function invalidRef(ref, why) {
|
|
11
|
+
return new require_errors.ValidationError("INVALID_REF", `Invalid reference ${JSON.stringify(ref)}: ${why}.`, { details: { ref } });
|
|
12
|
+
}
|
|
13
|
+
function checkIndex(n, max, what) {
|
|
14
|
+
if (!Number.isInteger(n) || n < 0 || n >= max) throw new require_errors.ValidationError("OUT_OF_BOUNDS", `${what} index ${n} is outside 0..${max - 1}.`, { details: {
|
|
15
|
+
index: n,
|
|
16
|
+
max: max - 1
|
|
17
|
+
} });
|
|
18
|
+
}
|
|
19
|
+
/** Column index to letters: 0 → 'A', 16383 → 'XFD'. */
|
|
20
|
+
function encodeCol(col) {
|
|
21
|
+
checkIndex(col, MAX_COLS, "Column");
|
|
22
|
+
let s = "";
|
|
23
|
+
for (let n = col + 1; n > 0; n = Math.floor((n - 1) / 26)) s = String.fromCharCode(65 + (n - 1) % 26) + s;
|
|
24
|
+
return s;
|
|
25
|
+
}
|
|
26
|
+
/** Column letters to index: 'A' → 0, 'XFD' → 16383. Case-insensitive. */
|
|
27
|
+
function decodeCol(letters) {
|
|
28
|
+
if (typeof letters !== "string" || !/^[A-Za-z]{1,3}$/.test(letters)) throw invalidRef(letters, "expected column letters");
|
|
29
|
+
let n = 0;
|
|
30
|
+
for (const ch of letters.toUpperCase()) n = n * 26 + (ch.charCodeAt(0) - 64);
|
|
31
|
+
if (n > 16384) throw invalidRef(letters, "beyond column XFD");
|
|
32
|
+
return n - 1;
|
|
33
|
+
}
|
|
34
|
+
/** { row: 0, col: 1 } → 'B1'. */
|
|
35
|
+
function encodeCell(a) {
|
|
36
|
+
checkIndex(a.row, MAX_ROWS, "Row");
|
|
37
|
+
return encodeCol(a.col) + String(a.row + 1);
|
|
38
|
+
}
|
|
39
|
+
const CELL_RE = /^\$?([A-Za-z]{1,3})\$?([1-9][0-9]{0,6})$/;
|
|
40
|
+
/** 'B1' or '$B$1' → { row: 0, col: 1 }. */
|
|
41
|
+
function decodeCell(ref) {
|
|
42
|
+
const m = typeof ref === "string" ? CELL_RE.exec(ref) : null;
|
|
43
|
+
if (!m) throw invalidRef(ref, "expected an A1 cell reference");
|
|
44
|
+
const row = Number(m[2]) - 1;
|
|
45
|
+
if (row >= 1048576) throw invalidRef(ref, "beyond row 1048576");
|
|
46
|
+
return {
|
|
47
|
+
row,
|
|
48
|
+
col: decodeCol(m[1])
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/** Range to 'A1:C3' (a single cell gives 'A1'). */
|
|
52
|
+
function encodeRange(r) {
|
|
53
|
+
const a = encodeCell(r.start);
|
|
54
|
+
const b = encodeCell(r.end);
|
|
55
|
+
return a === b ? a : `${a}:${b}`;
|
|
56
|
+
}
|
|
57
|
+
/** 'A1:C3' or 'A1' → range, normalised so start is top-left. */
|
|
58
|
+
function decodeRange(ref) {
|
|
59
|
+
if (typeof ref !== "string") throw invalidRef(ref, "expected an A1 range");
|
|
60
|
+
const parts = ref.split(":");
|
|
61
|
+
if (parts.length > 2) throw invalidRef(ref, "expected an A1 range");
|
|
62
|
+
const a = decodeCell(parts[0]);
|
|
63
|
+
const b = parts.length === 2 ? decodeCell(parts[1]) : a;
|
|
64
|
+
return {
|
|
65
|
+
start: {
|
|
66
|
+
row: Math.min(a.row, b.row),
|
|
67
|
+
col: Math.min(a.col, b.col)
|
|
68
|
+
},
|
|
69
|
+
end: {
|
|
70
|
+
row: Math.max(a.row, b.row),
|
|
71
|
+
col: Math.max(a.col, b.col)
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
Object.defineProperty(exports, "MAX_COLS", {
|
|
76
|
+
enumerable: true,
|
|
77
|
+
get: function() {
|
|
78
|
+
return MAX_COLS;
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
Object.defineProperty(exports, "MAX_ROWS", {
|
|
82
|
+
enumerable: true,
|
|
83
|
+
get: function() {
|
|
84
|
+
return MAX_ROWS;
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
Object.defineProperty(exports, "decodeCell", {
|
|
88
|
+
enumerable: true,
|
|
89
|
+
get: function() {
|
|
90
|
+
return decodeCell;
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
Object.defineProperty(exports, "decodeCol", {
|
|
94
|
+
enumerable: true,
|
|
95
|
+
get: function() {
|
|
96
|
+
return decodeCol;
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
Object.defineProperty(exports, "decodeRange", {
|
|
100
|
+
enumerable: true,
|
|
101
|
+
get: function() {
|
|
102
|
+
return decodeRange;
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
Object.defineProperty(exports, "encodeCell", {
|
|
106
|
+
enumerable: true,
|
|
107
|
+
get: function() {
|
|
108
|
+
return encodeCell;
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
Object.defineProperty(exports, "encodeCol", {
|
|
112
|
+
enumerable: true,
|
|
113
|
+
get: function() {
|
|
114
|
+
return encodeCol;
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
Object.defineProperty(exports, "encodeRange", {
|
|
118
|
+
enumerable: true,
|
|
119
|
+
get: function() {
|
|
120
|
+
return encodeRange;
|
|
121
|
+
}
|
|
122
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
const require_iso_date = require("./iso-date-3TY1OlGd.cjs");
|
|
2
|
+
// src/cells.ts
|
|
3
|
+
/**
|
|
4
|
+
* Cell helpers (docs/api.md §4.2).
|
|
5
|
+
*/
|
|
6
|
+
function valueText(v, zone) {
|
|
7
|
+
if (v === null || v === void 0) return "";
|
|
8
|
+
if (typeof v === "string") return v;
|
|
9
|
+
if (typeof v === "number") return String(v);
|
|
10
|
+
if (typeof v === "boolean") return v ? "TRUE" : "FALSE";
|
|
11
|
+
if (v instanceof Date) return Number.isFinite(v.getTime()) ? require_iso_date.isoDate(v, zone) : "";
|
|
12
|
+
if (typeof v === "object" && "error" in v) return String(v.error);
|
|
13
|
+
if (typeof v === "object" && "richText" in v && Array.isArray(v.richText)) return v.richText.map((r) => r.text).join("");
|
|
14
|
+
return "";
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Plain text of a cell: strings as they are, numbers as `String(n)`, booleans
|
|
18
|
+
* as TRUE/FALSE, dates as ISO 8601, errors as their code, rich text joined.
|
|
19
|
+
* A formula cell gives its cached value's text ('' when there is none).
|
|
20
|
+
* Number formats are not applied.
|
|
21
|
+
*/
|
|
22
|
+
function cellText(cell, options) {
|
|
23
|
+
const zone = options?.dateZone ?? "local";
|
|
24
|
+
if (cell === null || cell === void 0) return "";
|
|
25
|
+
if (typeof cell !== "object" || cell instanceof Date || "error" in cell || "richText" in cell) return valueText(cell, zone);
|
|
26
|
+
return valueText(cell.value, zone);
|
|
27
|
+
}
|
|
28
|
+
Object.defineProperty(exports, "cellText", {
|
|
29
|
+
enumerable: true,
|
|
30
|
+
get: function() {
|
|
31
|
+
return cellText;
|
|
32
|
+
}
|
|
33
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { t as isoDate } from "./iso-date-CXnn-9mT.js";
|
|
2
|
+
// src/cells.ts
|
|
3
|
+
/**
|
|
4
|
+
* Cell helpers (docs/api.md §4.2).
|
|
5
|
+
*/
|
|
6
|
+
function valueText(v, zone) {
|
|
7
|
+
if (v === null || v === void 0) return "";
|
|
8
|
+
if (typeof v === "string") return v;
|
|
9
|
+
if (typeof v === "number") return String(v);
|
|
10
|
+
if (typeof v === "boolean") return v ? "TRUE" : "FALSE";
|
|
11
|
+
if (v instanceof Date) return Number.isFinite(v.getTime()) ? isoDate(v, zone) : "";
|
|
12
|
+
if (typeof v === "object" && "error" in v) return String(v.error);
|
|
13
|
+
if (typeof v === "object" && "richText" in v && Array.isArray(v.richText)) return v.richText.map((r) => r.text).join("");
|
|
14
|
+
return "";
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Plain text of a cell: strings as they are, numbers as `String(n)`, booleans
|
|
18
|
+
* as TRUE/FALSE, dates as ISO 8601, errors as their code, rich text joined.
|
|
19
|
+
* A formula cell gives its cached value's text ('' when there is none).
|
|
20
|
+
* Number formats are not applied.
|
|
21
|
+
*/
|
|
22
|
+
function cellText(cell, options) {
|
|
23
|
+
const zone = options?.dateZone ?? "local";
|
|
24
|
+
if (cell === null || cell === void 0) return "";
|
|
25
|
+
if (typeof cell !== "object" || cell instanceof Date || "error" in cell || "richText" in cell) return valueText(cell, zone);
|
|
26
|
+
return valueText(cell.value, zone);
|
|
27
|
+
}
|
|
28
|
+
export { cellText as t };
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// src/colors.ts
|
|
2
|
+
/** The Office theme's colours in clrScheme order (dk1, lt1, dk2, lt2, accent1..6, hlink, folHlink), as the writer embeds it. */
|
|
3
|
+
const DEFAULT_THEME = [
|
|
4
|
+
"000000",
|
|
5
|
+
"FFFFFF",
|
|
6
|
+
"44546A",
|
|
7
|
+
"E7E6E6",
|
|
8
|
+
"4472C4",
|
|
9
|
+
"ED7D31",
|
|
10
|
+
"A5A5A5",
|
|
11
|
+
"FFC000",
|
|
12
|
+
"5B9BD5",
|
|
13
|
+
"70AD47",
|
|
14
|
+
"0563C1",
|
|
15
|
+
"954F72"
|
|
16
|
+
];
|
|
17
|
+
/** The default indexed palette, entries 0–63 (ECMA-376 §18.8.27); 64 is the system foreground, 65 the system background. */
|
|
18
|
+
const PALETTE = "000000 FFFFFF FF0000 00FF00 0000FF FFFF00 FF00FF 00FFFF 000000 FFFFFF FF0000 00FF00 0000FF FFFF00 FF00FF 00FFFF 800000 008000 000080 808000 800080 008080 C0C0C0 808080 9999FF 993366 FFFFCC CCFFFF 660066 FF8080 0066CC CCCCFF 000080 FF00FF FFFF00 00FFFF 800080 800000 008080 0000FF 00CCFF CCFFFF CCFFCC FFFF99 99CCFF FF99CC CC99FF FFCC99 3366FF 33CCCC 99CC00 FFCC00 FF9900 FF6600 666699 969696 003366 339966 003300 333300 993300 993366 333399 333333".split(" ");
|
|
19
|
+
/** Theme colour index (as cells use it) to clrScheme position: the first two pairs are swapped (§3.5.6). */
|
|
20
|
+
const THEME_ORDER = [
|
|
21
|
+
1,
|
|
22
|
+
0,
|
|
23
|
+
3,
|
|
24
|
+
2,
|
|
25
|
+
4,
|
|
26
|
+
5,
|
|
27
|
+
6,
|
|
28
|
+
7,
|
|
29
|
+
8,
|
|
30
|
+
9,
|
|
31
|
+
10,
|
|
32
|
+
11
|
|
33
|
+
];
|
|
34
|
+
function argb(s) {
|
|
35
|
+
if (typeof s !== "string") return void 0;
|
|
36
|
+
const hex = s.startsWith("#") ? s.slice(1) : s;
|
|
37
|
+
if (/^[0-9a-f]{6}$/i.test(hex)) return `FF${hex.toUpperCase()}`;
|
|
38
|
+
if (/^[0-9a-f]{8}$/i.test(hex)) return hex.toUpperCase();
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Resolves any `Color` to ARGB ('FFRRGGBB'). Theme colours use the file's
|
|
42
|
+
* theme and apply the tint (ECMA-376 §18.8.19, on HSL luminance); indexed
|
|
43
|
+
* colours use the file's palette. Without them, the defaults are used.
|
|
44
|
+
* Automatic colour and anything unreadable give black.
|
|
45
|
+
*/
|
|
46
|
+
function resolveColor(color, ctx) {
|
|
47
|
+
if (typeof color === "string") return argb(color) ?? "FF000000";
|
|
48
|
+
if ("rgb" in color) return argb(color.rgb) ?? "FF000000";
|
|
49
|
+
if ("indexed" in color) {
|
|
50
|
+
const i = color.indexed;
|
|
51
|
+
if (i === 64) return "FF000000";
|
|
52
|
+
if (i === 65) return "FFFFFFFF";
|
|
53
|
+
return argb(ctx?.indexedColors?.[i]) ?? (PALETTE[i] ? `FF${PALETTE[i]}` : "FF000000");
|
|
54
|
+
}
|
|
55
|
+
if ("theme" in color) {
|
|
56
|
+
const pos = THEME_ORDER[color.theme];
|
|
57
|
+
if (pos === void 0) return "FF000000";
|
|
58
|
+
const base = argb(ctx?.theme?.colors[pos]) ?? `FF${DEFAULT_THEME[pos]}`;
|
|
59
|
+
return color.tint ? tinted(base, color.tint) : base;
|
|
60
|
+
}
|
|
61
|
+
return "FF000000";
|
|
62
|
+
}
|
|
63
|
+
/** Applies a tint to the HSL luminance: darker for negative tints, lighter for positive ones. */
|
|
64
|
+
function tinted(base, tint) {
|
|
65
|
+
const [h, l, s] = rgbToHls(Number.parseInt(base.slice(2, 4), 16) / 255, Number.parseInt(base.slice(4, 6), 16) / 255, Number.parseInt(base.slice(6, 8), 16) / 255);
|
|
66
|
+
const t = Math.max(-1, Math.min(1, tint));
|
|
67
|
+
const [r2, g2, b2] = hlsToRgb(h, t < 0 ? l * (1 + t) : l * (1 - t) + t, s);
|
|
68
|
+
const hex = (x) => Math.round(Math.max(0, Math.min(1, x)) * 255).toString(16).padStart(2, "0").toUpperCase();
|
|
69
|
+
return `${base.slice(0, 2)}${hex(r2)}${hex(g2)}${hex(b2)}`;
|
|
70
|
+
}
|
|
71
|
+
function rgbToHls(r, g, b) {
|
|
72
|
+
const max = Math.max(r, g, b);
|
|
73
|
+
const min = Math.min(r, g, b);
|
|
74
|
+
const l = (max + min) / 2;
|
|
75
|
+
if (max === min) return [
|
|
76
|
+
0,
|
|
77
|
+
l,
|
|
78
|
+
0
|
|
79
|
+
];
|
|
80
|
+
const d = max - min;
|
|
81
|
+
const s = l <= .5 ? d / (max + min) : d / (2 - max - min);
|
|
82
|
+
let h;
|
|
83
|
+
if (max === r) h = (g - b) / d;
|
|
84
|
+
else if (max === g) h = 2 + (b - r) / d;
|
|
85
|
+
else h = 4 + (r - g) / d;
|
|
86
|
+
h = (h / 6 % 1 + 1) % 1;
|
|
87
|
+
return [
|
|
88
|
+
h,
|
|
89
|
+
l,
|
|
90
|
+
s
|
|
91
|
+
];
|
|
92
|
+
}
|
|
93
|
+
function hlsToRgb(h, l, s) {
|
|
94
|
+
if (s === 0) return [
|
|
95
|
+
l,
|
|
96
|
+
l,
|
|
97
|
+
l
|
|
98
|
+
];
|
|
99
|
+
const m2 = l <= .5 ? l * (1 + s) : l + s - l * s;
|
|
100
|
+
const m1 = 2 * l - m2;
|
|
101
|
+
const v = (hue) => {
|
|
102
|
+
const x = (hue % 1 + 1) % 1;
|
|
103
|
+
if (x < 1 / 6) return m1 + (m2 - m1) * x * 6;
|
|
104
|
+
if (x < .5) return m2;
|
|
105
|
+
if (x < 2 / 3) return m1 + (m2 - m1) * (2 / 3 - x) * 6;
|
|
106
|
+
return m1;
|
|
107
|
+
};
|
|
108
|
+
return [
|
|
109
|
+
v(h + 1 / 3),
|
|
110
|
+
v(h),
|
|
111
|
+
v(h - 1 / 3)
|
|
112
|
+
];
|
|
113
|
+
}
|
|
114
|
+
export { resolveColor as t };
|