@rosperitus/excelerate 0.11.0 → 0.12.3

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 CHANGED
@@ -1,15 +1,17 @@
1
1
  # excelerate
2
2
 
3
- Read, write and recalculate spreadsheets in Node - xlsx, xls, ods, csv, html
4
- and more - with a real formula engine (443 Excel functions). Rust compiled to
5
- WebAssembly, so there is no native module to build, no Python, no headless
6
- Office.
3
+ Read, write and recalculate spreadsheets in Node and the browser - xlsx, xls,
4
+ xlsb, ods, csv, html and more - with a formula engine of 518 Excel functions.
5
+ Rust compiled to WebAssembly: no native module to build, no Python, no
6
+ headless Office.
7
7
 
8
- ```
8
+ ```text
9
9
  npm install @rosperitus/excelerate
10
10
  ```
11
11
 
12
- TypeScript declarations ship with the package; nothing extra to install.
12
+ TypeScript declarations ship with the package. Only need to read? The
13
+ `@rosperitus/excelerate-reader` package has the reading half of the API in a
14
+ wasm file of 1.0 MB instead of 2.3 MB.
13
15
 
14
16
  ## Quick start
15
17
 
@@ -30,21 +32,39 @@ book.recalculateFrom(0, "B4"); // only what depends on B4
30
32
  writeFileSync("out.xlsx", book.toXlsx());
31
33
  ```
32
34
 
33
- Build one from scratch:
35
+ Build a report from scratch - values, a formula, a styled header, a sort and
36
+ a total row:
34
37
 
35
38
  ```ts
36
39
  import { Book } from "@rosperitus/excelerate";
40
+ import { writeFileSync } from "node:fs";
37
41
 
38
42
  const book = new Book();
39
- book.set(0, "A1", "Item");
40
- book.set(0, "B1", "Price");
41
- book.set(0, "A2", "Bolt");
42
- book.set(0, "B2", 7.5);
43
- book.set(0, "A3", "Total");
44
- book.set(0, "B3", "=SUM(B2:B2)"); // a string starting with = is a formula
43
+ book.setRange(0, "A1", [
44
+ ["Item", "Qty", "Price", "Sum"],
45
+ ["Bolt", 40, 0.25, "=B2*C2"],
46
+ ["Nut", 100, 0.1, null],
47
+ ["Washer", 250, 0.05, null],
48
+ ]);
49
+ book.fillDown(0, "D2:D4"); // =B3*C3, =B4*C4
50
+ book.setRangeStyle(0, "A1:D1", { font: { bold: true }, fill: { pattern: "solid", foreground: "#FFDDEBF7" } });
51
+ book.setRangeStyle(0, "C2:D5", { numberFormat: "#,##0.00" });
52
+
53
+ book.recalculate(); // the sort reads cached values
54
+ book.sortRange(0, "A1:D4", ["-Sum"], { header: true });
55
+ book.insertRows(0, 5, 1); // styled like the row above
56
+ book.setRange(0, "A5", [["Total", null, null, "=SUM(D2:D4)"]]);
45
57
 
46
58
  book.recalculate();
47
- console.log(book.toCsv(0));
59
+ book.freezePanes(0, 1, 0);
60
+ writeFileSync("report.xlsx", book.toXlsx());
61
+ ```
62
+
63
+ Continue a series the way the fill handle does:
64
+
65
+ ```ts
66
+ book.setRange(0, "F1", [["Jan", "Кв1", 1], [null, null, 3]]);
67
+ book.fillSeries(0, "F1:H6", "down"); // Feb..Jun, Кв2..Кв6, 5, 7, 9, 11
48
68
  ```
49
69
 
50
70
  ## API
