exceljs-lts 4.5.4 → 4.5.5

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.
Files changed (36) hide show
  1. package/README.md +28 -11
  2. package/README_zh.md +18 -10
  3. package/dist/es5/doc/cell.js +37 -12
  4. package/dist/es5/doc/cell.js.map +1 -1
  5. package/dist/es5/doc/data-validations.js +94 -1
  6. package/dist/es5/doc/data-validations.js.map +1 -1
  7. package/dist/es5/stream/xlsx/workbook-reader.js +2 -1
  8. package/dist/es5/stream/xlsx/workbook-reader.js.map +1 -1
  9. package/dist/es5/stream/xlsx/workbook-writer.js +21 -1
  10. package/dist/es5/stream/xlsx/workbook-writer.js.map +1 -1
  11. package/dist/es5/stream/xlsx/worksheet-reader.js +1 -1
  12. package/dist/es5/stream/xlsx/worksheet-reader.js.map +1 -1
  13. package/dist/es5/stream/xlsx/worksheet-writer.js +87 -1
  14. package/dist/es5/stream/xlsx/worksheet-writer.js.map +1 -1
  15. package/dist/es5/xlsx/rel-type.js +1 -0
  16. package/dist/es5/xlsx/rel-type.js.map +1 -1
  17. package/dist/es5/xlsx/xform/sheet/data-validations-xform.js +14 -3
  18. package/dist/es5/xlsx/xform/sheet/data-validations-xform.js.map +1 -1
  19. package/dist/exceljs.bare.js +148 -18
  20. package/dist/exceljs.bare.js.map +5 -5
  21. package/dist/exceljs.bare.min.js +1 -1
  22. package/dist/exceljs.bare.min.js.map +1 -1
  23. package/dist/exceljs.js +148 -18
  24. package/dist/exceljs.js.map +5 -5
  25. package/dist/exceljs.min.js +4 -4
  26. package/dist/exceljs.min.js.map +1 -1
  27. package/index.d.ts +36 -3
  28. package/lib/doc/cell.js +37 -12
  29. package/lib/doc/data-validations.js +83 -1
  30. package/lib/stream/xlsx/workbook-reader.js +6 -1
  31. package/lib/stream/xlsx/workbook-writer.js +19 -0
  32. package/lib/stream/xlsx/worksheet-reader.js +1 -1
  33. package/lib/stream/xlsx/worksheet-writer.js +71 -1
  34. package/lib/xlsx/rel-type.js +1 -0
  35. package/lib/xlsx/xform/sheet/data-validations-xform.js +14 -3
  36. package/package.json +1 -1
@@ -286,6 +286,9 @@ const {
286
286
  slideFormula
287
287
  } = require('../utils/shared-formula');
288
288
  const Note = require('./note');
289
+ const {
290
+ copyStyle
291
+ } = require('../utils/copy-style');
289
292
  // Cell requirements
290
293
  // Operate inside a worksheet
291
294
  // Store and retrieve a value with a range of types: text, number, date, hyperlink, reference, formula, etc.
