@rosperitus/excelerate 0.12.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,6 +72,8 @@ 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();
@@ -69,7 +91,7 @@ class Book {
69
91
  removeSheet(sheet: number): void; // references to it become #REF!
70
92
  cellCount(sheet?: number | null): number;
71
93
  usedRange(sheet: number): string | undefined; // "A1:D9", by walking the cells
72
- usedRangeHint(sheet: number): string | undefined; // the same, as the file states it
94
+ usedRangeHint(sheet: number): string | undefined; // the same, never walking; columns may be wider after a removal
73
95
 
74
96
  // Cells, by address
75
97
  get(sheet: number, address: string): CellValue;
@@ -79,6 +101,8 @@ class Book {
79
101
  getFormatted(sheet: number, address: string): string; // through the number format
80
102
  getRange(sheet: number, range: string): CellGrid; // one call, not one per cell
81
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;
82
106
 
83
107
  // Cells, by 1-based row and column - no address to build and re-parse
84
108
  getAt(sheet: number, row: number, column: number): CellValue;
@@ -115,16 +139,23 @@ class Book {
115
139
 
116
140
  // The grid itself: formulas everywhere in the workbook follow the cells they
117
141
  // read, and so do merges, links, validations, tables and drawings
118
- insertRows(sheet: number, at: number, count: number): void;
142
+ insertRows(sheet: number, at: number, count: number, copyOrigin?: CopyOrigin): void;
119
143
  removeRows(sheet: number, at: number, count: number): void;
120
- insertColumns(sheet: number, at: number, count: number): void; // 1-based
144
+ insertColumns(sheet: number, at: number, count: number, copyOrigin?: CopyOrigin): void; // 1-based
121
145
  removeColumns(sheet: number, at: number, count: number): void;
122
146
  copyRange(sheet: number, range: string, to: string, toSheet?: number): void; // formulas rewritten
123
147
  moveRange(sheet: number, range: string, to: string, toSheet?: number): void; // formulas kept, references follow
124
- insertCells(sheet: number, range: string, shift: "down" | "right"): void;
148
+ insertCells(sheet: number, range: string, shift: "down" | "right", copyOrigin?: CopyOrigin): void;
125
149
  removeCells(sheet: number, range: string, shift: "up" | "left"): void;
126
150
  moveSheet(from: number, to: number): void;
127
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
+
128
159
  // What is on a sheet besides cells
129
160
  comments(sheet: number): SheetComment[];
130
161
  hyperlinks(sheet: number): SheetHyperlink[];
@@ -228,8 +259,20 @@ missing sheet, a truncated package.
228
259
  carries, as if they had been written where they land; `moveRange` keeps them
229
260
  pointing at the same cells and rewrites the formulas elsewhere that read the
230
261
  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.
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.
233
276
  - **Cached results.** A formula cell read from a file carries whatever the app
234
277
  that saved it computed. Call `recalculate()` before relying on those numbers.
235
278
 
@@ -247,8 +290,9 @@ missing sheet, a truncated package.
247
290
  SYLK, Gnumeric and SpreadsheetML are read-only in the Rust crate too - there is
248
291
  nothing to bind.
249
292
 
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.
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.
252
296
 
253
297
  ## Development
254
298
 
@@ -262,14 +306,17 @@ npm start # example.js: build a book, calculate, round-trip
262
306
  npm run ts # typescript/basic.ts, no build step
263
307
  npm run typecheck # tsc --strict over the TypeScript examples
264
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=...
265
310
  ```
266
311
 
267
312
  | Path | What it is |
268
313
  |---|---|
269
- | `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) |
270
316
  | `example.js`, `test.js`, `bench.js` | JavaScript examples, tests, benchmark |
271
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 |
272
- | `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) |
273
320
 
274
321
  ## License
275
322
 
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
 
@@ -327,6 +356,23 @@ export class Book {
327
356
  * them. A path is `undefined` where the file does not name one.
328
357
  */
329
358
  externalBooks(): any[];
359
+ /**
360
+ * Copies the first row of a range into the rows below it, as Ctrl+D.
361
+ */
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;
368
+ /**
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.
374
+ */
375
+ fillSeries(sheet: number, range: string, direction: "down" | "right" | "up" | "left"): void;
330
376
  /**
331
377
  * Freezes the first `rows` rows and `columns` columns, so they stay put
332
378
  * while the rest scrolls. `freezePanes(0, 1, 0)` pins the header row.
@@ -375,6 +421,17 @@ export class Book {
375
421
  * to take. `getRangeAt(0, 1, 1, 3, 2)` is `A1:B3`.
376
422
  */
377
423
  getRangeAt(sheet: number, row: number, column: number, rows: number, columns: number): CellGrid;
424
+ /**
425
+ * The styles of a rectangle in one call: each distinct style once, and a
426
+ * grid of indexes into them. `cellStyle` per cell costs a crossing and a
427
+ * whole style object per cell; a formatted table of ten thousand cells
428
+ * usually has a dozen styles.
429
+ */
430
+ getRangeStyles(sheet: number, range: string): RangeStyles;
431
+ /**
432
+ * The same by numbers, as `getRangeAt` takes them.
433
+ */
434
+ getRangeStylesAt(sheet: number, row: number, column: number, rows: number, columns: number): RangeStyles;
378
435
  /**
379
436
  * A whole row in one crossing of the boundary: values, displayed text,
380
437
  * bold and indent, plus whether the row is hidden.
@@ -405,21 +462,25 @@ export class Book {
405
462
  /**
406
463
  * Inserts blank cells over a range, pushing what was there `"down"` or
407
464
  * `"right"` - Excel's "Insert Cells", which moves part of a row rather
408
- * than the whole of it.
465
+ * than the whole of it. `copyOrigin` works as in `insertRows`.
409
466
  */
410
- insertCells(sheet: number, range: string, shift: string): void;
467
+ insertCells(sheet: number, range: string, shift: string, copyOrigin?: "before" | "after" | "none"): void;
411
468
  /**
412
469
  * Inserts `count` columns to the left of column `at`, 1-based.
470
+ * `copyOrigin` works as in `insertRows`, with left and right.
413
471
  */
414
- insertColumns(sheet: number, at: number, count: number): void;
472
+ insertColumns(sheet: number, at: number, count: number, copyOrigin?: "before" | "after" | "none"): void;
415
473
  /**
416
474
  * Inserts `count` rows above row `at`, moving everything below down.
475
+ * `copyOrigin` - `"before"` (the row above), `"after"` (the row below)
476
+ * or `"none"` - says whose formatting the new rows take. Left out, it is
477
+ * `"before"`, as in Excel.
417
478
  *
418
479
  * Formulas across the whole workbook follow the cells they read, and so
419
480
  * do merges, links, validations, tables and drawings. `at` is the number
420
481
  * a user sees.
421
482
  */
422
- insertRows(sheet: number, at: number, count: number): void;
483
+ insertRows(sheet: number, at: number, count: number, copyOrigin?: "before" | "after" | "none"): void;
423
484
  /**
424
485
  * Merges a block of cells: `merge(0, "A1:C1")`.
425
486
  */
@@ -690,6 +751,18 @@ export class Book {
690
751
  * created empty, which is what carries the style.
691
752
  */
692
753
  setRangeStyle(sheet: number, range: string, patch: CellStylePatch): void;
754
+ /**
755
+ * Paints a rectangle cell by cell in one call, `at` its top-left corner:
756
+ * `grid` points into `styles`, each a patch as `setCellStyle` takes it,
757
+ * and `null` or `-1` leaves its cell alone. The output of
758
+ * `getRangeStyles` goes back in unchanged, so a block's formatting can be
759
+ * read, edited in JS and written back - or copied somewhere else.
760
+ */
761
+ setRangeStyles(sheet: number, at: string, styles: RangeStylesPatch): void;
762
+ /**
763
+ * The same with the corner given as 1-based row and column.
764
+ */
765
+ setRangeStylesAt(sheet: number, row: number, column: number, styles: RangeStylesPatch): void;
693
766
  /**
694
767
  * Sets the height of a row in points, or gives it back to the sheet
695
768
  * default with `undefined`. `row` is the number a user sees.
@@ -746,6 +819,24 @@ export class Book {
746
819
  * what the index of every other call counts.
747
820
  */
748
821
  sheetVisibility(sheet: number): string;
822
+ /**
823
+ * Sorts the rows of a range, or its columns. A key is a column number of
824
+ * the sheet, 1-based (a row number when sorting columns), or the text of
825
+ * a header when `options.header` says the first line is one; a minus
826
+ * sorts it largest first: `[2, -4]`, `["Region", "-Amount"]`. The first
827
+ * key decides first.
828
+ *
829
+ * ```js
830
+ * book.sortRange(0, "A1:D100", ["Region", "-Amount"], { header: true });
831
+ * ```
832
+ */
833
+ sortRange(sheet: number, range: string, keys: SortKeys, options?: SortRangeOptions): void;
834
+ /**
835
+ * Sorts the data rows of a table, found by name anywhere in the book;
836
+ * its header and totals rows stay put. Keys name its columns, as in
837
+ * `sortRange`: `book.sortTable("Sales", ["-Amount"])`.
838
+ */
839
+ sortTable(name: string, keys: SortKeys): void;
749
840
  /**
750
841
  * The tables of a sheet: what a structured reference like `Sales[Amount]`
751
842
  * names.
@@ -806,12 +897,12 @@ export class Book {
806
897
  */
807
898
  usedRange(sheet: number): string | undefined;
808
899
  /**
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.
900
+ * The used range of a sheet as `"A1:D20"` without walking the rows;
901
+ * `undefined` for an empty sheet.
811
902
  *
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.
903
+ * Rows are exact, columns an upper bound: removing a cell never narrows
904
+ * them. `usedRange` answers the same until a cell in an edge column is
905
+ * removed, and walks the rows after that to give the exact rectangle.
815
906
  */
816
907
  usedRangeHint(sheet: number): string | undefined;
817
908
  /**
package/excelerate.js CHANGED
@@ -366,6 +366,53 @@ class Book {
366
366
  wasm.__wbindgen_free(ret[0], ret[1] * 4, 4);
367
367
  return v1;
368
368
  }
369
+ /**
370
+ * Copies the first row of a range into the rows below it, as Ctrl+D.
371
+ * @param {number} sheet
372
+ * @param {string} range
373
+ */
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
+ if (ret[1]) {
379
+ throw takeFromExternrefTable0(ret[0]);
380
+ }
381
+ }
382
+ /**
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
387
+ */
388
+ fillRight(sheet, range) {
389
+ const ptr0 = passStringToWasm0(range, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
390
+ const len0 = WASM_VECTOR_LEN;
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
+ }
369
416
  /**
370
417
  * Freezes the first `rows` rows and `columns` columns, so they stay put
371
418
  * while the rest scrolls. `freezePanes(0, 1, 0)` pins the header row.
@@ -544,6 +591,40 @@ class Book {
544
591
  }
545
592
  return takeFromExternrefTable0(ret[0]);
546
593
  }
594
+ /**
595
+ * The styles of a rectangle in one call: each distinct style once, and a
596
+ * grid of indexes into them. `cellStyle` per cell costs a crossing and a
597
+ * whole style object per cell; a formatted table of ten thousand cells
598
+ * usually has a dozen styles.
599
+ * @param {number} sheet
600
+ * @param {string} range
601
+ * @returns {RangeStyles}
602
+ */
603
+ getRangeStyles(sheet, range) {
604
+ const ptr0 = passStringToWasm0(range, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
605
+ const len0 = WASM_VECTOR_LEN;
606
+ const ret = wasm.book_getRangeStyles(this.__wbg_ptr, sheet, ptr0, len0);
607
+ if (ret[2]) {
608
+ throw takeFromExternrefTable0(ret[1]);
609
+ }
610
+ return takeFromExternrefTable0(ret[0]);
611
+ }
612
+ /**
613
+ * The same by numbers, as `getRangeAt` takes them.
614
+ * @param {number} sheet
615
+ * @param {number} row
616
+ * @param {number} column
617
+ * @param {number} rows
618
+ * @param {number} columns
619
+ * @returns {RangeStyles}
620
+ */
621
+ getRangeStylesAt(sheet, row, column, rows, columns) {
622
+ const ret = wasm.book_getRangeStylesAt(this.__wbg_ptr, sheet, row, column, rows, columns);
623
+ if (ret[2]) {
624
+ throw takeFromExternrefTable0(ret[1]);
625
+ }
626
+ return takeFromExternrefTable0(ret[0]);
627
+ }
547
628
  /**
548
629
  * A whole row in one crossing of the boundary: values, displayed text,
549
630
  * bold and indent, plus whether the row is hidden.
@@ -611,35 +692,45 @@ class Book {
611
692
  /**
612
693
  * Inserts blank cells over a range, pushing what was there `"down"` or
613
694
  * `"right"` - Excel's "Insert Cells", which moves part of a row rather
614
- * than the whole of it.
695
+ * than the whole of it. `copyOrigin` works as in `insertRows`.
615
696
  * @param {number} sheet
616
697
  * @param {string} range
617
698
  * @param {string} shift
699
+ * @param {"before" | "after" | "none"} [copyOrigin]
618
700
  */
619
- insertCells(sheet, range, shift) {
701
+ insertCells(sheet, range, shift, copyOrigin) {
620
702
  const ptr0 = passStringToWasm0(range, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
621
703
  const len0 = WASM_VECTOR_LEN;
622
704
  const ptr1 = passStringToWasm0(shift, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
623
705
  const len1 = WASM_VECTOR_LEN;
624
- const ret = wasm.book_insertCells(this.__wbg_ptr, sheet, ptr0, len0, ptr1, len1);
706
+ var ptr2 = isLikeNone(copyOrigin) ? 0 : passStringToWasm0(copyOrigin, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
707
+ var len2 = WASM_VECTOR_LEN;
708
+ const ret = wasm.book_insertCells(this.__wbg_ptr, sheet, ptr0, len0, ptr1, len1, ptr2, len2);
625
709
  if (ret[1]) {
626
710
  throw takeFromExternrefTable0(ret[0]);
627
711
  }
628
712
  }
629
713
  /**
630
714
  * Inserts `count` columns to the left of column `at`, 1-based.
715
+ * `copyOrigin` works as in `insertRows`, with left and right.
631
716
  * @param {number} sheet
632
717
  * @param {number} at
633
718
  * @param {number} count
719
+ * @param {"before" | "after" | "none"} [copyOrigin]
634
720
  */
635
- insertColumns(sheet, at, count) {
636
- const ret = wasm.book_insertColumns(this.__wbg_ptr, sheet, at, count);
721
+ insertColumns(sheet, at, count, copyOrigin) {
722
+ var ptr0 = isLikeNone(copyOrigin) ? 0 : passStringToWasm0(copyOrigin, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
723
+ var len0 = WASM_VECTOR_LEN;
724
+ const ret = wasm.book_insertColumns(this.__wbg_ptr, sheet, at, count, ptr0, len0);
637
725
  if (ret[1]) {
638
726
  throw takeFromExternrefTable0(ret[0]);
639
727
  }
640
728
  }
641
729
  /**
642
730
  * Inserts `count` rows above row `at`, moving everything below down.
731
+ * `copyOrigin` - `"before"` (the row above), `"after"` (the row below)
732
+ * or `"none"` - says whose formatting the new rows take. Left out, it is
733
+ * `"before"`, as in Excel.
643
734
  *
644
735
  * Formulas across the whole workbook follow the cells they read, and so
645
736
  * do merges, links, validations, tables and drawings. `at` is the number
@@ -647,9 +738,12 @@ class Book {
647
738
  * @param {number} sheet
648
739
  * @param {number} at
649
740
  * @param {number} count
741
+ * @param {"before" | "after" | "none"} [copyOrigin]
650
742
  */
651
- insertRows(sheet, at, count) {
652
- const ret = wasm.book_insertRows(this.__wbg_ptr, sheet, at, count);
743
+ insertRows(sheet, at, count, copyOrigin) {
744
+ var ptr0 = isLikeNone(copyOrigin) ? 0 : passStringToWasm0(copyOrigin, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
745
+ var len0 = WASM_VECTOR_LEN;
746
+ const ret = wasm.book_insertRows(this.__wbg_ptr, sheet, at, count, ptr0, len0);
653
747
  if (ret[1]) {
654
748
  throw takeFromExternrefTable0(ret[0]);
655
749
  }
@@ -1345,6 +1439,37 @@ class Book {
1345
1439
  throw takeFromExternrefTable0(ret[0]);
1346
1440
  }
1347
1441
  }
1442
+ /**
1443
+ * Paints a rectangle cell by cell in one call, `at` its top-left corner:
1444
+ * `grid` points into `styles`, each a patch as `setCellStyle` takes it,
1445
+ * and `null` or `-1` leaves its cell alone. The output of
1446
+ * `getRangeStyles` goes back in unchanged, so a block's formatting can be
1447
+ * read, edited in JS and written back - or copied somewhere else.
1448
+ * @param {number} sheet
1449
+ * @param {string} at
1450
+ * @param {RangeStylesPatch} styles
1451
+ */
1452
+ setRangeStyles(sheet, at, styles) {
1453
+ const ptr0 = passStringToWasm0(at, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
1454
+ const len0 = WASM_VECTOR_LEN;
1455
+ const ret = wasm.book_setRangeStyles(this.__wbg_ptr, sheet, ptr0, len0, styles);
1456
+ if (ret[1]) {
1457
+ throw takeFromExternrefTable0(ret[0]);
1458
+ }
1459
+ }
1460
+ /**
1461
+ * The same with the corner given as 1-based row and column.
1462
+ * @param {number} sheet
1463
+ * @param {number} row
1464
+ * @param {number} column
1465
+ * @param {RangeStylesPatch} styles
1466
+ */
1467
+ setRangeStylesAt(sheet, row, column, styles) {
1468
+ const ret = wasm.book_setRangeStylesAt(this.__wbg_ptr, sheet, row, column, styles);
1469
+ if (ret[1]) {
1470
+ throw takeFromExternrefTable0(ret[0]);
1471
+ }
1472
+ }
1348
1473
  /**
1349
1474
  * Sets the height of a row in points, or gives it back to the sheet
1350
1475
  * default with `undefined`. `row` is the number a user sees.
@@ -1497,6 +1622,44 @@ class Book {
1497
1622
  wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
1498
1623
  }
1499
1624
  }
1625
+ /**
1626
+ * Sorts the rows of a range, or its columns. A key is a column number of
1627
+ * the sheet, 1-based (a row number when sorting columns), or the text of
1628
+ * a header when `options.header` says the first line is one; a minus
1629
+ * sorts it largest first: `[2, -4]`, `["Region", "-Amount"]`. The first
1630
+ * key decides first.
1631
+ *
1632
+ * ```js
1633
+ * book.sortRange(0, "A1:D100", ["Region", "-Amount"], { header: true });
1634
+ * ```
1635
+ * @param {number} sheet
1636
+ * @param {string} range
1637
+ * @param {SortKeys} keys
1638
+ * @param {SortRangeOptions} [options]
1639
+ */
1640
+ sortRange(sheet, range, keys, options) {
1641
+ const ptr0 = passStringToWasm0(range, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
1642
+ const len0 = WASM_VECTOR_LEN;
1643
+ const ret = wasm.book_sortRange(this.__wbg_ptr, sheet, ptr0, len0, keys, options);
1644
+ if (ret[1]) {
1645
+ throw takeFromExternrefTable0(ret[0]);
1646
+ }
1647
+ }
1648
+ /**
1649
+ * Sorts the data rows of a table, found by name anywhere in the book;
1650
+ * its header and totals rows stay put. Keys name its columns, as in
1651
+ * `sortRange`: `book.sortTable("Sales", ["-Amount"])`.
1652
+ * @param {string} name
1653
+ * @param {SortKeys} keys
1654
+ */
1655
+ sortTable(name, keys) {
1656
+ const ptr0 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
1657
+ const len0 = WASM_VECTOR_LEN;
1658
+ const ret = wasm.book_sortTable(this.__wbg_ptr, ptr0, len0, keys);
1659
+ if (ret[1]) {
1660
+ throw takeFromExternrefTable0(ret[0]);
1661
+ }
1662
+ }
1500
1663
  /**
1501
1664
  * The tables of a sheet: what a structured reference like `Sales[Amount]`
1502
1665
  * names.
@@ -1694,12 +1857,12 @@ class Book {
1694
1857
  return v1;
1695
1858
  }
1696
1859
  /**
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.
1860
+ * The used range of a sheet as `"A1:D20"` without walking the rows;
1861
+ * `undefined` for an empty sheet.
1699
1862
  *
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.
1863
+ * Rows are exact, columns an upper bound: removing a cell never narrows
1864
+ * them. `usedRange` answers the same until a cell in an edge column is
1865
+ * removed, and walks the rows after that to give the exact rectangle.
1703
1866
  * @param {number} sheet
1704
1867
  * @returns {string | undefined}
1705
1868
  */
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.12.3",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",