@cj-tech-master/excelts 1.4.5-canary.20251212053535.13d32d8 → 1.4.5-canary.20251212143538.45665af

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.
@@ -12,19 +12,29 @@ class Row {
12
12
  this.style = {};
13
13
  this.outlineLevel = 0;
14
14
  }
15
- // return the row number
15
+ /**
16
+ * The row number
17
+ */
16
18
  get number() {
17
19
  return this._number;
18
20
  }
21
+ /**
22
+ * The worksheet that contains this row
23
+ */
19
24
  get worksheet() {
20
25
  return this._worksheet;
21
26
  }
22
- // Inform Streaming Writer that this row (and all rows before it) are complete
23
- // and ready to write. Has no effect on Worksheet document
27
+ /**
28
+ * Commit a completed row to stream.
29
+ * Inform Streaming Writer that this row (and all rows before it) are complete
30
+ * and ready to write. Has no effect on Worksheet document.
31
+ */
24
32
  commit() {
25
33
  this._worksheet._commitRow(this);
26
34
  }
27
- // helps GC by breaking cyclic references
35
+ /**
36
+ * Helps GC by breaking cyclic references
37
+ */
28
38
  destroy() {
29
39
  delete this._worksheet;
30
40
  delete this._cells;
@@ -43,7 +53,9 @@ class Row {
43
53
  }
44
54
  return cell;
45
55
  }
46
- // get cell by key, letter or column number
56
+ /**
57
+ * Get cell by number, column letter or column key
58
+ */
47
59
  getCell(col) {
48
60
  let colNum;
49
61
  if (typeof col === "string") {
@@ -66,7 +78,11 @@ class Row {
66
78
  col: colNum
67
79
  }));
68
80
  }
69
- // remove cell(s) and shift all higher cells down by count
81
+ /**
82
+ * Cut one or more cells (cells to the right are shifted left)
83
+ *
84
+ * Note: this operation will not affect other rows
85
+ */
70
86
  splice(start, count, ...inserts) {
71
87
  const nKeep = start + count;
72
88
  const nExpand = inserts.length - count;
@@ -115,26 +131,26 @@ class Row {
115
131
  cDst.comment = undefined;
116
132
  }
117
133
  }