@@ -315,7 +318,8 @@ class Cell {
315
318
 
316
319
  // help GC by removing cyclic (and other) references
317
320
  destroy() {
318
- delete this.style;
321
+ delete this._style;
322
+ delete this._styleShared;
319
323
  delete this._value;
320
324
  delete this._row;
321
325
  delete this._column;
@@ -324,38 +328,59 @@ class Cell {
324
328
 
325
329
  // =========================================================================
326
330
  // Styles stuff
331
+
332
+ // Cells loaded from a file share one style object per xf. The object is only
333
+ // copied when the style is handed out (cell.style), so code that mutates it
334
+ // in place (cell.style.fill = ...) changes this cell alone.
335
+ get style() {
336
+ if (this._styleShared) {
337
+ this._style = copyStyle(this._style);
338
+ this._styleShared = false;
339
+ }
340
+ return this._style;
341
+ }
342
+ set style(value) {
343
+ this._style = value;
344
+ this._styleShared = false;
345
+ }
346
+
347
+ // use a style object that other cells may share (e.g. a cached xf style)
348
+ _setSharedStyle(style) {
349
+ this._style = style;
350
+ this._styleShared = true;
351
+ }
327
352
  get numFmt() {
328
- return this.style.numFmt;
353
+ return this._style.numFmt;
329
354
  }
330
355
  set numFmt(value) {
331
356
  this._setStyle('numFmt', value);
332
357
  }
333
358
  get font() {
334
- return this.style.font;
359
+ return this._style.font;
335
360
  }
336
361
  set font(value) {
337
362
  this._setStyle('font', value);
338
363
  }
339
364
  get alignment() {
340
- return this.style.alignment;
365
+ return this._style.alignment;
341
366
  }
342
367
  set alignment(value) {
343
368
  this._setStyle('alignment', value);
344
369
  }
345
370
  get border() {
346
- return this.style.border;
371
+ return this._style.border;
347
372
  }
348
373
  set border(value) {
349
374
  this._setStyle('border', value);
350
375
  }
351
376
  get fill() {
352
- return this.style.fill;
377
+ return this._style.fill;
353
378
  }
354
379
  set fill(value) {
355
380
  this._setStyle('fill', value);
356
381
  }
357
382
  get protection() {
358
- return this.style.protection;
383
+ return this._style.protection;
359
384
  }
360
385
  set protection(value) {
361
386
  this._setStyle('protection', value);
@@ -363,7 +388,7 @@ class Cell {
363
388
  _setStyle(name, value) {
364
389
  // style objects can be shared (e.g. by every cell loaded with the same xf),
365
390
  // so never mutate one in place; give this cell a new object instead
366
- const oldStyle = this.style;
391
+ const oldStyle = this._style;
367
392
  const style = {
368
393
  ...oldStyle,
369
394
  [name]: value
@@ -375,12 +400,12 @@ class Cell {
375
400
  const merges = master._mergeCount > 0 && this.worksheet._merges;
376
401
  // the streaming WorksheetWriter keeps its merges in an array
377
402
  const range = merges && (Array.isArray(merges) ? merges.find(merge => merge.top === master.row && merge.left === master.col) : merges[master.address]);
378
- if (range && (master === this || master.style === oldStyle)) {
403
+ if (range && (master === this || master._style === oldStyle)) {
379
404
  for (let r = range.top; r <= range.bottom; r++) {
380
405
  const row = this.worksheet.findRow(r);
381
406
  for (let c = range.left; row && c <= range.right; c++) {
382
407
  const cell = row.findCell(c);
383
- if (cell && cell.style === oldStyle) {
408
+ if (cell && cell._style === oldStyle) {
384
409
  cell.style = style;
385
410
  }
386
411
  }
@@ -588,7 +613,7 @@ class Cell {
588
613
  const {
589
614
  model
590
615
  } = this._value;
591
- model.style = this.style;
616
+ model.style = this._style;
592
617
  if (this._comment) {
593
618
  model.comment = this._comment.model;
594
619
  }
@@ -606,7 +631,7 @@ class Cell {
606
631
  }
607
632
  }
608
633
  if (value.style) {
609
- this.style = value.style;
634
+ this._setSharedStyle(value.style);
610
635
  } else {
611
636
  this.style = {};
612
637
  }
@@ -1263,7 +1288,7 @@ const Value = {
1263
1288
  };
1264
1289
  module.exports = Cell;
1265
1290
 
1266
- },{"../utils/col-cache":20,"../utils/shared-formula":24,"../utils/under-dash":27,"./enums":7,"./note":9}],4:[function(require,module,exports){
1291
+ },{"../utils/col-cache":20,"../utils/copy-style":21,"../utils/shared-formula":24,"../utils/under-dash":27,"./enums":7,"./note":9}],4:[function(require,module,exports){
1267
1292
  'use strict';
1268
1293
 
1269
1294
  const _ = require('../utils/under-dash');
@@ -1544,23 +1569,116 @@ module.exports = Column;
1544
1569
  },{"../utils/col-cache":20,"../utils/under-dash":27,"./enums":7}],5:[function(require,module,exports){
1545
1570
  "use strict";
1546
1571
 
1572
+ const colCache = require('../utils/col-cache');
1573
+ function isRange(address) {
1574
+ return typeof address === 'string' && address.includes(':');
1575
+ }
1547
1576
  class DataValidations {
1548
1577
  constructor(model) {
1549
1578
  this.model = model || {};
1579
+ // Range-keyed entries (e.g. 'A2:A1048576', kept intact when a file is loaded) are tracked
1580
+ // separately so per-cell add/find stay O(1) when there are none (exceljs#1842).
1581
+ this._ranges = new Map();
1582
+ Object.keys(this.model).forEach(key => {
1583
+ if (isRange(key)) {
1584
+ this._addRange(key);
1585
+ }
1586
+ });
1550
1587
  }
1551
1588
  add(address, validation) {
1589
+ if (isRange(address)) {
1590
+ this._addRange(address);
1591
+ } else {
1592
+ this._splitRangeAt(address);
1593
+ }
1552
1594
  return this.model[address] = validation;
1553
1595
  }
1554
1596
  find(address) {
1555
- return this.model[address];
1597
+ const validation = this.model[address];
1598
+ if (validation !== undefined || isRange(address) || !this._ranges.size) {
1599
+ return validation;
1600
+ }
1601
+ // fall back to range-keyed entries that contain the address
1602
+ const key = this._findRangeKey(address);
1603
+ return key ? this.model[key] : undefined;
1556
1604
  }
1557
1605
  remove(address) {
1606
+ if (isRange(address)) {
1607
+ this._ranges.delete(address);
1608
+ } else {
1609
+ this._splitRangeAt(address);
1610
+ }
1558
1611
  this.model[address] = undefined;
1559
1612
  }
1613
+ _addRange(key) {
1614
+ if (!this._ranges.has(key)) {
1615
+ this._ranges.set(key, colCache.decodeEx(key));
1616
+ }
1617
+ }
1618
+ _findRangeKey(address) {
1619
+ if (!this._ranges.size) {
1620
+ return undefined;
1621
+ }
1622
+ const {
1623
+ row,
1624
+ col
1625
+ } = colCache.decodeAddress(address);
1626
+ for (const [key, {
1627
+ top,
1628
+ left,
1629
+ bottom,
1630
+ right
1631
+ }] of this._ranges) {
1632
+ if (this.model[key] && row >= top && row <= bottom && col >= left && col <= right) {
1633
+ return key;
1634
+ }
1635
+ }
1636
+ return undefined;
1637
+ }
1638
+
1639
+ // If a single cell lies inside a range-keyed entry, replace that entry with up to four
1640
+ // rectangles covering the rest of the range, so the cell can get its own validation
1641
+ // (or none) without overlapping sqrefs.
1642
+ _splitRangeAt(address) {
1643
+ const key = this._findRangeKey(address);
1644
+ if (!key) {
1645
+ return;
1646
+ }
1647
+ const validation = this.model[key];
1648
+ const {
1649
+ row,
1650
+ col
1651
+ } = colCache.decodeAddress(address);
1652
+ const {
1653
+ top,
1654
+ left,
1655
+ bottom,
1656
+ right
1657
+ } = this._ranges.get(key);
1658
+ delete this.model[key];
1659
+ this._ranges.delete(key);
1660
+ const addRect = (t, l, b, r) => {
1661
+ if (t > b || l > r) {
1662
+ return;
1663
+ }
1664
+ const tl = colCache.encodeAddress(t, l);
1665
+ if (t === b && l === r) {
1666
+ this.model[tl] = validation;
1667
+ } else {
1668
+ const rangeKey = `${tl}:${colCache.encodeAddress(b, r)}`;
1669
+ this.model[rangeKey] = validation;
1670
+ this._addRange(rangeKey);
1671
+ }
1672
+ };
1673
+ addRect(top, left, row - 1, right); // rows above
1674
+ addRect(row + 1, left, bottom, right); // rows below
1675
+ addRect(row, left, row, col - 1); // left of the cell
1676
+ addRect(row, col + 1, row, right); // right of the cell
1677
+ }
1560
1678
  }
1561
1679
  module.exports = DataValidations;
1562
1680
 
1563
- },{}],6:[function(require,module,exports){
1681
+ },{"../utils/col-cache":20}],6:[function(require,module,exports){
1564
1682
  'use strict';
1565
1683
 
1566
1684
  const _ = require('../utils/under-dash');
@@ -6401,6 +6519,7 @@ module.exports = {
6401
6519
  CoreProperties: 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties',
6402
6520
  ExtenderProperties: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties',
6403
6521
  Comments: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments',
6522
+ Drawing: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing',
6404
6523
  VmlDrawing: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
6405
6524
  Table: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/table',
6406
6525
  PivotCacheDefinition: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/pivotCacheDefinition',
@@ -11407,6 +11526,9 @@ const utils = require('../../../utils/utils');
11407
11526
  const colCache = require('../../../utils/col-cache');
11408
11527
  const BaseXform = require('../base-xform');
11409
11528
  const Range = require('../../../doc/range');
11529
+
11530
+ // sqref ranges with more cells than this are kept as a single range-keyed model entry
11531
+ const MAX_EXPANDED_RANGE_CELLS = 1000;
11410
11532
  function assign(definedName, attributes, name, defaultValue) {
11411
11533
  const value = attributes[name];
11412
11534
  if (value !== undefined) {
@@ -11606,12 +11728,20 @@ class DataValidationsXform extends BaseXform {
11606
11728
  }
11607
11729
  // The four known cases: 1. E4:L9 N4:U9 2.E4 L9 3. N4:U9 4. E4
11608
11730
  const list = this._address.split(/\s+/g) || [];
11731
+ // Large ranges (e.g. a whole column 'A2:A1048576') are kept as one range-keyed entry
11732
+ // instead of being expanded into one entry per cell, which could take minutes and
11733
+ // gigabytes (exceljs#1842). DataValidations.find() resolves them. Small ranges are
11734
+ // still expanded so the model keeps its per-cell shape and lookups stay O(1).
11609
11735
  list.forEach(addr => {
11610
11736
  if (addr.includes(':')) {
11611
11737
  const range = new Range(addr);
11612
- range.forEachAddress(address => {
11613
- this.model[address] = this._dataValidation;
11614
- });
11738
+ if (range.count > MAX_EXPANDED_RANGE_CELLS) {
11739
+ this.model[range.range] = this._dataValidation;
11740
+ } else {
11741
+ range.forEachAddress(address => {
11742
+ this.model[address] = this._dataValidation;
11743
+ });
11744
+ }
11615
11745
  } else {
11616
11746
  this.model[addr] = this._dataValidation;
11617
11747
  }