@@ -52,11 +72,14 @@ console.log(book.toCsv(0));
52
72
  ```ts
53
73
  type CellValue = number | string | boolean | null;
54
74
  type CellGrid = CellValue[][];
75
+ type CopyOrigin = "before" | "after" | "none"; // whose formatting new rows take
76
+ type SortKeys = (number | string)[]; // 2 = column B, "-Amount" = header, largest first
55
77
 
56
78
  class Book {
57
79
  constructor();
58
80
  static fromXlsx(bytes: Uint8Array): Book;
59
81
  static read(bytes: Uint8Array, name?: string | null, maxExpanded?: number | null): Book;
82
+ static readCsv(bytes: Uint8Array, options: CsvOptions): Book; // shape stated, not guessed
60
83
 
61
84
  // Sheets
62
85
  sheetNames(): string[];
@@ -65,8 +88,10 @@ class Book {
65
88
  renameSheet(sheet: number, title: string): void;
66
89
  activeSheet(): number;
67
90
  setActiveSheet(sheet: number): void;
91
+ removeSheet(sheet: number): void; // references to it become #REF!
68
92
  cellCount(sheet?: number | null): number;
69
- usedRange(sheet: number): string | undefined; // "A1:D9"
93
+ usedRange(sheet: number): string | undefined; // "A1:D9", by walking the cells
94
+ usedRangeHint(sheet: number): string | undefined; // the same, never walking; columns may be wider after a removal
70
95
 
71
96
  // Cells, by address
72
97
  get(sheet: number, address: string): CellValue;
@@ -76,6 +101,8 @@ class Book {
76
101
  getFormatted(sheet: number, address: string): string; // through the number format
77
102
  getRange(sheet: number, range: string): CellGrid; // one call, not one per cell
78
103
  setRange(sheet: number, at: string, values: CellGrid): void;
104
+ getRangeStyles(sheet: number, range: string): RangeStyles; // { styles, grid } - each style once
105
+ setRangeStyles(sheet: number, at: string, styles: RangeStylesPatch): void;
79
106
 
80
107
  // Cells, by 1-based row and column - no address to build and re-parse
81
108
  getAt(sheet: number, row: number, column: number): CellValue;
@@ -99,8 +126,87 @@ class Book {
99
126
  sheetVisibility(sheet: number): "visible" | "hidden" | "veryHidden";
100
127
  mergedRanges(sheet: number): string[]; // ["A1:C1", ...]
101
128
  mergedRangesAt(sheet: number): Uint32Array; // [r1, c1, r2, c2] per area
129
+ merge(sheet: number, range: string): void;
130
+ unmerge(sheet: number, range: string): boolean;
131
+ columnWidth(sheet: number, column: number): number | undefined; // characters
132
+ rowHeight(sheet: number, row: number): number | undefined; // points
133
+ setColumnWidth(sheet: number, column: number, width?: number): void; // undefined: back to the default
134
+ setRowHeight(sheet: number, row: number, height?: number): void;
135
+ setColumnHidden(sheet: number, column: number, hidden: boolean): void;
136
+ setRowHidden(sheet: number, row: number, hidden: boolean): void;
137
+ setSheetVisibility(sheet: number, state: "visible" | "hidden" | "veryHidden"): void;
102
138
  getRowAt(sheet: number, row: number, formatted?: boolean): SheetRow;
103
139
 
140
+ // The grid itself: formulas everywhere in the workbook follow the cells they
141
+ // read, and so do merges, links, validations, tables and drawings
142
+ insertRows(sheet: number, at: number, count: number, copyOrigin?: CopyOrigin): void;
143
+ removeRows(sheet: number, at: number, count: number): void;
144
+ insertColumns(sheet: number, at: number, count: number, copyOrigin?: CopyOrigin): void; // 1-based
145
+ removeColumns(sheet: number, at: number, count: number): void;
146
+ copyRange(sheet: number, range: string, to: string, toSheet?: number): void; // formulas rewritten
147
+ moveRange(sheet: number, range: string, to: string, toSheet?: number): void; // formulas kept, references follow
148
+ insertCells(sheet: number, range: string, shift: "down" | "right", copyOrigin?: CopyOrigin): void;
149
+ removeCells(sheet: number, range: string, shift: "up" | "left"): void;
150
+ moveSheet(from: number, to: number): void;
151
+
152
+ // Sorting and filling, as Excel's Data - Sort, Ctrl+D and the fill handle
153
+ sortRange(sheet: number, range: string, keys: SortKeys, options?: SortRangeOptions): void;
154
+ sortTable(name: string, keys: SortKeys): void;
155
+ fillDown(sheet: number, range: string): void;
156
+ fillRight(sheet: number, range: string): void;
157
+ fillSeries(sheet: number, range: string, direction: "down" | "right" | "up" | "left"): void;
158
+
159
+ // What is on a sheet besides cells
160
+ comments(sheet: number): SheetComment[];
161
+ hyperlinks(sheet: number): SheetHyperlink[];
162
+ tables(sheet: number): SheetTable[];
163
+ charts(sheet: number): SheetChart[];
164
+ images(sheet: number): SheetImage[]; // without the bytes
165
+ imageData(sheet: number, index: number): Uint8Array;
166
+ shapes(sheet: number): SheetShape[];
167
+
168
+ // Style, written as a patch over what the cell has
169
+ setCellStyle(sheet: number, address: string, patch: CellStylePatch): void;
170
+ setCellStyleAt(sheet: number, row: number, column: number, patch: CellStylePatch): void;
171
+ setRangeStyle(sheet: number, range: string, patch: CellStylePatch): void;
172
+
173
+ // Notes, links, tables
174
+ setComment(sheet: number, address: string, author: string, text: string): void;
175
+ removeComment(sheet: number, address: string): boolean;
176
+ setHyperlink(sheet: number, range: string, target: string, inside?: boolean, display?: string, tooltip?: string): void;
177
+ removeHyperlink(sheet: number, range: string): boolean;
178
+ addTable(sheet: number, name: string, range: string, headerRow?: boolean): void;
179
+ removeTable(sheet: number, name: string): boolean;
180
+
181
+ // The saved view
182
+ sheetView(sheet: number): SheetViewInfo;
183
+ freezePanes(sheet: number, rows: number, columns: number): void; // (0, 1, 0) pins the header
184
+ unfreezePanes(sheet: number): void;
185
+ setZoom(sheet: number, percent?: number): void;
186
+ setShowGridLines(sheet: number, show: boolean, headers?: boolean): void;
187
+
188
+ // Names
189
+ definedNames(): WorkbookName[];
190
+ setDefinedName(name: string, formula: string, sheet?: number): void;
191
+ removeDefinedName(name: string, sheet?: number): boolean;
192
+
193
+ // Rules a file states
194
+ dataValidations(sheet: number): SheetValidation[];
195
+ conditionalFormats(sheet: number): SheetConditionalFormat[];
196
+ autoFilter(sheet: number): SheetAutoFilter | undefined;
197
+ pivotTables(sheet: number): SheetPivotTable[];
198
+ arrayFormulas(sheet: number): string[]; // ["B2:B4"]
199
+ externalBooks(): (string | undefined)[];
200
+
201
+ // Locks - they stop editing, they do not encrypt
202
+ protectSheet(sheet: number, password?: string): void;
203
+ unprotectSheet(sheet: number): void;
204
+ sheetProtection(sheet: number): ProtectionInfo;
205
+ verifySheetPassword(sheet: number, password: string): boolean;
206
+ protectWorkbook(password?: string, windows?: boolean): void;
207
+ unprotectWorkbook(): void;
208
+ workbookProtection(): ProtectionInfo;
209
+
104
210
  // Calculation
105
211
  evaluate(sheet: number, address: string, formula: string): CellValue;
106
212
  recalculate(sheet?: number | null): number;
@@ -114,6 +220,7 @@ class Book {
114
220
  toXls(): Uint8Array;
115
221
  toHtml(sheet?: number | null, fragment?: boolean | null): string;
116
222
  toCsv(sheet: number): string;
223
+ toCsvWith(sheet: number, options: CsvOptions): string; // a delimiter of your own
117
224
 
118
225
  free(): void;
119
226
  }
@@ -140,6 +247,32 @@ missing sheet, a truncated package.
140
247
  - **Big files.** Zip expansion is capped at 512 MB to stop a zip bomb. Real
141
248
  workbooks do exceed it (a 100 MB package can expand to 560 MB), so pass a
142
249
  larger `maxExpanded` third argument to `Book.read` when you know the source.
250
+ - **Editing the grid.** `insertRows` and its three siblings move the whole
251
+ workbook, not one sheet: `$A$5` becomes `$A$6`, a range that lost cells
252
+ narrows, a deleted cell reads `#REF!`, and drawings and tables follow. The
253
+ cached result of a rewritten formula is dropped, so recalculate after.
254
+ - **Style is a patch.** `setCellStyle(0, "A1", { font: { bold: true } })` keeps
255
+ the number format the cell had. Equal styles share one entry in the
256
+ workbook's table, so painting a column costs one style, not one per cell -
257
+ and `setRangeStyle` is one call for the whole rectangle.
258
+ - **Copy rewrites, move does not.** `copyRange` rewrites the formulas it
259
+ carries, as if they had been written where they land; `moveRange` keeps them
260
+ pointing at the same cells and rewrites the formulas elsewhere that read the
261
+ moved ones. That is Ctrl+C against Ctrl+X, and it is the whole difference.
262
+ - **Inserted rows look like their neighbours.** `insertRows` copies the row
263
+ above's cell styles and height, as Excel does. Pass `"after"` for the row
264
+ below, `"none"` for a blank row.
265
+ - **Sort keys are numbers or names.** `sortRange(0, "A1:D99", [3, -2])` sorts
266
+ by column C, then by B largest first; with `{ header: true }` the same reads
267
+ `["Price", "-Qty"]`. Formulas sort by their cached value, so recalculate
268
+ first if you have edited the inputs. `sortTable("Sales", ["-Amount"])`
269
+ needs no range at all.
270
+ - **Read and write formatting by the block.** `getRangeStyles` returns each
271
+ distinct style once and a grid of indexes into them; `setRangeStyles` takes
272
+ the same shape back, so copying a block's look is two calls.
273
+ - **Charts, pictures and shapes are read-only here.** `charts`, `images` and
274
+ `shapes` describe what a sheet carries; the parts themselves travel through
275
+ a write byte for byte.
143
276
  - **Cached results.** A formula cell read from a file carries whatever the app
