Skip to content

Commit 0120757

Browse files
Merge pull request #24 from zurmokeeper/feature/add_ignore_node_func
Add the option to ignore node when read excel func
2 parents 37272c4 + ffb4f85 commit 0120757

File tree

7 files changed

+112
-4
lines changed

7 files changed

+112
-4
lines changed

README.md

+25
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ Read, manipulate and write spreadsheet data and styles to XLSX and JSON.
66

77
Reverse engineered from Excel spreadsheet files as a project.
88

9+
# Special thanks
10+
11+
The project code is forked from exceljs, based on the last commit on 2023-5-5, [commitid](https://github.com/exceljs/exceljs/commit/ ec92cb3b898bdf7f806ff9d7b8370c955ee8ba20), Since the latest version of exceljs is v4.3.0, sincere thanks to all the developers of the exceljs project.
12+
13+
@zurmokeeper/exceljs is compatible with exceljs V4.3.0 and previous versions, so feel free to switch to it.
14+
915
# Translations
1016

1117
* [中文文档](README_zh.md)
@@ -2254,6 +2260,16 @@ faster or more resilient.
22542260

22552261
#### Reading XLSX[](#contents)<!-- Link generated with jump2header -->
22562262

2263+
Options supported when reading xlsx files.
2264+
2265+
| Field | Required | Type |Description |
2266+
| ---------------- | ----------- | ----------- | ----------- |
2267+
| ignoreNodes | N | array | A list of node names to ignore while loading the XLSX document. Improves performance in some situations. <br/> Available: `sheetPr`, `dimension`, `sheetViews `, `sheetFormatPr`, `cols `, `sheetData`, `autoFilter `, `mergeCells `, `rowBreaks`, `hyperlinks `, `pageMargins`, `dataValidations`, `pageSetup`, `headerFooter `, `printOptions `, `picture`, `drawing`, `sheetProtection`, `tableParts `, `conditionalFormatting`, `extLst`,|
2268+
| password | N | string | Decrypted passwords, maximum length is 255. |
2269+
| base64 | N | boolean | This parameter indicates that the input is a base64-encoded buffer. Only valid for the load method. |
2270+
| maxRows | N | number | TODO:. |
2271+
| maxCols | N | number | TODO:. |
2272+
22572273
```javascript
22582274
// read from a file
22592275
const workbook = new Excel.Workbook();
@@ -2262,6 +2278,9 @@ await workbook.xlsx.readFile(filename);
22622278
// read from a file, decrypt excel files encrypted with password
22632279
const workbook = new Excel.Workbook();
22642280
await workbook.xlsx.readFile(filename, {password:'123456'});
2281+
await workbook.xlsx.readFile(filename, {
2282+
ignoreNodes:['dataValidations'] // ignores the workbook's Data Validations
2283+
});
22652284
// ... use workbook
22662285

22672286

@@ -2272,6 +2291,9 @@ await workbook.xlsx.read(stream);
22722291
// read from a stream, decrypt excel files encrypted with password
22732292
const workbook = new Excel.Workbook();
22742293
await workbook.xlsx.read(stream, {password:'123456'});
2294+
await workbook.xlsx.read(stream, {
2295+
ignoreNodes:['dataValidations'] // ignores the workbook's Data Validations
2296+
});
22752297
// ... use workbook
22762298

22772299

@@ -2282,6 +2304,9 @@ await workbook.xlsx.load(data);
22822304
// load from buffer, decrypt excel files encrypted with password
22832305
const workbook = new Excel.Workbook();
22842306
await workbook.xlsx.load(data, {password:'123456'});
2307+
await workbook.xlsx.load(data, {
2308+
ignoreNodes:['dataValidations'] // ignores the workbook's Data Validations
2309+
});
22852310
// ... use workbook
22862311
```
22872312

README_zh.md

+26
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88

99
一个 Excel 电子表格文件逆向工程项目。
1010

11+
# 特别鸣谢
12+
13+
此项目代码基于 exceljs 2023-5-5 的最后一次提交,
14+
[commitid](https://github.com/exceljs/exceljs/commit/ ec92cb3b898bdf7f806ff9d7b8370c955ee8ba20), exceljs的最新版本是V4.3.0,感谢exceljs项目所有的开发者
15+
16+
@zurmokeeper/exceljs 兼容 exceljs V4.3.0和之前的版本,可以放心切换使用。
17+
1118
# 安装
1219

1320
```shell
@@ -2144,6 +2151,16 @@ worksheet.unprotect();
21442151

21452152
#### 读 XLSX[](#目录)<!-- Link generated with jump2header -->
21462153

2154+
读取xlsx文件的可选参数Option如下
2155+
2156+
| Field | Required | Type |Description |
2157+
| ---------------- | ----------- | ----------- | ----------- |
2158+
| ignoreNodes | N | array | 读取xlsx文件时要忽略的xml节点数组,用于提高性能. <br/> 节点名有这些: `sheetPr`, `dimension`, `sheetViews `, `sheetFormatPr`, `cols `, `sheetData`, `autoFilter `, `mergeCells `, `rowBreaks`, `hyperlinks `, `pageMargins`, `dataValidations`, `pageSetup`, `headerFooter `, `printOptions `, `picture`, `drawing`, `sheetProtection`, `tableParts `, `conditionalFormatting`, `extLst`,|
2159+
| password | N | string | 解密用的密码,最大长度是255个字符. |
2160+
| base64 | N | boolean | 传入的是否是base64编码的内容,只在load方法时有这个参数. |
2161+
| maxRows | N | number | TODO:. |
2162+
| maxCols | N | number | TODO:. |
2163+
21472164
```javascript
21482165
// 从文件读取
21492166
const workbook = new Excel.Workbook();
@@ -2152,6 +2169,9 @@ await workbook.xlsx.readFile(filename);
21522169
// 从文件读取, 解密使用密码加密的excel文件
21532170
const workbook = new Excel.Workbook();
21542171
await workbook.xlsx.readFile(filename, {password:'123456'});
2172+
await workbook.xlsx.readFile(filename, {
2173+
ignoreNodes:['dataValidations'] // 忽略工作簿的数据有限性验证节点
2174+
});
21552175
// ... 使用 workbook
21562176

21572177

@@ -2162,6 +2182,9 @@ await workbook.xlsx.read(stream);
21622182
// 从流读取, 解密使用密码加密的excel文件
21632183
const workbook = new Excel.Workbook();
21642184
await workbook.xlsx.read(stream, {password:'123456'});
2185+
await workbook.xlsx.read(stream, {
2186+
ignoreNodes:['dataValidations'] // 忽略工作簿的数据有限性验证节点
2187+
});
21652188
// ... 使用 workbook
21662189

21672190

@@ -2172,6 +2195,9 @@ await workbook.xlsx.load(data);
21722195
// 从 buffer 加载, 解密使用密码加密的excel文件
21732196
const workbook = new Excel.Workbook();
21742197
await workbook.xlsx.load(data, {password:'123456'});
2198+
await workbook.xlsx.load(data, {
2199+
ignoreNodes:['dataValidations'] // 忽略工作簿的数据有限性验证节点
2200+
});
21752201
// ... 使用 workbook
21762202
```
21772203

index.d.ts

+17
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,23 @@ export interface XlsxReadOptions {
16141614
* optional
16151615
*/
16161616
maxCols: number;
1617+
1618+
/**
1619+
* @desc The list of XML node names to ignore while parsing an XLSX file
1620+
* optional
1621+
*
1622+
* Example:
1623+
*
1624+
* ignoreNodes: [
1625+
* 'dataValidations' // ignores the workbook's Data Validations
1626+
* ],
1627+
*
1628+
* Available: `sheetPr`, `dimension`, `sheetViews `, `sheetFormatPr`, `cols `,
1629+
* `sheetData`, `autoFilter `, `mergeCells `, `rowBreaks`, `hyperlinks `, `pageMargins`,
1630+
* `dataValidations`, `pageSetup`, `headerFooter `, `printOptions `, `picture`,
1631+
* `drawing`, `sheetProtection`, `tableParts `, `conditionalFormatting`, `extLst`
1632+
*/
1633+
ignoreNodes: string[];
16171634
}
16181635

16191636
export interface Xlsx {

lib/xlsx/xform/sheet/worksheet-xform.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ class WorkSheetXform extends BaseXform {
9393
constructor(options) {
9494
super();
9595

96-
const {maxRows, maxCols} = options || {};
96+
const {maxRows, maxCols, ignoreNodes} = options || {};
97+
this.ignoreNodes = ignoreNodes || [];
9798
this.map = {
9899
sheetPr: new SheetPropertiesXform(),
99100
dimension: new DimensionXform(),
@@ -377,8 +378,8 @@ class WorkSheetXform extends BaseXform {
377378
return true;
378379
}
379380

380-
this.parser = this.map[node.name];
381-
if (this.parser) {
381+
if (this.map[node.name] && !this.ignoreNodes.includes(node.name)) {
382+
this.parser = this.map[node.name];
382383
this.parser.parseOpen(node);
383384
}
384385
return true;

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zurmokeeper/exceljs",
3-
"version": "4.4.5",
3+
"version": "4.4.6",
44
"description": "Excel Workbook Manager - Read and Write xlsx and csv Files.",
55
"private": false,
66
"license": "MIT",
7.98 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const path = require('path');
2+
const fs = require('fs');
3+
4+
const ExcelJS = verquire('exceljs');
5+
const fileName = './spec/integration/data/test-new-issue-22.xlsx';
6+
7+
describe('github issues', () => {
8+
describe('new issue 22 - Memory overload when unnecessary dataValidations apply', () => {
9+
it('when using readFile', async () => {
10+
const wb = new ExcelJS.Workbook();
11+
await wb.xlsx.readFile(fileName, {
12+
ignoreNodes: ['dataValidations'],
13+
});
14+
15+
expect(true).to.equal(true);
16+
});
17+
18+
it('when loading an in memory buffer', async () => {
19+
const filePath = path.join(process.cwd(), fileName);
20+
const buffer = fs.readFileSync(filePath);
21+
const wb = new ExcelJS.Workbook();
22+
await wb.xlsx.load(buffer, {
23+
ignoreNodes: ['dataValidations'],
24+
});
25+
26+
expect(true).to.equal(true);
27+
});
28+
29+
it('when using read', async () => {
30+
const wb = new ExcelJS.Workbook();
31+
const input = fs.createReadStream(fileName);
32+
await wb.xlsx.read(input, {
33+
ignoreNodes: ['dataValidations'],
34+
});
35+
36+
expect(true).to.equal(true);
37+
});
38+
});
39+
});

0 commit comments

Comments
 (0)