Skip to content
This repository was archived by the owner on Oct 17, 2024. It is now read-only.

Commit 75c8812

Browse files
committed
Removed protection, pivot tables and node uuid for much smaller bundle size
1 parent 5bed18b commit 75c8812

File tree

7 files changed

+30
-197
lines changed

7 files changed

+30
-197
lines changed

.prettier

-7
This file was deleted.

lib/doc/workbook.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
const Worksheet = require('./worksheet');
44
const DefinedNames = require('./defined-names');
55
const XLSX = require('../xlsx/xlsx');
6-
const CSV = require('../csv/csv');
76

87
// Workbook requirements
98
// Load and Save from file and stream
@@ -36,11 +35,6 @@ class Workbook {
3635
return this._xlsx;
3736
}
3837

39-
get csv() {
40-
if (!this._csv) this._csv = new CSV(this);
41-
return this._csv;
42-
}
43-
4438
get nextId() {
4539
// find the next unique spot to add worksheet
4640
for (let i = 1; i < this._worksheets.length; i++) {
@@ -79,7 +73,10 @@ class Workbook {
7973
}
8074
}
8175

82-
const lastOrderNo = this._worksheets.reduce((acc, ws) => ((ws && ws.orderNo) > acc ? ws.orderNo : acc), 0);
76+
const lastOrderNo = this._worksheets.reduce(
77+
(acc, ws) => ((ws && ws.orderNo) > acc ? ws.orderNo : acc),
78+
0
79+
);
8380
const worksheetOptions = Object.assign({}, options, {
8481
id,
8582
name,

lib/doc/worksheet.js

+15-62
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ const Enums = require('./enums');
88
const Image = require('./image');
99
const Table = require('./table');
1010
const DataValidations = require('./data-validations');
11-
const {makePivotTable} = require('./pivot-table');
12-
const Encryptor = require('../utils/encryptor');
1311
const {copyStyle} = require('../utils/copy-style');
1412

1513
// Worksheet requirements
@@ -156,11 +154,15 @@ class Worksheet {
156154
// Illegal character in worksheet name: asterisk (*), question mark (?),
157155
// colon (:), forward slash (/ \), or bracket ([])
158156
if (/[*?:/\\[\]]/.test(name)) {
159-
throw new Error(`Worksheet name ${name} cannot include any of the following characters: * ? : \\ / [ ]`);
157+
throw new Error(
158+
`Worksheet name ${name} cannot include any of the following characters: * ? : \\ / [ ]`
159+
);
160160
}
161161

162162
if (/(^')|('$)/.test(name)) {
163-
throw new Error(`The first or last character of worksheet name cannot be a single quotation mark: ${name}`);
163+
throw new Error(
164+
`The first or last character of worksheet name cannot be a single quotation mark: ${name}`
165+
);
164166
}
165167

166168
if (name && name.length > 31) {
@@ -529,7 +531,9 @@ class Worksheet {
529531
if (cell._value.constructor.name === 'MergeValue') {
530532
const cellToBeMerged = this.getRow(cell._row._number + nInserts).getCell(colNumber);
531533
const prevMaster = cell._value._master;
532-
const newMaster = this.getRow(prevMaster._row._number + nInserts).getCell(prevMaster._column._number);
534+
const newMaster = this.getRow(prevMaster._row._number + nInserts).getCell(
535+
prevMaster._column._number
536+
);
533537
cellToBeMerged.merge(newMaster);
534538
}
535539
});
@@ -751,44 +755,6 @@ class Worksheet {
751755
return image && image.imageId;
752756
}
753757

754-
// =========================================================================
755-
// Worksheet Protection
756-
protect(password, options) {
757-
// TODO: make this function truly async
758-
// perhaps marshal to worker thread or something
759-
return new Promise(resolve => {
760-
this.sheetProtection = {
761-
sheet: true,
762-
};
763-
if (options && 'spinCount' in options) {
764-
// force spinCount to be integer >= 0
765-
options.spinCount = Number.isFinite(options.spinCount) ? Math.round(Math.max(0, options.spinCount)) : 100000;
766-
}
767-
if (password) {
768-
this.sheetProtection.algorithmName = 'SHA-512';
769-
this.sheetProtection.saltValue = Encryptor.randomBytes(16).toString('base64');
770-
this.sheetProtection.spinCount = options && 'spinCount' in options ? options.spinCount : 100000; // allow user specified spinCount
771-
this.sheetProtection.hashValue = Encryptor.convertPasswordToHash(
772-
password,
773-
'SHA512',
774-
this.sheetProtection.saltValue,
775-
this.sheetProtection.spinCount
776-
);
777-
}
778-
if (options) {
779-
this.sheetProtection = Object.assign(this.sheetProtection, options);
780-
if (!password && 'spinCount' in options) {
781-
delete this.sheetProtection.spinCount;
782-
}
783-
}
784-
resolve();
785-
});
786-
}
787-
788-
unprotect() {
789-
this.sheetProtection = null;
790-
}
791-
792758
// =========================================================================
793759
// Tables
794760
addTable(model) {
@@ -809,23 +775,6 @@ class Worksheet {
809775
return Object.values(this.tables);
810776
}
811777

812-
// =========================================================================
813-
// Pivot Tables
814-
addPivotTable(model) {
815-
// eslint-disable-next-line no-console
816-
console.warn(
817-
`Warning: Pivot Table support is experimental.
818-
Please leave feedback at https://github.com/exceljs/exceljs/discussions/2575`
819-
);
820-
821-
const pivotTable = makePivotTable(this, model);
822-
823-
this.pivotTables.push(pivotTable);
824-
this.workbook.pivotTables.push(pivotTable);
825-
826-
return pivotTable;
827-
}
828-
829778
// ===========================================================================
830779
// Conditional Formatting
831780
addConditionalFormatting(cf) {
@@ -846,13 +795,17 @@ Please leave feedback at https://github.com/exceljs/exceljs/discussions/2575`
846795
// Deprecated
847796
get tabColor() {
848797
// eslint-disable-next-line no-console
849-
console.trace('worksheet.tabColor property is now deprecated. Please use worksheet.properties.tabColor');
798+
console.trace(
799+
'worksheet.tabColor property is now deprecated. Please use worksheet.properties.tabColor'
800+
);
850801
return this.properties.tabColor;
851802
}
852803

853804
set tabColor(value) {
854805
// eslint-disable-next-line no-console
855-
console.trace('worksheet.tabColor property is now deprecated. Please use worksheet.properties.tabColor');
806+
console.trace(
807+
'worksheet.tabColor property is now deprecated. Please use worksheet.properties.tabColor'
808+
);
856809
this.properties.tabColor = value;
857810
}
858811

lib/stream/xlsx/worksheet-writer.js

-41
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const _ = require('../../utils/under-dash');
33
const RelType = require('../../xlsx/rel-type');
44

55
const colCache = require('../../utils/col-cache');
6-
const Encryptor = require('../../utils/encryptor');
76
const Dimensions = require('../../doc/range');
87
const StringBuf = require('../../utils/string-buf');
98

@@ -481,46 +480,6 @@ class WorksheetWriter {
481480
return this._background && this._background.imageId;
482481
}
483482

484-
// =========================================================================
485-
// Worksheet Protection
486-
protect(password, options) {
487-
// TODO: make this function truly async
488-
// perhaps marshal to worker thread or something
489-
return new Promise(resolve => {
490-
this.sheetProtection = {
491-
sheet: true,
492-
};
493-
if (options && 'spinCount' in options) {
494-
// force spinCount to be integer >= 0
495-
options.spinCount = Number.isFinite(options.spinCount) ? Math.round(Math.max(0, options.spinCount)) : 100000;
496-
}
497-
if (password) {
498-
this.sheetProtection.algorithmName = 'SHA-512';
499-
this.sheetProtection.saltValue = Encryptor.randomBytes(16).toString('base64');
500-
this.sheetProtection.spinCount = options && 'spinCount' in options ? options.spinCount : 100000; // allow user specified spinCount
501-
this.sheetProtection.hashValue = Encryptor.convertPasswordToHash(
502-
password,
503-
'SHA512',
504-
this.sheetProtection.saltValue,
505-
this.sheetProtection.spinCount
506-
);
507-
}
508-
if (options) {
509-
this.sheetProtection = Object.assign(this.sheetProtection, options);
510-
if (!password && 'spinCount' in options) {
511-
delete this.sheetProtection.spinCount;
512-
}
513-
}
514-
resolve();
515-
});
516-
}
517-
518-
unprotect() {
519-
this.sheetProtection = null;
520-
}
521-
522-
// ================================================================================
523-
524483
_write(text) {
525484
xmlBuffer.reset();
526485
xmlBuffer.addText(text);

lib/xlsx/xform/book/workbook-xform.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const SheetXform = require('./sheet-xform');
1111
const WorkbookViewXform = require('./workbook-view-xform');
1212
const WorkbookPropertiesXform = require('./workbook-properties-xform');
1313
const WorkbookCalcPropertiesXform = require('./workbook-calc-properties-xform');
14-
const WorkbookPivotCacheXform = require('./workbook-pivot-cache-xform');
1514

1615
class WorkbookXform extends BaseXform {
1716
constructor() {
@@ -32,11 +31,6 @@ class WorkbookXform extends BaseXform {
3231
childXform: new DefinedNameXform(),
3332
}),
3433
calcPr: new WorkbookCalcPropertiesXform(),
35-
pivotCaches: new ListXform({
36-
tag: 'pivotCaches',
37-
count: false,
38-
childXform: new WorkbookPivotCacheXform(),
39-
}),
4034
};
4135
}
4236

@@ -59,7 +53,10 @@ class WorkbookXform extends BaseXform {
5953
});
6054
}
6155

62-
if (sheet.pageSetup && (sheet.pageSetup.printTitlesRow || sheet.pageSetup.printTitlesColumn)) {
56+
if (
57+
sheet.pageSetup &&
58+
(sheet.pageSetup.printTitlesRow || sheet.pageSetup.printTitlesColumn)
59+
) {
6360
const ranges = [];
6461

6562
if (sheet.pageSetup.printTitlesColumn) {
@@ -102,7 +99,6 @@ class WorkbookXform extends BaseXform {
10299
this.map.sheets.render(xmlStream, model.sheets);
103100
this.map.definedNames.render(xmlStream, model.definedNames);
104101
this.map.calcPr.render(xmlStream, model.calcProperties);
105-
this.map.pivotCaches.render(xmlStream, model.pivotTables);
106102

107103
xmlStream.closeNode();
108104
}

lib/xlsx/xform/sheet/cf-ext/cf-rule-ext-xform.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const {v4: uuidv4} = require('uuid');
21
const BaseXform = require('../../base-xform');
32
const CompositeXform = require('../../composite-xform');
43

@@ -11,6 +10,8 @@ const extIcons = {
1110
'5Boxes': true,
1211
};
1312

13+
const uuidv4 = global.crypto.randomUUID;
14+
1415
class CfRuleExtXform extends CompositeXform {
1516
constructor() {
1617
super();

lib/xlsx/xlsx.js

+5-71
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ const WorkbookXform = require('./xform/book/workbook-xform');
1919
const WorksheetXform = require('./xform/sheet/worksheet-xform');
2020
const DrawingXform = require('./xform/drawing/drawing-xform');
2121
const TableXform = require('./xform/table/table-xform');
22-
const PivotCacheRecordsXform = require('./xform/pivot-table/pivot-cache-records-xform');
23-
const PivotCacheDefinitionXform = require('./xform/pivot-table/pivot-cache-definition-xform');
24-
const PivotTableXform = require('./xform/pivot-table/pivot-table-xform');
2522
const CommentsXform = require('./xform/comment/comments-xform');
2623
const VmlNotesXform = require('./xform/comment/vml-notes-xform');
2724

@@ -474,71 +471,6 @@ class XLSX {
474471
});
475472
}
476473

477-
addPivotTables(zip, model) {
478-
if (!model.pivotTables.length) return;
479-
480-
const pivotTable = model.pivotTables[0];
481-
482-
const pivotCacheRecordsXform = new PivotCacheRecordsXform();
483-
const pivotCacheDefinitionXform = new PivotCacheDefinitionXform();
484-
const pivotTableXform = new PivotTableXform();
485-
const relsXform = new RelationshipsXform();
486-
487-
// pivot cache records
488-
// --------------------------------------------------
489-
// copy of the source data.
490-
//
491-
// Note: cells in the columns of the source data which are part
492-
// of the "rows" or "columns" of the pivot table configuration are
493-
// replaced by references to their __cache field__ identifiers.
494-
// See "pivot cache definition" below.
495-
496-
let xml = pivotCacheRecordsXform.toXml(pivotTable);
497-
zip.append(xml, {name: 'xl/pivotCache/pivotCacheRecords1.xml'});
498-
499-
// pivot cache definition
500-
// --------------------------------------------------
501-
// cache source (source data):
502-
// ref="A1:E7" on sheet="Sheet1"
503-
// cache fields:
504-
// - 0: "A" (a1, a2, a3)
505-
// - 1: "B" (b1, b2)
506-
// - ...
507-
508-
xml = pivotCacheDefinitionXform.toXml(pivotTable);
509-
zip.append(xml, {name: 'xl/pivotCache/pivotCacheDefinition1.xml'});
510-
511-
xml = relsXform.toXml([
512-
{
513-
Id: 'rId1',
514-
Type: XLSX.RelType.PivotCacheRecords,
515-
Target: 'pivotCacheRecords1.xml',
516-
},
517-
]);
518-
zip.append(xml, {name: 'xl/pivotCache/_rels/pivotCacheDefinition1.xml.rels'});
519-
520-
// pivot tables (on destination worksheet)
521-
// --------------------------------------------------
522-
// location: ref="A3:E15"
523-
// pivotFields
524-
// rowFields and rowItems
525-
// colFields and colItems
526-
// dataFields
527-
// pivotTableStyleInfo
528-
529-
xml = pivotTableXform.toXml(pivotTable);
530-
zip.append(xml, {name: 'xl/pivotTables/pivotTable1.xml'});
531-
532-
xml = relsXform.toXml([
533-
{
534-
Id: 'rId1',
535-
Type: XLSX.RelType.PivotCacheDefinition,
536-
Target: '../pivotCache/pivotCacheDefinition1.xml',
537-
},
538-
]);
539-
zip.append(xml, {name: 'xl/pivotTables/_rels/pivotTable1.xml.rels'});
540-
}
541-
542474
async addContentTypes(zip, model) {
543475
const xform = new ContentTypesXform();
544476
const xml = xform.toXml(model);
@@ -644,7 +576,9 @@ class XLSX {
644576
if (worksheet.rels && worksheet.rels.length) {
645577
xmlStream = new XmlStream();
646578
relationshipsXform.render(xmlStream, worksheet.rels);
647-
zip.append(xmlStream.xml, {name: `xl/worksheets/_rels/sheet${worksheet.id}.xml.rels`});
579+
zip.append(xmlStream.xml, {
580+
name: `xl/worksheets/_rels/sheet${worksheet.id}.xml.rels`,
581+
});
648582
}
649583

650584
if (worksheet.comments.length > 0) {
@@ -676,7 +610,8 @@ class XLSX {
676610
model.created = model.created || new Date();
677611
model.modified = model.modified || new Date();
678612

679-
model.useSharedStrings = options.useSharedStrings !== undefined ? options.useSharedStrings : true;
613+
model.useSharedStrings =
614+
options.useSharedStrings !== undefined ? options.useSharedStrings : true;
680615
model.useStyles = options.useStyles !== undefined ? options.useStyles : true;
681616

682617
// Manage the shared strings
@@ -733,7 +668,6 @@ class XLSX {
733668
await this.addSharedStrings(zip, model); // always after worksheets
734669
await this.addDrawings(zip, model);
735670
await this.addTables(zip, model);
736-
await this.addPivotTables(zip, model);
737671
await Promise.all([this.addThemes(zip, model), this.addStyles(zip, model)]);
738672
await this.addMedia(zip, model);
739673
await Promise.all([this.addApp(zip, model), this.addCore(zip, model)]);

0 commit comments

Comments
 (0)