@cj-tech-master/excelts 1.4.5-canary.20251212064440.3eb099f → 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.
@@ -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,26 +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(options, iteratee) {
474
- if (!iteratee) {
475
- iteratee = options;
476
- options = undefined;
554
+ eachRow(optOrCallback, maybeCallback) {
555
+ let options;
556
+ let callback;
557
+ if (typeof optOrCallback === "function") {
558
+ callback = optOrCallback;
559
+ }
560
+ else {
561
+ options = optOrCallback;
562
+ callback = maybeCallback;
477
563
  }
478
564
  if (options && options.includeEmpty) {
479
565
  const n = this._rows.length;
480
566
  for (let i = 1; i <= n; i++) {
481
- iteratee(this.getRow(i), i);
567
+ callback(this.getRow(i), i);
482
568
  }
483
569
  }
484
570
  else {
485
571
  this._rows.forEach(row => {
486
572
  if (row && row.hasValues) {
487
- iteratee(row, row.number);
573
+ callback(row, row.number);
488
574
  }
489
575
  });
490
576
  }
491
577
  }
492
- // return all rows as sparse array
578
+ /**
579
+ * Return all rows as sparse array
580
+ */
493
581
  getSheetValues() {
494
582
  const rows = [];
495
583
  this._rows.forEach(row => {
@@ -501,13 +589,17 @@ class Worksheet {
501
589
  }
502
590
  // =========================================================================
503
591
  // Cells
504
- // 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
+ */
505
595
  findCell(r, c) {
506
596
  const address = col_cache_js_1.colCache.getAddress(r, c);
507
597
  const row = this._rows[address.row - 1];
508
598
  return row ? row.findCell(address.col) : undefined;
509
599
  }
510
- // 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
+ */
511
603
  getCell(r, c) {
512
604
  const address = col_cache_js_1.colCache.getAddress(r, c);
513
605
  const row = this.getRow(address.row);
@@ -515,7 +607,15 @@ class Worksheet {
515
607
  }
516
608
  // =========================================================================
517
609
  // Merge
518
- // 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
+ */
519
619
  mergeCells(...cells) {
520
620
  const dimensions = new range_js_1.Range(cells);
521
621
  this._mergeCellsInternal(dimensions);
@@ -560,9 +660,11 @@ class Worksheet {
560
660
  // return true if this._merges has a merge object
561
661
  return Object.values(this._merges).some(Boolean);
562
662
  }
563
- // scan the range defined by ['tl:br'], [tl,br] or [t,l,b,r] and if any cell is part of a merge,
564
- // un-merge the group. Note this function can affect multiple merges and merge-blocks are
565
- // 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
+ */
566
668
  unMergeCells(...cells) {
567
669
  const dimensions = new range_js_1.Range(cells);
568
670
  // find any cells in that range and unmerge them
@@ -634,6 +736,10 @@ class Worksheet {
634
736
  }
635
737
  // =========================================================================
636
738
  // Images
739
+ /**
740
+ * Using the image id from `Workbook.addImage`,
741
+ * embed an image within the worksheet to cover a range
742
+ */
637
743
  addImage(imageId, range) {
638
744
  const model = {
639
745
  type: "image",
@@ -645,6 +751,9 @@ class Worksheet {
645
751
  getImages() {
646
752
  return this._media.filter(m => m.type === "image");
647
753
  }
754
+ /**
755
+ * Using the image id from `Workbook.addImage`, set the background to the worksheet
756
+ */
648
757
  addBackgroundImage(imageId) {
649
758
  const model = {
650
759
  type: "background",
@@ -658,6 +767,9 @@ class Worksheet {
658
767
  }
659
768
  // =========================================================================
660
769
  // Worksheet Protection
770
+ /**
771
+ * Protect the worksheet with optional password and options
772
+ */
661
773
  protect(password, options) {
662
774
  // TODO: make this function truly async
663
775
  // perhaps marshal to worker thread or something
@@ -692,17 +804,29 @@ class Worksheet {
692
804
  }
693
805
  // =========================================================================
694
806
  // Tables
807
+ /**
808
+ * Add a new table and return a reference to it
809
+ */
695
810
  addTable(model) {
696
811
  const table = new table_js_1.Table(this, model);
697
812
  this.tables[model.name] = table;
698
813
  return table;
699
814
  }
815
+ /**
816
+ * Fetch table by name
817
+ */
700
818
  getTable(name) {
701
819
  return this.tables[name];
702
820
  }
821
+ /**
822
+ * Delete table by name
823
+ */
703
824
  removeTable(name) {
704
825
  delete this.tables[name];
705
826
  }
827
+ /**
828
+ * Fetch all tables in the worksheet
829
+ */
706
830
  getTables() {
707
831
  return Object.values(this.tables);
708
832
  }
@@ -718,9 +842,15 @@ Please leave feedback at https://github.com/excelts/excelts/discussions/2575`);
718
842
  }
719
843
  // ===========================================================================
720
844
  // Conditional Formatting
845
+ /**
846
+ * Add conditional formatting rules
847
+ */
721
848
  addConditionalFormatting(cf) {
722
849
  this.conditionalFormattings.push(cf);
723
850
  }
851
+ /**
852
+ * Delete conditional formatting rules
853
+ */
724
854
  removeConditionalFormatting(filter) {
725
855
  if (typeof filter === "number") {
726
856
  this.conditionalFormattings.splice(filter, 1);
@@ -2,9 +2,11 @@ import { colCache } from "../utils/col-cache.js";
2
2
  import { isEqual } from "../utils/under-dash.js";
3
3
  import { Enums } from "./enums.js";
4
4
  const DEFAULT_COLUMN_WIDTH = 9;
5
- // Column defines the column properties for 1 column.
6
- // This includes header rows, widths, key, (style), etc.
7
- // Worksheet will condense the columns as appropriate during serialization
5
+ /**
6
+ * Column defines the column properties for 1 column.
7
+ * This includes header rows, widths, key, (style), etc.
8
+ * Worksheet will condense the columns as appropriate during serialization
9
+ */
8
10
  class Column {
9
11
  constructor(worksheet, number, defn) {
10
12
  this._worksheet = worksheet;
@@ -20,6 +22,9 @@ class Column {
20
22
  get worksheet() {
21
23
  return this._worksheet;
22
24
  }
25
+ /**
26
+ * Column letter key
27
+ */
23
28
  get letter() {
24
29
  return colCache.n2l(this._number);
25
30
  }
@@ -68,6 +73,9 @@ class Column {
68
73
  }
69
74
  return [];
70
75
  }
76
+ /**
77
+ * Can be a string to set one row high header or an array to set multi-row high header
78
+ */
71
79
  get header() {
72
80
  return this._header;
73
81
  }
@@ -82,6 +90,9 @@ class Column {
82
90
  this._header = undefined;
83
91
  }
84
92
  }
93
+ /**
94
+ * The name of the properties associated with this column in each row
95
+ */
85
96
  get key() {
86
97
  return this._key;
87
98
  }
@@ -95,18 +106,27 @@ class Column {
95
106
  this._worksheet.setColumnKey(this._key, this);
96
107
  }
97
108
  }
109
+ /**
110
+ * Hides the column
111
+ */
98
112
  get hidden() {
99
113
  return !!this._hidden;
100
114
  }
101
115
  set hidden(value) {
102
116
  this._hidden = value;
103
117
  }
118
+ /**
119
+ * Set an outline level for columns
120
+ */
104
121
  get outlineLevel() {
105
122
  return this._outlineLevel || 0;
106
123
  }
107
124
  set outlineLevel(value) {
108
125
  this._outlineLevel = value;
109
126
  }
127
+ /**
128
+ * Indicate the collapsed state based on outlineLevel
129
+ */
110
130
  get collapsed() {
111
131
  return !!(this._outlineLevel && this._outlineLevel >= this._worksheet.properties.outlineLevelCol);
112
132
  }
@@ -148,16 +168,25 @@ class Column {
148
168
  get headerCount() {
149
169
  return this.headers.length;
150
170
  }
151
- eachCell(options, iteratee) {
171
+ eachCell(optionsOrCallback, maybeCallback) {
152
172
  const colNumber = this.number;
153
- if (!iteratee) {
154
- iteratee = options;
173
+ let options;
174
+ let callback;
175
+ if (typeof optionsOrCallback === "function") {
155
176
  options = {};
177
+ callback = optionsOrCallback;
178
+ }
179
+ else {
180
+ options = optionsOrCallback;
181
+ callback = maybeCallback;
156
182
  }
157
183
  this._worksheet.eachRow(options, (row, rowNumber) => {
158
- iteratee(row.getCell(colNumber), rowNumber);
184
+ callback(row.getCell(colNumber), rowNumber);
159
185
  });
160
186
  }
187
+ /**
188
+ * The cell values in the column
189
+ */
161
190
  get values() {
162
191
  const v = [];
163
192
  this.eachCell((cell, rowNumber) => {
@@ -9,19 +9,29 @@ class Row {
9
9
  this.style = {};
10
10
  this.outlineLevel = 0;
11
11
  }
12
- // return the row number
12
+ /**
13
+ * The row number
14
+ */
13
15
  get number() {
14
16
  return this._number;
15
17
  }
18
+ /**
19
+ * The worksheet that contains this row
20
+ */
16
21
  get worksheet() {
17
22
  return this._worksheet;
18
23
  }
19
- // Inform Streaming Writer that this row (and all rows before it) are complete
20
- // and ready to write. Has no effect on Worksheet document
24
+ /**
25
+ * Commit a completed row to stream.
26
+ * Inform Streaming Writer that this row (and all rows before it) are complete
27
+ * and ready to write. Has no effect on Worksheet document.
28
+ */
21
29
  commit() {
22
30
  this._worksheet._commitRow(this);
23
31
  }
24
- // helps GC by breaking cyclic references
32
+ /**
33
+ * Helps GC by breaking cyclic references
34
+ */
25
35
  destroy() {
26
36
  delete this._worksheet;
27
37
  delete this._cells;
@@ -40,7 +50,9 @@ class Row {
40
50
  }
41
51
  return cell;
42
52
  }
43
- // get cell by key, letter or column number
53
+ /**
54
+ * Get cell by number, column letter or column key
55
+ */
44
56
  getCell(col) {
45
57
  let colNum;
46
58
  if (typeof col === "string") {
@@ -63,7 +75,11 @@ class Row {
63
75
  col: colNum
64
76
  }));
65
77
  }
66
- // remove cell(s) and shift all higher cells down by count
78
+ /**
79
+ * Cut one or more cells (cells to the right are shifted left)
80
+ *
81
+ * Note: this operation will not affect other rows
82
+ */
67
83
  splice(start, count, ...inserts) {
68
84
  const nKeep = start + count;
69
85
  const nExpand = inserts.length - count;
@@ -112,26 +128,26 @@ class Row {
112
128
  cDst.comment = undefined;
113
129
  }
114
130
  }
115
- eachCell(optionsOrIteratee, maybeIteratee) {
131
+ eachCell(optOrCallback, maybeCallback) {
116
132
  let options = null;
117
- let iteratee;
118
- if (typeof optionsOrIteratee === "function") {
119
- iteratee = optionsOrIteratee;
133
+ let callback;
134
+ if (typeof optOrCallback === "function") {
135
+ callback = optOrCallback;
120
136
  }
121
137
  else {
122
- options = optionsOrIteratee;
123
- iteratee = maybeIteratee;
138
+ options = optOrCallback;
139
+ callback = maybeCallback;
124
140
  }
125
141
  if (options && options.includeEmpty) {
126
142
  const n = this._cells.length;
127
143
  for (let i = 1; i <= n; i++) {
128
- iteratee(this.getCell(i), i);
144
+ callback(this.getCell(i), i);
129
145
  }
130
146
  }
131
147
  else {
132
148
  this._cells.forEach((cell, index) => {
133
149
  if (cell && cell.type !== Enums.ValueType.Null) {
134
- iteratee(cell, index + 1);
150
+ callback(cell, index + 1);
135
151
  }
136
152
  });
137
153
  }
@@ -152,7 +168,9 @@ class Row {
152
168
  }
153
169
  ws.rowBreaks.push(pb);
154
170
  }
155
- // return a sparse array of cell values
171
+ /**
172
+ * Get a row as a sparse array
173
+ */
156
174
  get values() {
157
175
  const values = [];
158
176
  this._cells.forEach(cell => {
@@ -162,7 +180,9 @@ class Row {
162
180
  });
163
181
  return values;
164
182
  }
165
- // set the values by contiguous or sparse array, or by key'd object literal
183
+ /**
184
+ * Set the values by contiguous or sparse array, or by key'd object literal
185
+ */
166
186
  set values(value) {
167
187
  // this operation is not additive - any prior cells are removed
168
188
  this._cells = [];
@@ -198,13 +218,21 @@ class Row {
198
218
  });
199
219
  }
200
220
  }
201
- // returns true if the row includes at least one cell with a value
221
+ /**
222
+ * Returns true if the row includes at least one cell with a value
223
+ */
202
224
  get hasValues() {
203
225
  return this._cells.some(cell => cell && cell.type !== Enums.ValueType.Null);
204
226
  }
227
+ /**
228
+ * Number of cells including empty ones
229
+ */
205
230
  get cellCount() {
206
231
  return this._cells.length;
207
232
  }
233
+ /**
234
+ * Number of non-empty cells
235
+ */
208
236
  get actualCellCount() {
209
237
  let count = 0;
210
238
  this.eachCell(() => {
@@ -212,7 +240,9 @@ class Row {
212
240
  });
213
241
  return count;
214
242
  }
215
- // get the min and max column number for the non-null cells in this row or null
243
+ /**
244
+ * Get the min and max column number for the non-null cells in this row or null
245
+ */
216
246
  get dimensions() {
217
247
  let min = 0;
218
248
  let max = 0;