@rosperitus/excelerate 0.12.0 → 0.13.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/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
77
+ type Progress = { stage: "reading" | "writing" | "recalculating"; done: number;
78
+ total: number | null; what: string; fraction: number | null };
55
79
 
56
80
  class Book {
57
81
  constructor();
58
- static fromXlsx(bytes: Uint8Array): Book;
59
- static read(bytes: Uint8Array, name?: string | null, maxExpanded?: number | null): Book;
82
+ static read(bytes: Uint8Array, name?: string | null, maxExpanded?: number | null, onProgress?: (p: Progress) => void): Book;
60
83
  static readCsv(bytes: Uint8Array, options: CsvOptions): Book; // shape stated, not guessed
61
84
 
62
85
  // Sheets
@@ -69,7 +92,7 @@ class Book {
69
92
  removeSheet(sheet: number): void; // references to it become #REF!
70
93
  cellCount(sheet?: number | null): number;
71
94
  usedRange(sheet: number): string | undefined; // "A1:D9", by walking the cells
72
- usedRangeHint(sheet: number): string | undefined; // the same, as the file states it
95
+ usedRangeHint(sheet: number): string | undefined; // the same, never walking; columns may be wider after a removal
73
96
 
74
97
  // Cells, by address
75
98
  get(sheet: number, address: string): CellValue;
@@ -79,6 +102,8 @@ class Book {
79
102
  getFormatted(sheet: number, address: string): string; // through the number format
80
103
  getRange(sheet: number, range: string): CellGrid; // one call, not one per cell
81
104
  setRange(sheet: number, at: string, values: CellGrid): void;
105
+ getRangeStyles(sheet: number, range: string): RangeStyles; // { styles, grid } - each style once
106
+ setRangeStyles(sheet: number, at: string, styles: RangeStylesPatch): void;
82
107
 
83
108
  // Cells, by 1-based row and column - no address to build and re-parse
84
109
  getAt(sheet: number, row: number, column: number): CellValue;
@@ -88,6 +113,8 @@ class Book {
88
113
  getFormattedAt(sheet: number, row: number, column: number): string;
89
114
  getRangeAt(sheet: number, row: number, column: number, rows: number, columns: number): CellGrid;
90
115
  setRangeAt(sheet: number, row: number, column: number, values: CellGrid): void;
116
+ getRangeStylesAt(sheet: number, row: number, column: number, rows: number, columns: number): RangeStyles;
117
+ setRangeStylesAt(sheet: number, row: number, column: number, styles: RangeStylesPatch): void;
91
118
  recalculateCellAt(sheet: number, row: number, column: number): boolean;
92
119
  recalculateFromAt(sheet: number, row: number, column: number): number;
93
120
 
@@ -115,16 +142,23 @@ class Book {
115
142
 
116
143
  // The grid itself: formulas everywhere in the workbook follow the cells they
117
144
  // read, and so do merges, links, validations, tables and drawings
118
- insertRows(sheet: number, at: number, count: number): void;
145
+ insertRows(sheet: number, at: number, count: number, copyOrigin?: CopyOrigin): void;
119
146
  removeRows(sheet: number, at: number, count: number): void;
120
- insertColumns(sheet: number, at: number, count: number): void; // 1-based
147
+ insertColumns(sheet: number, at: number, count: number, copyOrigin?: CopyOrigin): void; // 1-based
121
148
  removeColumns(sheet: number, at: number, count: number): void;
122
149
  copyRange(sheet: number, range: string, to: string, toSheet?: number): void; // formulas rewritten
123
150
  moveRange(sheet: number, range: string, to: string, toSheet?: number): void; // formulas kept, references follow
124
- insertCells(sheet: number, range: string, shift: "down" | "right"): void;
151
+ insertCells(sheet: number, range: string, shift: "down" | "right", copyOrigin?: CopyOrigin): void;
125
152
  removeCells(sheet: number, range: string, shift: "up" | "left"): void;
126
153
  moveSheet(from: number, to: number): void;
127
154
 
155
+ // Sorting and filling, as Excel's Data - Sort, Ctrl+D and the fill handle
156
+ sortRange(sheet: number, range: string, keys: SortKeys, options?: SortRangeOptions): void;
157
+ sortTable(name: string, keys: SortKeys): void;
158
+ fillDown(sheet: number, range: string): void;
159
+ fillRight(sheet: number, range: string): void;
160
+ fillSeries(sheet: number, range: string, direction: "down" | "right" | "up" | "left"): void;
161
+
128
162
  // What is on a sheet besides cells
129
163
  comments(sheet: number): SheetComment[];
130
164
  hyperlinks(sheet: number): SheetHyperlink[];
@@ -134,7 +168,9 @@ class Book {
134
168
  imageData(sheet: number, index: number): Uint8Array;
135
169
  shapes(sheet: number): SheetShape[];
136
170
 
137
- // Style, written as a patch over what the cell has
171
+ // Style: read whole, written as a patch over what the cell has
172
+ cellStyle(sheet: number, address: string): CellStyle; // numberFormat, font, fill, borders, alignment
173
+ cellStyleAt(sheet: number, row: number, column: number): CellStyle;
138
174
  setCellStyle(sheet: number, address: string, patch: CellStylePatch): void;
139
175
  setCellStyleAt(sheet: number, row: number, column: number, patch: CellStylePatch): void;
140
176
  setRangeStyle(sheet: number, range: string, patch: CellStylePatch): void;
@@ -149,8 +185,7 @@ class Book {
149
185
 
150
186
  // The saved view
151
187
  sheetView(sheet: number): SheetViewInfo;
152
- freezePanes(sheet: number, rows: number, columns: number): void; // (0, 1, 0) pins the header
153
- unfreezePanes(sheet: number): void;
188
+ freezePanes(sheet: number, rows: number, columns: number): void; // (0, 1, 0) pins the header, (0, 0, 0) unfreezes
154
189
  setZoom(sheet: number, percent?: number): void;
155
190
  setShowGridLines(sheet: number, show: boolean, headers?: boolean): void;
156
191
 
@@ -178,18 +213,22 @@ class Book {
178
213
 
179
214
  // Calculation
180
215
  evaluate(sheet: number, address: string, formula: string): CellValue;
181
- recalculate(sheet?: number | null): number;
216
+ recalculate(sheet?: number | null, onProgress?: (p: Progress) => void): number;
182
217
  recalculateCell(sheet: number, address: string): boolean;
183
218
  recalculateFrom(sheet: number, address: string): number;
184
219
  recalculateFromMany(sheet: number, addresses: string[]): number;
185
220
 
221
+ // Functions of your own, for names no built-in claims
222
+ registerFunction(name: string, fn: (...args: (CellValue | CellGrid)[]) => CellValue | CellGrid): void;
223
+ unregisterFunction(name: string): boolean;
224
+ registeredFunctions(): string[];
225
+
186
226
  // Output
187
- toXlsx(): Uint8Array;
227
+ toXlsx(onProgress?: (p: Progress) => void): Uint8Array;
188
228
  toOds(): Uint8Array;
189
229
  toXls(): Uint8Array;
190
230
  toHtml(sheet?: number | null, fragment?: boolean | null): string;
191
- toCsv(sheet: number): string;
192
- toCsvWith(sheet: number, options: CsvOptions): string; // a delimiter of your own
231
+ toCsv(sheet: number, options?: CsvOptions): string; // { delimiter: ";" } for a delimiter of your own
193
232
 
194
233
  free(): void;
195
234
  }
@@ -228,8 +267,28 @@ missing sheet, a truncated package.
228
267
  carries, as if they had been written where they land; `moveRange` keeps them
229
268
  pointing at the same cells and rewrites the formulas elsewhere that read the
230
269
  moved ones. That is Ctrl+C against Ctrl+X, and it is the whole difference.
231
- - **Objects are read-only here.** `charts`, `images` and `shapes` describe what
232
- a sheet carries; the parts themselves travel through a write byte for byte.
270
+ - **Inserted rows look like their neighbours.** `insertRows` copies the row
271
+ above's cell styles and height, as Excel does. Pass `"after"` for the row
272
+ below, `"none"` for a blank row.
273
+ - **Sort keys are numbers or names.** `sortRange(0, "A1:D99", [3, -2])` sorts
274
+ by column C, then by B largest first; with `{ header: true }` the same reads
275
+ `["Price", "-Qty"]`. Formulas sort by their cached value, so recalculate
276
+ first if you have edited the inputs. `sortTable("Sales", ["-Amount"])`
277
+ needs no range at all.
278
+ - **Read and write formatting by the block.** `getRangeStyles` returns each
279
+ distinct style once and a grid of indexes into them; `setRangeStyles` takes
280
+ the same shape back, so copying a block's look is two calls.
281
+ - **Charts, pictures and shapes are read-only here.** `charts`, `images` and
282
+ `shapes` describe what a sheet carries; the parts themselves travel through
283
+ a write byte for byte.
284
+ - **Functions of your own.** `book.registerFunction("MYTOTAL", (range) =>
285
+ range.flat().reduce((a, b) => a + (b ?? 0), 0))` - arguments arrive
286
+ evaluated, a range as a grid. A built-in name stays the built-in's, an
287
+ unregistered one reads `#NAME?`, and a function that throws gives
288
+ `#VALUE!` rather than an exception out of the formula.
289
+ - **Progress.** `Book.read`, `toXlsx` and `recalculate` take a callback last;
290
+ it gets `{ stage, done, total, what, fraction }` per sheet, part or formula.
291
+ `total` is `null` while a read has not reached the workbook part yet.
233
292
  - **Cached results.** A formula cell read from a file carries whatever the app
234
293
  that saved it computed. Call `recalculate()` before relying on those numbers.
235
294
 
@@ -247,8 +306,9 @@ missing sheet, a truncated package.
247
306
  SYLK, Gnumeric and SpreadsheetML are read-only in the Rust crate too - there is
248
307
  nothing to bind.
249
308
 
250
- One catch on `toXls`: BIFF8 cannot store a formula as text, so every formula
251
- goes out as its last computed value. Call `recalculate()` first.
309
+ `toXls` compiles formulas to BIFF8 tokens with their cached results; what
310
+ BIFF8 cannot hold (a structured reference, a row past 65,536) goes out as its
311
+ value. Call `recalculate()` first so those values are current.
252
312
 
253
313
  ## Development
254
314
 
@@ -262,14 +322,17 @@ npm start # example.js: build a book, calculate, round-trip
262
322
  npm run ts # typescript/basic.ts, no build step
263
323
  npm run typecheck # tsc --strict over the TypeScript examples
264
324
  npm run bench [iterations] # parse timings, median and best
325
+ ../tools/pack-npm.sh # pkg -> pkg-publish, then: cd pkg-publish && npm publish --otp=...
265
326
  ```
266
327
 
267
328
  | Path | What it is |
268
329
  |---|---|
269
- | `pkg/` | build output, published as-is (git-ignored) |
330
+ | `pkg/` | build output for Node (git-ignored); `pkg-web/` and `pkg-bundler/` for the other targets |
331
+ | `pkg-publish/` | `tools/pack-npm.sh`: a copy of `pkg/` renamed to `@rosperitus/excelerate`, the folder `npm publish` runs in (git-ignored) |
270
332
  | `example.js`, `test.js`, `bench.js` | JavaScript examples, tests, benchmark |
271
333
  | `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 |
272
- | `files/` | workbooks the benchmark and tests use (git-ignored except `gen.xlsx`) |
334
+ | `browser/` | the same API in a page: `index.html` over `pkg-web/` |
335
+ | `files/` | workbooks the tests read (`gen.xlsx`, `grouped.xlsx`, committed) and whatever the benchmark and examples read and write (git-ignored) |
273
336
 
274
337
  ## License
275
338
 
package/excelerate.d.ts CHANGED
@@ -33,6 +33,27 @@ export interface CellStylePatch {
33
33
  textRotation: number;
34
34
  }>;
35
35
  }
36
+ /**
37
+ * Sort keys: a 1-based column number of the sheet (a row number when sorting
38
+ * columns) or a header's text; a minus in front sorts largest first.
39
+ */
40
+ export type SortKeys = (number | string)[];
41
+ /** How `sortRange` reads its range. */
42
+ export interface SortRangeOptions {
43
+ /** The first row (or column) is a header: it stays put and names keys. */
44
+ header?: boolean;
45
+ /** Reorder columns left to right instead of rows. */
46
+ byColumns?: boolean;
47
+ }
48
+ /**
49
+ * Styles to write over a rectangle, as `setRangeStyles` takes them: `grid`
50
+ * points into `styles`, and `null` or `-1` leaves its cell as it is. What
51
+ * `getRangeStyles` returns fits here unchanged.
52
+ */
53
+ export interface RangeStylesPatch {
54
+ styles: CellStylePatch[];
55
+ grid: (number | null)[][];
56
+ }
36
57
 
37
58
 
38
59
 
@@ -191,6 +212,14 @@ export interface CellStyle {
191
212
  textRotation: number;
192
213
  };
193
214
  }
215
+ /**
216
+ * The styles of a rectangle, as `getRangeStyles` returns them: each distinct
217
+ * style once in `styles`, and `grid` - row by row - pointing into it.
218
+ */
219
+ export interface RangeStyles {
220
+ styles: CellStyle[];
221
+ grid: number[][];
222
+ }
194
223
 
195
224
 
196
225
 
@@ -328,23 +357,35 @@ export class Book {
328
357
  */
329
358
  externalBooks(): any[];
330
359
  /**
331
- * Freezes the first `rows` rows and `columns` columns, so they stay put
332
- * while the rest scrolls. `freezePanes(0, 1, 0)` pins the header row.
360
+ * Copies the first row of a range into the rows below it, as Ctrl+D.
333
361
  */
334
- freezePanes(sheet: number, rows: number, columns: number): void;
362
+ fillDown(sheet: number, range: string): void;
363
+ /**
364
+ * Copies the first column of a range into the columns to its right, as
365
+ * Ctrl+R.
366
+ */
367
+ fillRight(sheet: number, range: string): void;
335
368
  /**
336
- * Reads an xlsx package from its bytes.
369
+ * Continues what the first cells of a range start, as dragging the fill
370
+ * handle does: `1, 2` goes on `3, 4`, `Кв1` to `Кв2`, `Jan` to `Feb`, a
371
+ * date by a day; anything else repeats. `"down"` fills each column,
372
+ * `"right"` each row; `"up"` and `"left"` run the series back from the
373
+ * last cells.
337
374
  */
338
- static fromXlsx(bytes: Uint8Array): Book;
375
+ fillSeries(sheet: number, range: string, direction: "down" | "right" | "up" | "left"): void;
376
+ /**
377
+ * Freezes the first `rows` rows and `columns` columns, so they stay put
378
+ * while the rest scrolls. `freezePanes(0, 1, 0)` pins the header row;
379
+ * `freezePanes(0, 0, 0)` unfreezes.
380
+ */
381
+ freezePanes(sheet: number, rows: number, columns: number): void;
339
382
  /**
340
383
  * The value at `address` on sheet `sheet`, as a JS number, string, boolean
341
384
  * or `null`. A formula cell gives its cached result, not its text.
342
385
  */
343
386
  get(sheet: number, address: string): CellValue;
344
387
  /**
345
- * The same by 1-based row and column, the numbers `ROW()` and `COLUMN()`
346
- * return: `getAt(0, 4, 2)` is `B4`. Saves formatting an address only to
347
- * parse it again.
388
+ * The same by 1-based row and column.
348
389
  */
349
390
  getAt(sheet: number, row: number, column: number): CellValue;
350
391
  /**
@@ -375,6 +416,17 @@ export class Book {
375
416
  * to take. `getRangeAt(0, 1, 1, 3, 2)` is `A1:B3`.
376
417
  */
377
418
  getRangeAt(sheet: number, row: number, column: number, rows: number, columns: number): CellGrid;
419
+ /**
420
+ * The styles of a rectangle in one call: each distinct style once, and a
421
+ * grid of indexes into them. `cellStyle` per cell costs a crossing and a
422
+ * whole style object per cell; a formatted table of ten thousand cells
423
+ * usually has a dozen styles.
424
+ */
425
+ getRangeStyles(sheet: number, range: string): RangeStyles;
426
+ /**
427
+ * The same by numbers, as `getRangeAt` takes them.
428
+ */
429
+ getRangeStylesAt(sheet: number, row: number, column: number, rows: number, columns: number): RangeStyles;
378
430
  /**
379
431
  * A whole row in one crossing of the boundary: values, displayed text,
380
432
  * bold and indent, plus whether the row is hidden.
@@ -405,21 +457,25 @@ export class Book {
405
457
  /**
406
458
  * Inserts blank cells over a range, pushing what was there `"down"` or
407
459
  * `"right"` - Excel's "Insert Cells", which moves part of a row rather
408
- * than the whole of it.
460
+ * than the whole of it. `copyOrigin` works as in `insertRows`.
409
461
  */
410
- insertCells(sheet: number, range: string, shift: string): void;
462
+ insertCells(sheet: number, range: string, shift: "down" | "right", copyOrigin?: "before" | "after" | "none"): void;
411
463
  /**
412
464
  * Inserts `count` columns to the left of column `at`, 1-based.
465
+ * `copyOrigin` works as in `insertRows`, with left and right.
413
466
  */
414
- insertColumns(sheet: number, at: number, count: number): void;
467
+ insertColumns(sheet: number, at: number, count: number, copyOrigin?: "before" | "after" | "none"): void;
415
468
  /**
416
469
  * Inserts `count` rows above row `at`, moving everything below down.
470
+ * `copyOrigin` - `"before"` (the row above), `"after"` (the row below)
471
+ * or `"none"` - says whose formatting the new rows take. Left out, it is
472
+ * `"before"`, as in Excel.
417
473
  *
418
474
  * Formulas across the whole workbook follow the cells they read, and so
419
475
  * do merges, links, validations, tables and drawings. `at` is the number
420
476
  * a user sees.
421
477
  */
422
- insertRows(sheet: number, at: number, count: number): void;
478
+ insertRows(sheet: number, at: number, count: number, copyOrigin?: "before" | "after" | "none"): void;
423
479
  /**
424
480
  * Merges a block of cells: `merge(0, "A1:C1")`.
425
481
  */
@@ -560,7 +616,7 @@ export class Book {
560
616
  * Removes the cells of a range, pulling the rest back over the hole from
561
617
  * below (`"up"`) or from the right (`"left"`).
562
618
  */
563
- removeCells(sheet: number, range: string, shift: string): void;
619
+ removeCells(sheet: number, range: string, shift: "up" | "left"): void;
564
620
  /**
565
621
  * Removes `count` columns from column `at`, 1-based.
566
622
  */
@@ -614,7 +670,7 @@ export class Book {
614
670
  * Writes a value: a number, boolean, or string. A string starting with `=`
615
671
  * becomes a formula.
616
672
  */
617
- set(index: number, address: string, value: CellValue): void;
673
+ set(sheet: number, address: string, value: CellValue): void;
618
674
  /**
619
675
  * Sets that tab.
620
676
  */
@@ -690,6 +746,18 @@ export class Book {
690
746
  * created empty, which is what carries the style.
691
747
  */
692
748
  setRangeStyle(sheet: number, range: string, patch: CellStylePatch): void;
749
+ /**
750
+ * Paints a rectangle cell by cell in one call, `at` its top-left corner:
751
+ * `grid` points into `styles`, each a patch as `setCellStyle` takes it,
752
+ * and `null` or `-1` leaves its cell alone. The output of
753
+ * `getRangeStyles` goes back in unchanged, so a block's formatting can be
754
+ * read, edited in JS and written back - or copied somewhere else.
755
+ */
756
+ setRangeStyles(sheet: number, at: string, styles: RangeStylesPatch): void;
757
+ /**
758
+ * The same with the corner given as 1-based row and column.
759
+ */
760
+ setRangeStylesAt(sheet: number, row: number, column: number, styles: RangeStylesPatch): void;
693
761
  /**
694
762
  * Sets the height of a row in points, or gives it back to the sheet
695
763
  * default with `undefined`. `row` is the number a user sees.
@@ -746,20 +814,35 @@ export class Book {
746
814
  * what the index of every other call counts.
747
815
  */
748
816
  sheetVisibility(sheet: number): string;
817
+ /**
818
+ * Sorts the rows of a range, or its columns. A key is a column number of
819
+ * the sheet, 1-based (a row number when sorting columns), or the text of
820
+ * a header when `options.header` says the first line is one; a minus
821
+ * sorts it largest first: `[2, -4]`, `["Region", "-Amount"]`. The first
822
+ * key decides first.
823
+ *
824
+ * ```js
825
+ * book.sortRange(0, "A1:D100", ["Region", "-Amount"], { header: true });
826
+ * ```
827
+ */
828
+ sortRange(sheet: number, range: string, keys: SortKeys, options?: SortRangeOptions): void;
829
+ /**
830
+ * Sorts the data rows of a table, found by name anywhere in the book;
831
+ * its header and totals rows stay put. Keys name its columns, as in
832
+ * `sortRange`: `book.sortTable("Sales", ["-Amount"])`.
833
+ */
834
+ sortTable(name: string, keys: SortKeys): void;
749
835
  /**
750
836
  * The tables of a sheet: what a structured reference like `Sales[Amount]`
751
837
  * names.
752
838
  */
753
839
  tables(sheet: number): SheetTable[];
754
840
  /**
755
- * One sheet as CSV, comma-separated.
841
+ * One sheet as CSV, comma-separated unless `options.delimiter` says
842
+ * otherwise - a semicolon for a locale where the comma is the decimal
843
+ * mark.
756
844
  */
757
- toCsv(sheet: number): string;
758
- /**
759
- * The same with the delimiter stated - a semicolon for a locale where the
760
- * comma is the decimal mark.
761
- */
762
- toCsvWith(sheet: number, options: CsvOptions): string;
845
+ toCsv(sheet: number, options?: CsvOptions): string;
763
846
  /**
764
847
  * The workbook as an HTML page, or one sheet of it. `fragment` leaves out
765
848
  * the `<html>` wrapper, for embedding in a page that already has one.
@@ -772,18 +855,15 @@ export class Book {
772
855
  /**
773
856
  * The workbook as a BIFF8 `.xls` file.
774
857
  *
775
- * The format cannot hold a formula as text, so every formula is written
776
- * as its last computed value; call `recalculate` first if that matters.
858
+ * Formulas are written as tokens with their cached result; what BIFF8
859
+ * cannot hold - a structured reference, a row past 65536 - is written as
860
+ * the value, so call `recalculate` first if the cache is stale.
777
861
  */
778
862
  toXls(): Uint8Array;
779
863
  /**
780
864
  * The workbook as an xlsx package.
781
865
  */
782
866
  toXlsx(on_progress?: Function | null): Uint8Array;
783
- /**
784
- * Unfreezes the panes. The same as `freezePanes(sheet, 0, 0)`.
785
- */
786
- unfreezePanes(sheet: number): void;
787
867
  /**
788
868
  * Takes a merge back out. Answers whether there was one.
789
869
  */
@@ -806,12 +886,12 @@ export class Book {
806
886
  */
807
887
  usedRange(sheet: number): string | undefined;
808
888
  /**
809
- * The used range of a sheet as `"A1:D20"`, taken from what the file says
810
- * rather than from walking the cells; `undefined` when it says nothing.
889
+ * The used range of a sheet as `"A1:D20"` without walking the rows;
890
+ * `undefined` for an empty sheet.
811
891
  *
812
- * A reader that only needs the shape of a sheet pays nothing for it;
813
- * `usedRange` walks every cell and answers for a sheet built in memory
814
- * too, where the file has nothing to say.
892
+ * Rows are exact, columns an upper bound: removing a cell never narrows
893
+ * them. `usedRange` answers the same until a cell in an edge column is
894
+ * removed, and walks the rows after that to give the exact rectangle.
815
895
  */
816
896
  usedRangeHint(sheet: number): string | undefined;
817
897
  /**
package/excelerate.js CHANGED
@@ -367,31 +367,65 @@ class Book {
367
367
  return v1;
368
368
  }
369
369
  /**
370
- * Freezes the first `rows` rows and `columns` columns, so they stay put
371
- * while the rest scrolls. `freezePanes(0, 1, 0)` pins the header row.
370
+ * Copies the first row of a range into the rows below it, as Ctrl+D.
372
371
  * @param {number} sheet
373
- * @param {number} rows
374
- * @param {number} columns
372
+ * @param {string} range
375
373
  */
376
- freezePanes(sheet, rows, columns) {
377
- const ret = wasm.book_freezePanes(this.__wbg_ptr, sheet, rows, columns);
374
+ fillDown(sheet, range) {
375
+ const ptr0 = passStringToWasm0(range, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
376
+ const len0 = WASM_VECTOR_LEN;
377
+ const ret = wasm.book_fillDown(this.__wbg_ptr, sheet, ptr0, len0);
378
378
  if (ret[1]) {
379
379
  throw takeFromExternrefTable0(ret[0]);
380
380
  }
381
381
  }
382
382
  /**
383
- * Reads an xlsx package from its bytes.
384
- * @param {Uint8Array} bytes
385
- * @returns {Book}
383
+ * Copies the first column of a range into the columns to its right, as
384
+ * Ctrl+R.
385
+ * @param {number} sheet
386
+ * @param {string} range
386
387
  */
387
- static fromXlsx(bytes) {
388
- const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc);
388
+ fillRight(sheet, range) {
389
+ const ptr0 = passStringToWasm0(range, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
389
390
  const len0 = WASM_VECTOR_LEN;
390
- const ret = wasm.book_fromXlsx(ptr0, len0);
391
- if (ret[2]) {
392
- throw takeFromExternrefTable0(ret[1]);
391
+ const ret = wasm.book_fillRight(this.__wbg_ptr, sheet, ptr0, len0);
392
+ if (ret[1]) {
393
+ throw takeFromExternrefTable0(ret[0]);
394
+ }
395
+ }
396
+ /**
397
+ * Continues what the first cells of a range start, as dragging the fill
398
+ * handle does: `1, 2` goes on `3, 4`, `Кв1` to `Кв2`, `Jan` to `Feb`, a
399
+ * date by a day; anything else repeats. `"down"` fills each column,
400
+ * `"right"` each row; `"up"` and `"left"` run the series back from the
401
+ * last cells.
402
+ * @param {number} sheet
403
+ * @param {string} range
404
+ * @param {"down" | "right" | "up" | "left"} direction
405
+ */
406
+ fillSeries(sheet, range, direction) {
407
+ const ptr0 = passStringToWasm0(range, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
408
+ const len0 = WASM_VECTOR_LEN;
409
+ const ptr1 = passStringToWasm0(direction, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
410
+ const len1 = WASM_VECTOR_LEN;
411
+ const ret = wasm.book_fillSeries(this.__wbg_ptr, sheet, ptr0, len0, ptr1, len1);
412
+ if (ret[1]) {
413
+ throw takeFromExternrefTable0(ret[0]);
414
+ }
415
+ }
416
+ /**
417
+ * Freezes the first `rows` rows and `columns` columns, so they stay put
418
+ * while the rest scrolls. `freezePanes(0, 1, 0)` pins the header row;
419
+ * `freezePanes(0, 0, 0)` unfreezes.
420
+ * @param {number} sheet
421
+ * @param {number} rows
422
+ * @param {number} columns
423
+ */
424
+ freezePanes(sheet, rows, columns) {
425
+ const ret = wasm.book_freezePanes(this.__wbg_ptr, sheet, rows, columns);
426
+ if (ret[1]) {
427
+ throw takeFromExternrefTable0(ret[0]);
393
428
  }
394
- return Book.__wrap(ret[0]);
395
429
  }
396
430
  /**
397
431
  * The value at `address` on sheet `sheet`, as a JS number, string, boolean
@@ -410,9 +444,7 @@ class Book {
410
444
  return takeFromExternrefTable0(ret[0]);
411
445
  }
412
446
  /**
413
- * The same by 1-based row and column, the numbers `ROW()` and `COLUMN()`
414
- * return: `getAt(0, 4, 2)` is `B4`. Saves formatting an address only to
415
- * parse it again.
447
+ * The same by 1-based row and column.
416
448
  * @param {number} sheet
417
449
  * @param {number} row
418
450
  * @param {number} column
@@ -544,6 +576,40 @@ class Book {
544
576
  }
545
577
  return takeFromExternrefTable0(ret[0]);
546
578
  }
579
+ /**
580
+ * The styles of a rectangle in one call: each distinct style once, and a
581
+ * grid of indexes into them. `cellStyle` per cell costs a crossing and a
582
+ * whole style object per cell; a formatted table of ten thousand cells
583
+ * usually has a dozen styles.
584
+ * @param {number} sheet
585
+ * @param {string} range
586
+ * @returns {RangeStyles}
587
+ */
588
+ getRangeStyles(sheet, range) {
589
+ const ptr0 = passStringToWasm0(range, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
590
+ const len0 = WASM_VECTOR_LEN;
591
+ const ret = wasm.book_getRangeStyles(this.__wbg_ptr, sheet, ptr0, len0);
592
+ if (ret[2]) {
593
+ throw takeFromExternrefTable0(ret[1]);
594
+ }
595
+ return takeFromExternrefTable0(ret[0]);
596
+ }
597
+ /**
598
+ * The same by numbers, as `getRangeAt` takes them.
599
+ * @param {number} sheet
600
+ * @param {number} row
601
+ * @param {number} column
602
+ * @param {number} rows
603
+ * @param {number} columns
604
+ * @returns {RangeStyles}
605
+ */
606
+ getRangeStylesAt(sheet, row, column, rows, columns) {
607
+ const ret = wasm.book_getRangeStylesAt(this.__wbg_ptr, sheet, row, column, rows, columns);
608
+ if (ret[2]) {
609
+ throw takeFromExternrefTable0(ret[1]);
610
+ }
611
+ return takeFromExternrefTable0(ret[0]);
612
+ }
547
613
  /**
548
614
  * A whole row in one crossing of the boundary: values, displayed text,
549
615
  * bold and indent, plus whether the row is hidden.
@@ -611,35 +677,45 @@ class Book {
611
677
  /**
612
678
  * Inserts blank cells over a range, pushing what was there `"down"` or
613
679
  * `"right"` - Excel's "Insert Cells", which moves part of a row rather
614
- * than the whole of it.
680
+ * than the whole of it. `copyOrigin` works as in `insertRows`.
615
681
  * @param {number} sheet
616
682
  * @param {string} range
617
- * @param {string} shift
683
+ * @param {"down" | "right"} shift
684
+ * @param {"before" | "after" | "none"} [copyOrigin]
618
685
  */
619
- insertCells(sheet, range, shift) {
686
+ insertCells(sheet, range, shift, copyOrigin) {
620
687
  const ptr0 = passStringToWasm0(range, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
621
688
  const len0 = WASM_VECTOR_LEN;
622
689
  const ptr1 = passStringToWasm0(shift, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
623
690
  const len1 = WASM_VECTOR_LEN;
624
- const ret = wasm.book_insertCells(this.__wbg_ptr, sheet, ptr0, len0, ptr1, len1);
691
+ var ptr2 = isLikeNone(copyOrigin) ? 0 : passStringToWasm0(copyOrigin, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
692
+ var len2 = WASM_VECTOR_LEN;
693
+ const ret = wasm.book_insertCells(this.__wbg_ptr, sheet, ptr0, len0, ptr1, len1, ptr2, len2);
625
694
  if (ret[1]) {
626
695
  throw takeFromExternrefTable0(ret[0]);
627
696
  }
628
697
  }
629
698
  /**
630
699
  * Inserts `count` columns to the left of column `at`, 1-based.
700
+ * `copyOrigin` works as in `insertRows`, with left and right.
631
701
  * @param {number} sheet
632
702
  * @param {number} at
633
703
  * @param {number} count
704
+ * @param {"before" | "after" | "none"} [copyOrigin]
634
705
  */
635
- insertColumns(sheet, at, count) {
636
- const ret = wasm.book_insertColumns(this.__wbg_ptr, sheet, at, count);
706
+ insertColumns(sheet, at, count, copyOrigin) {
707
+ var ptr0 = isLikeNone(copyOrigin) ? 0 : passStringToWasm0(copyOrigin, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
708
+ var len0 = WASM_VECTOR_LEN;
709
+ const ret = wasm.book_insertColumns(this.__wbg_ptr, sheet, at, count, ptr0, len0);
637
710
  if (ret[1]) {
638
711
  throw takeFromExternrefTable0(ret[0]);
639
712
  }
640
713
  }
641
714
  /**
642
715
  * Inserts `count` rows above row `at`, moving everything below down.
716
+ * `copyOrigin` - `"before"` (the row above), `"after"` (the row below)
717
+ * or `"none"` - says whose formatting the new rows take. Left out, it is
718
+ * `"before"`, as in Excel.
643
719
  *
644
720
  * Formulas across the whole workbook follow the cells they read, and so
645
721
  * do merges, links, validations, tables and drawings. `at` is the number
@@ -647,9 +723,12 @@ class Book {
647
723
  * @param {number} sheet
648
724
  * @param {number} at
649
725
  * @param {number} count
726
+ * @param {"before" | "after" | "none"} [copyOrigin]
650
727
  */
651
- insertRows(sheet, at, count) {
652
- const ret = wasm.book_insertRows(this.__wbg_ptr, sheet, at, count);
728
+ insertRows(sheet, at, count, copyOrigin) {
729
+ var ptr0 = isLikeNone(copyOrigin) ? 0 : passStringToWasm0(copyOrigin, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
730
+ var len0 = WASM_VECTOR_LEN;
731
+ const ret = wasm.book_insertRows(this.__wbg_ptr, sheet, at, count, ptr0, len0);
653
732
  if (ret[1]) {
654
733
  throw takeFromExternrefTable0(ret[0]);
655
734
  }
@@ -970,7 +1049,7 @@ class Book {
970
1049
  * below (`"up"`) or from the right (`"left"`).
971
1050
  * @param {number} sheet
972
1051
  * @param {string} range
973
- * @param {string} shift
1052
+ * @param {"up" | "left"} shift
974
1053
  */
975
1054
  removeCells(sheet, range, shift) {
976
1055
  const ptr0 = passStringToWasm0(range, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
@@ -1133,14 +1212,14 @@ class Book {
1133
1212
  /**
1134
1213
  * Writes a value: a number, boolean, or string. A string starting with `=`
1135
1214
  * becomes a formula.
1136
- * @param {number} index
1215
+ * @param {number} sheet
1137
1216
  * @param {string} address
1138
1217
  * @param {CellValue} value
1139
1218
  */
1140
- set(index, address, value) {
1219
+ set(sheet, address, value) {
1141
1220
  const ptr0 = passStringToWasm0(address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
1142
1221
  const len0 = WASM_VECTOR_LEN;
1143
- const ret = wasm.book_set(this.__wbg_ptr, index, ptr0, len0, value);
1222
+ const ret = wasm.book_set(this.__wbg_ptr, sheet, ptr0, len0, value);
1144
1223
  if (ret[1]) {
1145
1224
  throw takeFromExternrefTable0(ret[0]);
1146
1225
  }
@@ -1345,6 +1424,37 @@ class Book {
1345
1424
  throw takeFromExternrefTable0(ret[0]);
1346
1425
  }
1347
1426
  }
1427
+ /**
1428
+ * Paints a rectangle cell by cell in one call, `at` its top-left corner:
1429
+ * `grid` points into `styles`, each a patch as `setCellStyle` takes it,
1430
+ * and `null` or `-1` leaves its cell alone. The output of
1431
+ * `getRangeStyles` goes back in unchanged, so a block's formatting can be
1432
+ * read, edited in JS and written back - or copied somewhere else.
1433
+ * @param {number} sheet
1434
+ * @param {string} at
1435
+ * @param {RangeStylesPatch} styles
1436
+ */
1437
+ setRangeStyles(sheet, at, styles) {
1438
+ const ptr0 = passStringToWasm0(at, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
1439
+ const len0 = WASM_VECTOR_LEN;
1440
+ const ret = wasm.book_setRangeStyles(this.__wbg_ptr, sheet, ptr0, len0, styles);
1441
+ if (ret[1]) {
1442
+ throw takeFromExternrefTable0(ret[0]);
1443
+ }
1444
+ }
1445
+ /**
1446
+ * The same with the corner given as 1-based row and column.
1447
+ * @param {number} sheet
1448
+ * @param {number} row
1449
+ * @param {number} column
1450
+ * @param {RangeStylesPatch} styles
1451
+ */
1452
+ setRangeStylesAt(sheet, row, column, styles) {
1453
+ const ret = wasm.book_setRangeStylesAt(this.__wbg_ptr, sheet, row, column, styles);
1454
+ if (ret[1]) {
1455
+ throw takeFromExternrefTable0(ret[0]);
1456
+ }
1457
+ }
1348
1458
  /**
1349
1459
  * Sets the height of a row in points, or gives it back to the sheet
1350
1460
  * default with `undefined`. `row` is the number a user sees.
@@ -1497,6 +1607,44 @@ class Book {
1497
1607
  wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
1498
1608
  }
1499
1609
  }
1610
+ /**
1611
+ * Sorts the rows of a range, or its columns. A key is a column number of
1612
+ * the sheet, 1-based (a row number when sorting columns), or the text of
1613
+ * a header when `options.header` says the first line is one; a minus
1614
+ * sorts it largest first: `[2, -4]`, `["Region", "-Amount"]`. The first
1615
+ * key decides first.
1616
+ *
1617
+ * ```js
1618
+ * book.sortRange(0, "A1:D100", ["Region", "-Amount"], { header: true });
1619
+ * ```
1620
+ * @param {number} sheet
1621
+ * @param {string} range
1622
+ * @param {SortKeys} keys
1623
+ * @param {SortRangeOptions} [options]
1624
+ */
1625
+ sortRange(sheet, range, keys, options) {
1626
+ const ptr0 = passStringToWasm0(range, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
1627
+ const len0 = WASM_VECTOR_LEN;
1628
+ const ret = wasm.book_sortRange(this.__wbg_ptr, sheet, ptr0, len0, keys, options);
1629
+ if (ret[1]) {
1630
+ throw takeFromExternrefTable0(ret[0]);
1631
+ }
1632
+ }
1633
+ /**
1634
+ * Sorts the data rows of a table, found by name anywhere in the book;
1635
+ * its header and totals rows stay put. Keys name its columns, as in
1636
+ * `sortRange`: `book.sortTable("Sales", ["-Amount"])`.
1637
+ * @param {string} name
1638
+ * @param {SortKeys} keys
1639
+ */
1640
+ sortTable(name, keys) {
1641
+ const ptr0 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
1642
+ const len0 = WASM_VECTOR_LEN;
1643
+ const ret = wasm.book_sortTable(this.__wbg_ptr, ptr0, len0, keys);
1644
+ if (ret[1]) {
1645
+ throw takeFromExternrefTable0(ret[0]);
1646
+ }
1647
+ }
1500
1648
  /**
1501
1649
  * The tables of a sheet: what a structured reference like `Sales[Amount]`
1502
1650
  * names.
@@ -1511,40 +1659,18 @@ class Book {
1511
1659
  return takeFromExternrefTable0(ret[0]);
1512
1660
  }
1513
1661
  /**
1514
- * One sheet as CSV, comma-separated.
1662
+ * One sheet as CSV, comma-separated unless `options.delimiter` says
1663
+ * otherwise - a semicolon for a locale where the comma is the decimal
1664
+ * mark.
1515
1665
  * @param {number} sheet
1666
+ * @param {CsvOptions} [options]
1516
1667
  * @returns {string}
1517
1668
  */
1518
- toCsv(sheet) {
1669
+ toCsv(sheet, options) {
1519
1670
  let deferred2_0;
1520
1671
  let deferred2_1;
1521
1672
  try {
1522
- const ret = wasm.book_toCsv(this.__wbg_ptr, sheet);
1523
- var ptr1 = ret[0];
1524
- var len1 = ret[1];
1525
- if (ret[3]) {
1526
- ptr1 = 0; len1 = 0;
1527
- throw takeFromExternrefTable0(ret[2]);
1528
- }
1529
- deferred2_0 = ptr1;
1530
- deferred2_1 = len1;
1531
- return getStringFromWasm0(ptr1, len1);
1532
- } finally {
1533
- wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
1534
- }
1535
- }
1536
- /**
1537
- * The same with the delimiter stated - a semicolon for a locale where the
1538
- * comma is the decimal mark.
1539
- * @param {number} sheet
1540
- * @param {CsvOptions} options
1541
- * @returns {string}
1542
- */
1543
- toCsvWith(sheet, options) {
1544
- let deferred2_0;
1545
- let deferred2_1;
1546
- try {
1547
- const ret = wasm.book_toCsvWith(this.__wbg_ptr, sheet, options);
1673
+ const ret = wasm.book_toCsv(this.__wbg_ptr, sheet, options);
1548
1674
  var ptr1 = ret[0];
1549
1675
  var len1 = ret[1];
1550
1676
  if (ret[3]) {
@@ -1599,8 +1725,9 @@ class Book {
1599
1725
  /**
1600
1726
  * The workbook as a BIFF8 `.xls` file.
1601
1727
  *
1602
- * The format cannot hold a formula as text, so every formula is written
1603
- * as its last computed value; call `recalculate` first if that matters.
1728
+ * Formulas are written as tokens with their cached result; what BIFF8
1729
+ * cannot hold - a structured reference, a row past 65536 - is written as
1730
+ * the value, so call `recalculate` first if the cache is stale.
1604
1731
  * @returns {Uint8Array}
1605
1732
  */
1606
1733
  toXls() {
@@ -1626,16 +1753,6 @@ class Book {
1626
1753
  wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
1627
1754
  return v1;
1628
1755
  }
1629
- /**
1630
- * Unfreezes the panes. The same as `freezePanes(sheet, 0, 0)`.
1631
- * @param {number} sheet
1632
- */
1633
- unfreezePanes(sheet) {
1634
- const ret = wasm.book_unfreezePanes(this.__wbg_ptr, sheet);
1635
- if (ret[1]) {
1636
- throw takeFromExternrefTable0(ret[0]);
1637
- }
1638
- }
1639
1756
  /**
1640
1757
  * Takes a merge back out. Answers whether there was one.
1641
1758
  * @param {number} sheet
@@ -1694,12 +1811,12 @@ class Book {
1694
1811
  return v1;
1695
1812
  }
1696
1813
  /**
1697
- * The used range of a sheet as `"A1:D20"`, taken from what the file says
1698
- * rather than from walking the cells; `undefined` when it says nothing.
1814
+ * The used range of a sheet as `"A1:D20"` without walking the rows;
1815
+ * `undefined` for an empty sheet.
1699
1816
  *
1700
- * A reader that only needs the shape of a sheet pays nothing for it;
1701
- * `usedRange` walks every cell and answers for a sheet built in memory
1702
- * too, where the file has nothing to say.
1817
+ * Rows are exact, columns an upper bound: removing a cell never narrows
1818
+ * them. `usedRange` answers the same until a cell in an edge column is
1819
+ * removed, and walks the rows after that to give the exact rectangle.
1703
1820
  * @param {number} sheet
1704
1821
  * @returns {string | undefined}
1705
1822
  */
Binary file
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rosperitus/excelerate",
3
3
  "description": "Read, write and recalculate spreadsheets: xlsx, xls, ods, csv, html, and a 518-function formula engine",
4
- "version": "0.12.0",
4
+ "version": "0.13.0",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",