@rosperitus/excelerate 0.12.3 → 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
@@ -74,11 +74,12 @@ type CellValue = number | string | boolean | null;
74
74
  type CellGrid = CellValue[][];
75
75
  type CopyOrigin = "before" | "after" | "none"; // whose formatting new rows take
76
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 };
77
79
 
78
80
  class Book {
79
81
  constructor();
80
- static fromXlsx(bytes: Uint8Array): Book;
81
- 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;
82
83
  static readCsv(bytes: Uint8Array, options: CsvOptions): Book; // shape stated, not guessed
83
84
 
84
85
  // Sheets
@@ -112,6 +113,8 @@ class Book {
112
113
  getFormattedAt(sheet: number, row: number, column: number): string;
113
114
  getRangeAt(sheet: number, row: number, column: number, rows: number, columns: number): CellGrid;
114
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;
115
118
  recalculateCellAt(sheet: number, row: number, column: number): boolean;
116
119
  recalculateFromAt(sheet: number, row: number, column: number): number;
117
120
 
@@ -165,7 +168,9 @@ class Book {
165
168
  imageData(sheet: number, index: number): Uint8Array;
166
169
  shapes(sheet: number): SheetShape[];
167
170
 
168
- // 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;
169
174
  setCellStyle(sheet: number, address: string, patch: CellStylePatch): void;
170
175
  setCellStyleAt(sheet: number, row: number, column: number, patch: CellStylePatch): void;
171
176
  setRangeStyle(sheet: number, range: string, patch: CellStylePatch): void;
@@ -180,8 +185,7 @@ class Book {
180
185
 
181
186
  // The saved view
182
187
  sheetView(sheet: number): SheetViewInfo;
183
- freezePanes(sheet: number, rows: number, columns: number): void; // (0, 1, 0) pins the header
184
- unfreezePanes(sheet: number): void;
188
+ freezePanes(sheet: number, rows: number, columns: number): void; // (0, 1, 0) pins the header, (0, 0, 0) unfreezes
185
189
  setZoom(sheet: number, percent?: number): void;
186
190
  setShowGridLines(sheet: number, show: boolean, headers?: boolean): void;
187
191
 
@@ -209,18 +213,22 @@ class Book {
209
213
 
210
214
  // Calculation
211
215
  evaluate(sheet: number, address: string, formula: string): CellValue;
212
- recalculate(sheet?: number | null): number;
216
+ recalculate(sheet?: number | null, onProgress?: (p: Progress) => void): number;
213
217
  recalculateCell(sheet: number, address: string): boolean;
214
218
  recalculateFrom(sheet: number, address: string): number;
215
219
  recalculateFromMany(sheet: number, addresses: string[]): number;
216
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
+
217
226
  // Output
218
- toXlsx(): Uint8Array;
227
+ toXlsx(onProgress?: (p: Progress) => void): Uint8Array;
219
228
  toOds(): Uint8Array;
220
229
  toXls(): Uint8Array;
221
230
  toHtml(sheet?: number | null, fragment?: boolean | null): string;
222
- toCsv(sheet: number): string;
223
- 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
224
232
 
225
233
  free(): void;
226
234
  }
@@ -273,6 +281,14 @@ missing sheet, a truncated package.
273
281
  - **Charts, pictures and shapes are read-only here.** `charts`, `images` and
274
282
  `shapes` describe what a sheet carries; the parts themselves travel through
275
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.
276
292
  - **Cached results.** A formula cell read from a file carries whatever the app
277
293
  that saved it computed. Call `recalculate()` before relying on those numbers.
278
294
 
package/excelerate.d.ts CHANGED
@@ -375,22 +375,17 @@ export class Book {
375
375
  fillSeries(sheet: number, range: string, direction: "down" | "right" | "up" | "left"): void;
376
376
  /**
377
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.
378
+ * while the rest scrolls. `freezePanes(0, 1, 0)` pins the header row;
379
+ * `freezePanes(0, 0, 0)` unfreezes.
379
380
  */
380
381
  freezePanes(sheet: number, rows: number, columns: number): void;
381
- /**
382
- * Reads an xlsx package from its bytes.
383
- */
384
- static fromXlsx(bytes: Uint8Array): Book;
385
382
  /**
386
383
  * The value at `address` on sheet `sheet`, as a JS number, string, boolean
387
384
  * or `null`. A formula cell gives its cached result, not its text.
388
385
  */
389
386
  get(sheet: number, address: string): CellValue;
390
387
  /**
391
- * The same by 1-based row and column, the numbers `ROW()` and `COLUMN()`
392
- * return: `getAt(0, 4, 2)` is `B4`. Saves formatting an address only to
393
- * parse it again.
388
+ * The same by 1-based row and column.
394
389
  */
395
390
  getAt(sheet: number, row: number, column: number): CellValue;
396
391
  /**
@@ -464,7 +459,7 @@ export class Book {
464
459
  * `"right"` - Excel's "Insert Cells", which moves part of a row rather
465
460
  * than the whole of it. `copyOrigin` works as in `insertRows`.
466
461
  */
467
- insertCells(sheet: number, range: string, shift: string, copyOrigin?: "before" | "after" | "none"): void;
462
+ insertCells(sheet: number, range: string, shift: "down" | "right", copyOrigin?: "before" | "after" | "none"): void;
468
463
  /**
469
464
  * Inserts `count` columns to the left of column `at`, 1-based.
470
465
  * `copyOrigin` works as in `insertRows`, with left and right.
@@ -621,7 +616,7 @@ export class Book {
621
616
  * Removes the cells of a range, pulling the rest back over the hole from
622
617
  * below (`"up"`) or from the right (`"left"`).
623
618
  */
624
- removeCells(sheet: number, range: string, shift: string): void;
619
+ removeCells(sheet: number, range: string, shift: "up" | "left"): void;
625
620
  /**
626
621
  * Removes `count` columns from column `at`, 1-based.
627
622
  */
@@ -675,7 +670,7 @@ export class Book {
675
670
  * Writes a value: a number, boolean, or string. A string starting with `=`
676
671
  * becomes a formula.
677
672
  */
678
- set(index: number, address: string, value: CellValue): void;
673
+ set(sheet: number, address: string, value: CellValue): void;
679
674
  /**
680
675
  * Sets that tab.
681
676
  */
@@ -843,14 +838,11 @@ export class Book {
843
838
  */
844
839
  tables(sheet: number): SheetTable[];
845
840
  /**
846
- * 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.
847
844
  */
848
- toCsv(sheet: number): string;
849
- /**
850
- * The same with the delimiter stated - a semicolon for a locale where the
851
- * comma is the decimal mark.
852
- */
853
- toCsvWith(sheet: number, options: CsvOptions): string;
845
+ toCsv(sheet: number, options?: CsvOptions): string;
854
846
  /**
855
847
  * The workbook as an HTML page, or one sheet of it. `fragment` leaves out
856
848
  * the `<html>` wrapper, for embedding in a page that already has one.
@@ -863,18 +855,15 @@ export class Book {
863
855
  /**
864
856
  * The workbook as a BIFF8 `.xls` file.
865
857
  *
866
- * The format cannot hold a formula as text, so every formula is written
867
- * 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.
868
861
  */
869
862
  toXls(): Uint8Array;
870
863
  /**
871
864
  * The workbook as an xlsx package.
872
865
  */
873
866
  toXlsx(on_progress?: Function | null): Uint8Array;
874
- /**
875
- * Unfreezes the panes. The same as `freezePanes(sheet, 0, 0)`.
876
- */
877
- unfreezePanes(sheet: number): void;
878
867
  /**
879
868
  * Takes a merge back out. Answers whether there was one.
880
869
  */
package/excelerate.js CHANGED
@@ -415,7 +415,8 @@ class Book {
415
415
  }
416
416
  /**
417
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.
418
+ * while the rest scrolls. `freezePanes(0, 1, 0)` pins the header row;
419
+ * `freezePanes(0, 0, 0)` unfreezes.
419
420
  * @param {number} sheet
420
421
  * @param {number} rows
421
422
  * @param {number} columns
@@ -426,20 +427,6 @@ class Book {
426
427
  throw takeFromExternrefTable0(ret[0]);
427
428
  }
428
429
  }
429
- /**
430
- * Reads an xlsx package from its bytes.
431
- * @param {Uint8Array} bytes
432
- * @returns {Book}
433
- */
434
- static fromXlsx(bytes) {
435
- const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc);
436
- const len0 = WASM_VECTOR_LEN;
437
- const ret = wasm.book_fromXlsx(ptr0, len0);
438
- if (ret[2]) {
439
- throw takeFromExternrefTable0(ret[1]);
440
- }
441
- return Book.__wrap(ret[0]);
442
- }
443
430
  /**
444
431
  * The value at `address` on sheet `sheet`, as a JS number, string, boolean
445
432
  * or `null`. A formula cell gives its cached result, not its text.
@@ -457,9 +444,7 @@ class Book {
457
444
  return takeFromExternrefTable0(ret[0]);
458
445
  }
459
446
  /**
460
- * The same by 1-based row and column, the numbers `ROW()` and `COLUMN()`
461
- * return: `getAt(0, 4, 2)` is `B4`. Saves formatting an address only to
462
- * parse it again.
447
+ * The same by 1-based row and column.
463
448
  * @param {number} sheet
464
449
  * @param {number} row
465
450
  * @param {number} column
@@ -695,7 +680,7 @@ class Book {
695
680
  * than the whole of it. `copyOrigin` works as in `insertRows`.
696
681
  * @param {number} sheet
697
682
  * @param {string} range
698
- * @param {string} shift
683
+ * @param {"down" | "right"} shift
699
684
  * @param {"before" | "after" | "none"} [copyOrigin]
700
685
  */
701
686
  insertCells(sheet, range, shift, copyOrigin) {
@@ -1064,7 +1049,7 @@ class Book {
1064
1049
  * below (`"up"`) or from the right (`"left"`).
1065
1050
  * @param {number} sheet
1066
1051
  * @param {string} range
1067
- * @param {string} shift
1052
+ * @param {"up" | "left"} shift
1068
1053
  */
1069
1054
  removeCells(sheet, range, shift) {
1070
1055
  const ptr0 = passStringToWasm0(range, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
@@ -1227,14 +1212,14 @@ class Book {
1227
1212
  /**
1228
1213
  * Writes a value: a number, boolean, or string. A string starting with `=`
1229
1214
  * becomes a formula.
1230
- * @param {number} index
1215
+ * @param {number} sheet
1231
1216
  * @param {string} address
1232
1217
  * @param {CellValue} value
1233
1218
  */
1234
- set(index, address, value) {
1219
+ set(sheet, address, value) {
1235
1220
  const ptr0 = passStringToWasm0(address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
1236
1221
  const len0 = WASM_VECTOR_LEN;
1237
- 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);
1238
1223
  if (ret[1]) {
1239
1224
  throw takeFromExternrefTable0(ret[0]);
1240
1225
  }
@@ -1674,40 +1659,18 @@ class Book {
1674
1659
  return takeFromExternrefTable0(ret[0]);
1675
1660
  }
1676
1661
  /**
1677
- * One sheet as CSV, comma-separated.
1678
- * @param {number} sheet
1679
- * @returns {string}
1680
- */
1681
- toCsv(sheet) {
1682
- let deferred2_0;
1683
- let deferred2_1;
1684
- try {
1685
- const ret = wasm.book_toCsv(this.__wbg_ptr, sheet);
1686
- var ptr1 = ret[0];
1687
- var len1 = ret[1];
1688
- if (ret[3]) {
1689
- ptr1 = 0; len1 = 0;
1690
- throw takeFromExternrefTable0(ret[2]);
1691
- }
1692
- deferred2_0 = ptr1;
1693
- deferred2_1 = len1;
1694
- return getStringFromWasm0(ptr1, len1);
1695
- } finally {
1696
- wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
1697
- }
1698
- }
1699
- /**
1700
- * The same with the delimiter stated - a semicolon for a locale where the
1701
- * comma is the decimal mark.
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.
1702
1665
  * @param {number} sheet
1703
- * @param {CsvOptions} options
1666
+ * @param {CsvOptions} [options]
1704
1667
  * @returns {string}
1705
1668
  */
1706
- toCsvWith(sheet, options) {
1669
+ toCsv(sheet, options) {
1707
1670
  let deferred2_0;
1708
1671
  let deferred2_1;
1709
1672
  try {
1710
- const ret = wasm.book_toCsvWith(this.__wbg_ptr, sheet, options);
1673
+ const ret = wasm.book_toCsv(this.__wbg_ptr, sheet, options);
1711
1674
  var ptr1 = ret[0];
1712
1675
  var len1 = ret[1];
1713
1676
  if (ret[3]) {
@@ -1762,8 +1725,9 @@ class Book {
1762
1725
  /**
1763
1726
  * The workbook as a BIFF8 `.xls` file.
1764
1727
  *
1765
- * The format cannot hold a formula as text, so every formula is written
1766
- * 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.
1767
1731
  * @returns {Uint8Array}
1768
1732
  */
1769
1733
  toXls() {
@@ -1789,16 +1753,6 @@ class Book {
1789
1753
  wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
1790
1754
  return v1;
1791
1755
  }
1792
- /**
1793
- * Unfreezes the panes. The same as `freezePanes(sheet, 0, 0)`.
1794
- * @param {number} sheet
1795
- */
1796
- unfreezePanes(sheet) {
1797
- const ret = wasm.book_unfreezePanes(this.__wbg_ptr, sheet);
1798
- if (ret[1]) {
1799
- throw takeFromExternrefTable0(ret[0]);
1800
- }
1801
- }
1802
1756
  /**
1803
1757
  * Takes a merge back out. Answers whether there was one.
1804
1758
  * @param {number} sheet
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.3",
4
+ "version": "0.13.0",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",