118
- eachCell(optionsOrIteratee, maybeIteratee) {
134
+ eachCell(optOrCallback, maybeCallback) {
119
135
  let options = null;
120
- let iteratee;
121
- if (typeof optionsOrIteratee === "function") {
122
- iteratee = optionsOrIteratee;
136
+ let callback;
137
+ if (typeof optOrCallback === "function") {
138
+ callback = optOrCallback;
123
139
  }
124
140
  else {
125
- options = optionsOrIteratee;
126
- iteratee = maybeIteratee;
141
+ options = optOrCallback;
142
+ callback = maybeCallback;
127
143
  }
128
144
  if (options && options.includeEmpty) {
129
145
  const n = this._cells.length;
130
146
  for (let i = 1; i <= n; i++) {
131
- iteratee(this.getCell(i), i);
147
+ callback(this.getCell(i), i);
132
148
  }
133
149
  }
134
150
  else {
135
151
  this._cells.forEach((cell, index) => {
136
152
  if (cell && cell.type !== enums_js_1.Enums.ValueType.Null) {
137
- iteratee(cell, index + 1);
153
+ callback(cell, index + 1);
138
154
  }
139
155
  });
140
156
  }
@@ -155,7 +171,9 @@ class Row {
155
171
  }
156
172
  ws.rowBreaks.push(pb);
157
173
  }
158
- // return a sparse array of cell values
174
+ /**
175
+ * Get a row as a sparse array
176
+ */
159
177
  get values() {
160
178
  const values = [];
161
179
  this._cells.forEach(cell => {
@@ -165,7 +183,9 @@ class Row {
165
183
  });
166
184
  return values;
167
185
  }
168
- // set the values by contiguous or sparse array, or by key'd object literal
186
+ /**
187
+ * Set the values by contiguous or sparse array, or by key'd object literal
188
+ */
169
189
  set values(value) {
170
190
  // this operation is not additive - any prior cells are removed
171
191
  this._cells = [];
@@ -201,13 +221,21 @@ class Row {
201
221
  });
202
222
  }
203
223
  }
204
- // returns true if the row includes at least one cell with a value
224
+ /**
225
+ * Returns true if the row includes at least one cell with a value
226
+ */
205
227
  get hasValues() {
206
228
  return this._cells.some(cell => cell && cell.type !== enums_js_1.Enums.ValueType.Null);
207
229
  }
230
+ /**
231
+ * Number of cells including empty ones
232
+ */
208
233
  get cellCount() {
209
234
  return this._cells.length;
210
235
  }
236
+ /**
237
+ * Number of non-empty cells
238
+ */
211
239
  get actualCellCount() {
212
240
  let count = 0;
213
241
  this.eachCell(() => {
@@ -215,7 +243,9 @@ class Row {
215
243
  });
216
244
  return count;
217
245
  }
218
- // get the min and max column number for the non-null cells in this row or null
246
+ /**
247
+ * Get the min and max column number for the non-null cells in this row or null
248
+ */
219
249
  get dimensions() {
220
250
  let min = 0;
221
251
  let max = 0;
@@ -29,12 +29,18 @@ class Workbook {
29
29
  this.pivotTables = [];
30
30
  this._definedNames = new defined_names_js_1.DefinedNames();
31
31
  }
32
+ /**
33
+ * xlsx file format operations
34
+ */
32
35
  get xlsx() {
33
36
  if (!this._xlsx) {
34
37
  this._xlsx = new xlsx_js_1.XLSX(this);
35
38
  }
36
39
  return this._xlsx;
37
40
  }
41
+ /**
42
+ * csv file format operations
43
+ */
38
44
  get csv() {
39
45
  if (!this._csv) {
40
46
  this._csv = new csv_js_1.CSV(this);
@@ -50,6 +56,9 @@ class Workbook {
50
56
  }
51
57
  return this._worksheets.length || 1;
52
58
  }
59
+ /**
60
+ * Add a new worksheet and return a reference to it
61
+ */
53
62
  addWorksheet(name, options) {
54
63
  const id = this.nextId;
55
64
  const lastOrderNo = this._worksheets.reduce((acc, ws) => ((ws && ws.orderNo) > acc ? ws.orderNo : acc), 0);
@@ -73,6 +82,9 @@ class Workbook {
73
82
  worksheet.destroy();
74
83
  }
75
84
  }
85
+ /**
86
+ * Fetch sheet by name or id
87
+ */
76
88
  getWorksheet(id) {
77
89
  if (id === undefined) {
78
90
  return this._worksheets.find(Boolean);
@@ -85,6 +97,9 @@ class Workbook {
85
97
  }
86
98
  return undefined;
87
99
  }
100
+ /**
101
+ * Return a clone of worksheets in order
102
+ */
88
103
  get worksheets() {
89
104
  // return a clone of _worksheets
90
105
  return this._worksheets
@@ -92,9 +107,14 @@ class Workbook {
92
107
  .sort((a, b) => a.orderNo - b.orderNo)
93
108
  .filter(Boolean);
94
109
  }
95
- eachSheet(iteratee) {
110
+ /**
111
+ * Iterate over all sheets.
112
+ *
113
+ * Note: `workbook.worksheets.forEach` will still work but this is better.
114
+ */
115
+ eachSheet(callback) {
96
116
  this.worksheets.forEach(sheet => {
97
- iteratee(sheet, sheet.id);
117
+ callback(sheet, sheet.id);
98
118
  });
99
119
  }
100
120
  get definedNames() {
@@ -104,6 +124,9 @@ class Workbook {
104
124
  // Note: themes are not an exposed feature, meddle at your peril!
105
125
  this._themes = undefined;
106
126
  }
127
+ /**
128
+ * Add Image to Workbook and return the id
129
+ */
107
130
  addImage(image) {
108
131
  // TODO: validation?
109
132
  const id = this.media.length;
@@ -133,14 +133,21 @@ class Worksheet {
133
133
  }
134
134
  this._name = name;
135
135
  }
136
+ /**
137
+ * The workbook that contains this worksheet
138
+ */
136
139
  get workbook() {
137
140
  return this._workbook;
138
141
  }
139
- // when you're done with this worksheet, call this to remove from workbook
142
+ /**
143
+ * When you're done with this worksheet, call this to remove from workbook
144
+ */
140
145
  destroy() {
141
146
  this._workbook.removeWorksheetEx(this);
142
147
  }
143
- // Get the bounding range of the cells in this worksheet
148
+ /**
149
+ * Get the bounding range of the cells in this worksheet
150
+ */
144
151
  get dimensions() {
145
152
  const dimensions = new range_js_1.Range();
146
153
  this._rows.forEach(row => {
@@ -155,12 +162,18 @@ class Worksheet {
155
162
  }
156
163
  // =========================================================================
157
164
  // Columns
158
- // get the current columns array.
165
+ /**
166
+ * Get the current columns array
167
+ */
159
168
  get columns() {
160
169
  return this._columns;
161
170
  }
162
- // set the columns from an array of column definitions.
163
- // Note: any headers defined will overwrite existing values.
171
+ /**
172
+ * Add column headers and define column keys and widths.
173
+ *
174
+ * Note: these column structures are a workbook-building convenience only,
175
+ * apart from the column width, they will not be fully persisted.
176
+ */
164
177
  set columns(value) {
165
178
  // calculate max header row count
166
179
  this._headerRowCount = value.reduce((pv, cv) => {
@@ -188,7 +201,9 @@ class Worksheet {
188
201
  eachColumnKey(f) {
189
202
  Object.keys(this._keys).forEach(key => f(this._keys[key], key));
190
203
  }
191
- // get a single column by col number. If it doesn't exist, create it and any gaps before it
204
+ /**
205
+ * Access an individual column by key, letter and 1-based column number
206
+ */
192
207
  getColumn(c) {
193
208
  let colNum;
194
209
  if (typeof c === "string") {
@@ -214,6 +229,17 @@ class Worksheet {
214
229
  }
215
230
  return this._columns[colNum - 1];
216
231
  }
232
+ /**
233
+ * Cut one or more columns (columns to the right are shifted left)
234
+ * and optionally insert more
235
+ *
236
+ * If column properties have been defined, they will be cut or moved accordingly
237
+ *
238
+ * Known Issue: If a splice causes any merged cells to move, the results may be unpredictable
239
+ *
240
+ * Also: If the worksheet has more rows than values in the column inserts,
241
+ * the rows will still be shifted as if the values existed
242
+ */
217
243
  spliceColumns(start, count, ...inserts) {
218
244
  const rows = this._rows;
219
245
  const nRows = rows.length;
@@ -253,9 +279,15 @@ class Worksheet {
253
279
  // account for defined names
254
280
  this.workbook.definedNames.spliceColumns(this.name, start, count, inserts.length);
255
281
  }
282
+ /**
283
+ * Get the last column in a worksheet
284
+ */
256
285
  get lastColumn() {
257
286
  return this.getColumn(this.columnCount);
258
287
  }
288
+ /**
289
+ * The total column size of the document. Equal to the maximum cell count from all of the rows
290
+ */
259
291
  get columnCount() {
260
292
  let maxCount = 0;
261
293
  this.eachRow(row => {
@@ -263,6 +295,9 @@ class Worksheet {
263
295
  });
264
296
  return maxCount;
265
297
  }
298
+ /**
299
+ * A count of the number of columns that have values
300
+ */
266
301
  get actualColumnCount() {
267
302
  // performance nightmare - for each row, counts all the columns used
268
303
  const counts = [];
@@ -294,23 +329,41 @@ class Worksheet {
294
329
  get _nextRow() {
295
330
  return this._lastRowNumber + 1;
296
331
  }
332
+ /**
333
+ * Get the last editable row in a worksheet (or undefined if there are none)
334
+ */
297
335
  get lastRow() {
298
336
  if (this._rows.length) {
299
337
  return this._rows[this._rows.length - 1];
300
338
  }
301
339
  return undefined;
302
340
  }
303
- // find a row (if exists) by row number
341
+ /**
342
+ * Tries to find and return row for row number, else undefined
343
+ *
344
+ * @param r - The 1-indexed row number
345
+ */
304
346
  findRow(r) {
305
347
  return this._rows[r - 1];
306
348
  }
307
- // find multiple rows (if exists) by row number
349
+ /**
350
+ * Tries to find and return rows for row number start and length, else undefined
351
+ *
352
+ * @param start - The 1-indexed starting row number
353
+ * @param length - The length of the expected array
354
+ */
308
355
  findRows(start, length) {
309
356
  return this._rows.slice(start - 1, start - 1 + length);
310
357
  }
358
+ /**
359
+ * The total row size of the document. Equal to the row number of the last row that has values.
360
+ */
311
361
  get rowCount() {
312
362
  return this._lastRowNumber;
313
363
  }
364
+ /**
365
+ * A count of the number of rows that have values. If a mid-document row is empty, it will not be included in the count.
366
+ */
314
367
  get actualRowCount() {
315
368
  // counts actual rows that have actual data
316
369
  let count = 0;
@@ -319,7 +372,9 @@ class Worksheet {
319
372
  });
320
373
  return count;
321
374
  }
322
- // get a row by row number.
375
+ /**
376
+ * Get or create row by 1-based index
377
+ */
323
378
  getRow(r) {
324
379
  let row = this._rows[r - 1];
325
380
  if (!row) {
@@ -327,7 +382,9 @@ class Worksheet {
327
382
  }
328
383
  return row;
329
384
  }
330
- // get multiple rows by row number.
385
+ /**
386
+ * Get or create rows by 1-based index
387
+ */
331
388
  getRows(start, length) {
332
389
  if (length < 1) {
333
390
  return undefined;
@@ -338,6 +395,10 @@ class Worksheet {
338
395
  }
339
396
  return rows;
340
397
  }
398
+ /**
399
+ * Add a couple of Rows by key-value, after the last current row, using the column keys,
400
+ * or add a row by contiguous Array (assign to columns A, B & C)
401
+ */
341
402
  addRow(value, style = "n") {
342
403
  const rowNo = this._nextRow;
343
404
  const row = this.getRow(rowNo);
@@ -345,6 +406,9 @@ class Worksheet {
345
406
  this._setStyleOption(rowNo, style[0] === "i" ? style : "n");
346
407
  return row;
347
408
  }
409
+ /**
410
+ * Add multiple rows by providing an array of arrays or key-value pairs
411
+ */
348
412
  addRows(value, style = "n") {
349
413
  const rows = [];
350
414
  value.forEach(row => {
@@ -352,11 +416,19 @@ class Worksheet {
352
416
  });
353
417
  return rows;
354
418
  }
419
+ /**
420
+ * Insert a Row by key-value, at the position (shifting down all rows from position),
421
+ * using the column keys, or add a row by contiguous Array (assign to columns A, B & C)
422
+ */
355
423
  insertRow(pos, value, style = "n") {
356
424
  this.spliceRows(pos, 0, value);
357
425
  this._setStyleOption(pos, style);
358
426
  return this.getRow(pos);
359
427
  }
428
+ /**
429
+ * Insert multiple rows at position (shifting down all rows from position)
430
+ * by providing an array of arrays or key-value pairs
431
+ */
360
432
  insertRows(pos, values, style = "n") {
361
433
  this.spliceRows(pos, 0, ...values);
362
434
  if (style !== "n") {
@@ -390,6 +462,9 @@ class Worksheet {
390
462
  });
391
463
  rDst.height = rSrc.height;
392
464
  }
465
+ /**
466
+ * Duplicate rows and insert new rows
467
+ */
393
468
  duplicateRow(rowNum, count, insert = false) {
394
469
  // create count duplicates of rowNum
395
470
  // either inserting new or overwriting existing rows
@@ -406,6 +481,12 @@ class Worksheet {
406
481
  });
407
482
  }
408
483
  }
484
+ /**
485
+ * Cut one or more rows (rows below are shifted up)
486
+ * and optionally insert more
487
+ *
488
+ * Known Issue: If a splice causes any merged cells to move, the results may be unpredictable
489
+ */
409
490
  spliceRows(start, count, ...inserts) {
410
491
  // same problem as row.splice, except worse.
411
492
  const nKeep = start + count;
@@ -470,31 +551,33 @@ class Worksheet {
470
551
  // account for defined names
471
552
  this.workbook.definedNames.spliceRows(this.name, start, count, nInserts);
472
553
  }
473
- eachRow(optionsOrIteratee, maybeIteratee) {
554
+ eachRow(optOrCallback, maybeCallback) {
474
555
  let options;
475
- let iteratee;
476
- if (typeof optionsOrIteratee === "function") {
477
- iteratee = optionsOrIteratee;
556
+ let callback;
557
+ if (typeof optOrCallback === "function") {
558
+ callback = optOrCallback;
478
559
  }
479
560
  else {
480
- options = optionsOrIteratee;
481
- iteratee = maybeIteratee;
561
+ options = optOrCallback;
562
+ callback = maybeCallback;
482
563
  }
483
564
  if (options && options.includeEmpty) {
484
565
  const n = this._rows.length;
485
566
  for (let i = 1; i <= n; i++) {
486
- iteratee(this.getRow(i), i);
567
+ callback(this.getRow(i), i);
487
568
  }
488
569
  }
489
570
  else {
490
571
  this._rows.forEach(row => {
491
572
  if (row && row.hasValues) {
492
- iteratee(row, row.number);
573
+ callback(row, row.number);
493
574
  }
494
575
  });
495
576
  }
496
577
  }
497
- // return all rows as sparse array
578
+ /**
579
+ * Return all rows as sparse array
580
+ */
498
581
  getSheetValues() {
499
582
  const rows = [];
500
583
  this._rows.forEach(row => {
@@ -506,13 +589,17 @@ class Worksheet {
506
589
  }
507
590
  // =========================================================================
508
591
  // Cells
509
- // returns the cell at [r,c] or address given by r. If not found, return undefined
592
+ /**
593
+ * Returns the cell at [r,c] or address given by r. If not found, return undefined
594
+ */
510
595
  findCell(r, c) {
511
596
  const address = col_cache_js_1.colCache.getAddress(r, c);
512
597
  const row = this._rows[address.row - 1];
513
598
  return row ? row.findCell(address.col) : undefined;
514
599
  }
515
- // return the cell at [r,c] or address given by r. If not found, create a new one.
600
+ /**
601
+ * Get or create cell at [r,c] or address given by r
602
+ */
516
603
  getCell(r, c) {
517
604
  const address = col_cache_js_1.colCache.getAddress(r, c);
518
605
  const row = this.getRow(address.row);
@@ -520,7 +607,15 @@ class Worksheet {
520
607
  }
521
608
  // =========================================================================
522
609
  // Merge
523
- // convert the range defined by ['tl:br'], [tl,br] or [t,l,b,r] into a single 'merged' cell
610
+ /**
611
+ * Merge cells, either:
612
+ *
613
+ * tlbr string, e.g. `'A4:B5'`
614
+ *
615
+ * tl string, br string, e.g. `'G10', 'H11'`
616
+ *
617
+ * t, l, b, r numbers, e.g. `10,11,12,13`
618
+ */
524
619
  mergeCells(...cells) {
525
620
  const dimensions = new range_js_1.Range(cells);
526
621
  this._mergeCellsInternal(dimensions);
@@ -565,9 +660,11 @@ class Worksheet {
565
660
  // return true if this._merges has a merge object
566
661
  return Object.values(this._merges).some(Boolean);
567
662
  }
568
- // scan the range defined by ['tl:br'], [tl,br] or [t,l,b,r] and if any cell is part of a merge,
569
- // un-merge the group. Note this function can affect multiple merges and merge-blocks are
570
- // atomic - either they're all merged or all un-merged.
663
+ /**
664
+ * Scan the range and if any cell is part of a merge, un-merge the group.
665
+ * Note this function can affect multiple merges and merge-blocks are
666
+ * atomic - either they're all merged or all un-merged.
667
+ */
571
668
  unMergeCells(...cells) {
572
669
  const dimensions = new range_js_1.Range(cells);
573
670
  // find any cells in that range and unmerge them
@@ -639,6 +736,10 @@ class Worksheet {
639
736
  }
640
737
  // =========================================================================
641
738
  // Images
739
+ /**
740
+ * Using the image id from `Workbook.addImage`,
741
+ * embed an image within the worksheet to cover a range
742
+ */
642
743
  addImage(imageId, range) {
643
744
  const model = {
644
745
  type: "image",
@@ -650,6 +751,9 @@ class Worksheet {
650
751
  getImages() {
651
752
  return this._media.filter(m => m.type === "image");
652
753
  }
754
+ /**
755
+ * Using the image id from `Workbook.addImage`, set the background to the worksheet
756
+ */
653
757
  addBackgroundImage(imageId) {
654
758
  const model = {
655
759
  type: "background",
@@ -663,6 +767,9 @@ class Worksheet {
663
767
  }
664
768
  // =========================================================================
665
769
  // Worksheet Protection
770
+ /**
771
+ * Protect the worksheet with optional password and options
772
+ */
666
773
  protect(password, options) {
667
774
  // TODO: make this function truly async
668
775
  // perhaps marshal to worker thread or something
@@ -697,17 +804,29 @@ class Worksheet {
697
804
  }
698
805
  // =========================================================================
699
806
  // Tables
807
+ /**
808
+ * Add a new table and return a reference to it
809
+ */
700
810
  addTable(model) {
701
811
  const table = new table_js_1.Table(this, model);
702
812
  this.tables[model.name] = table;
703
813
  return table;
704
814
  }
815
+ /**
816
+ * Fetch table by name
817
+ */
705
818
  getTable(name) {
706
819
  return this.tables[name];
707
820
  }
821
+ /**
822
+ * Delete table by name
823
+ */
708
824
  removeTable(name) {
709
825
  delete this.tables[name];
710
826
  }
827
+ /**
828
+ * Fetch all tables in the worksheet
829
+ */
711
830
  getTables() {
712
831
  return Object.values(this.tables);
713
832
  }
@@ -723,9 +842,15 @@ Please leave feedback at https://github.com/excelts/excelts/discussions/2575`);
723
842
  }
724
843
  // ===========================================================================
725
844
  // Conditional Formatting
845
+ /**
846
+ * Add conditional formatting rules
847
+ */
726
848
  addConditionalFormatting(cf) {
727
849
  this.conditionalFormattings.push(cf);
728
850
  }
851
+ /**
852
+ * Delete conditional formatting rules
853
+ */
729
854
  removeConditionalFormatting(filter) {
730
855
  if (typeof filter === "number") {
731
856
  this.conditionalFormattings.splice(filter, 1);