144
277
  that saved it computed. Call `recalculate()` before relying on those numbers.
145
278
 
@@ -157,8 +290,9 @@ missing sheet, a truncated package.
157
290
  SYLK, Gnumeric and SpreadsheetML are read-only in the Rust crate too - there is
158
291
  nothing to bind.
159
292
 
160
- One catch on `toXls`: BIFF8 cannot store a formula as text, so every formula
161
- goes out as its last computed value. Call `recalculate()` first.
293
+ `toXls` compiles formulas to BIFF8 tokens with their cached results; what
294
+ BIFF8 cannot hold (a structured reference, a row past 65,536) goes out as its
295
+ value. Call `recalculate()` first so those values are current.
162
296
 
163
297
  ## Development
164
298
 
@@ -172,14 +306,17 @@ npm start # example.js: build a book, calculate, round-trip
172
306
  npm run ts # typescript/basic.ts, no build step
173
307
  npm run typecheck # tsc --strict over the TypeScript examples
174
308
  npm run bench [iterations] # parse timings, median and best
309
+ ../tools/pack-npm.sh # pkg -> pkg-publish, then: cd pkg-publish && npm publish --otp=...
175
310
  ```
176
311
 
177
312
  | Path | What it is |
178
313
  |---|---|
179
- | `pkg/` | build output, published as-is (git-ignored) |
314
+ | `pkg/` | build output for Node (git-ignored); `pkg-web/` and `pkg-bundler/` for the other targets |
315
+ | `pkg-publish/` | `tools/pack-npm.sh`: a copy of `pkg/` renamed to `@rosperitus/excelerate`, the folder `npm publish` runs in (git-ignored) |
180
316
  | `example.js`, `test.js`, `bench.js` | JavaScript examples, tests, benchmark |
181
317
  | `typescript/` | TypeScript examples, run from here: `node typescript/basic.ts` (Node 22.6+). No node_modules of their own - they resolve the package from this folder |
182
- | `files/` | workbooks the benchmark and tests use (git-ignored except `gen.xlsx`) |
318
+ | `browser/` | the same API in a page: `index.html` over `pkg-web/` |
319
+ | `files/` | workbooks the tests read (`gen.xlsx`, `grouped.xlsx`, committed) and whatever the benchmark and examples read and write (git-ignored) |
183
320
 
184
321
  ## License
185
322