Skip to content

Commit 677a1c5

Browse files
authored
Merge pull request #3 from zurmokeeper/feature/add_encryption_func
Feature/add encryption func
2 parents f9249c0 + 37df5de commit 677a1c5

File tree

11 files changed

+640
-10
lines changed

11 files changed

+640
-10
lines changed

README.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,44 @@ Reverse engineered from Excel spreadsheet files as a project.
1313
# Installation
1414

1515
```shell
16-
npm install exceljs
16+
npm install @zurmokeeper/exceljs
1717
```
1818

19+
# V4.4.1 New Features!
20+
21+
Change Log:
22+
<ul>
23+
<li>
24+
Add nested columns feature. Thank you <a href="https://github.com/jeka1985">jeka1985</a>, Merged <a href="https://github.com/exceljs/exceljs/pull/1889"> PR1889</a>.
25+
26+
Code snippets:
27+
28+
// new api: worksheet.makeColumns(headers)
29+
</li>
30+
<li>
31+
Add file encryption function.
32+
33+
Code snippets:
34+
35+
await workbook.xlsx.writeFile(filename, {password: '123456'});
36+
</li>
37+
</ul>
38+
39+
# V4.4.0 New Features!
40+
41+
Change Log:
42+
<ul>
43+
<li>
44+
Add decryption of excel files with password encryption (Support frontend and backend)
45+
46+
Code snippets:
47+
48+
// read from a stream, decrypt excel files encrypted with password
49+
// const workbook = new Excel.Workbook();
50+
// await workbook.xlsx.readFile(filename, {password:'123456'});
51+
</li>
52+
</ul>
53+
1954
# New Features!
2055

2156
<ul>
@@ -742,6 +777,26 @@ const newCol3Values = [1,2,3,4,5];
742777
const newCol4Values = ['one', 'two', 'three', 'four', 'five'];
743778
worksheet.spliceColumns(3, 1, newCol3Values, newCol4Values);
744779

780+
// Nested columns
781+
// Also you can build nested columns
782+
worksheet.makeColumns([
783+
{
784+
id: 1,
785+
title: 'Some',
786+
},
787+
{id: 2, title: 'Qwe'},
788+
{id: 3, title: 'Foo'},
789+
{
790+
id: 4,
791+
title: 'Zoo',
792+
children: [
793+
{ id: 41, title: 'Zoo 1' },
794+
{ id: 42, title: 'Zoo 2' },
795+
{ id: 44, title: 'Zoo 3' },
796+
{ id: 45, title: 'Zoo 4' },
797+
]
798+
}
799+
]);
745800
```
746801

747802
## Rows[](#contents)<!-- Link generated with jump2header -->
@@ -2193,11 +2248,21 @@ await workbook.xlsx.load(data, {password:'123456'});
21932248
const workbook = createAndFillWorkbook();
21942249
await workbook.xlsx.writeFile(filename);
21952250

2251+
// write to a file, with password encryption
2252+
const workbook = createAndFillWorkbook();
2253+
await workbook.xlsx.writeFile(filename, {password: '123456'});
2254+
21962255
// write to a stream
21972256
await workbook.xlsx.write(stream);
21982257

2258+
// write to a stream, with password encryption
2259+
await workbook.xlsx.write(stream, {password: '123456'}));
2260+
21992261
// write to a new buffer
22002262
const buffer = await workbook.xlsx.writeBuffer();
2263+
2264+
// write to a new buffer, with password encryption
2265+
const buffer = await workbook.xlsx.writeBuffer({password: '123456'});
22012266
```
22022267

22032268
### CSV[](#contents)<!-- Link generated with jump2header -->

README_zh.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,44 @@
1111
# 安装
1212

1313
```shell
14-
npm install exceljs
14+
npm install @zurmokeeper/exceljs
1515
```
1616

17+
# V4.4.1 新的功能!
18+
19+
变更日志:
20+
<ul>
21+
<li>
22+
新增嵌套列功能(多表头). 感谢 <a href="https://github.com/jeka1985">jeka1985</a>, Merged <a href="https://github.com/exceljs/exceljs/pull/1889"> PR1889</a>.
23+
24+
Code snippets:
25+
26+
// new api: worksheet.makeColumns(headers)
27+
</li>
28+
<li>
29+
新增导出加密Excel文件功能.
30+
31+
Code snippets:
32+
33+
await workbook.xlsx.writeFile(filename, {password: '123456'});
34+
</li>
35+
</ul>
36+
37+
# V4.4.0 新的功能!
38+
39+
变更日志:
40+
<ul>
41+
<li>
42+
新增读取加密Excel的功能 (支持前端和后端(Node.js))
43+
44+
Code snippets:
45+
46+
// read from a stream, decrypt excel files encrypted with password
47+
// const workbook = new Excel.Workbook();
48+
// await workbook.xlsx.readFile(filename, {password:'123456'});
49+
</li>
50+
</ul>
51+
1752
# 新的功能!
1853

1954
<ul>
@@ -689,6 +724,26 @@ const newCol3Values = [1,2,3,4,5];
689724
const newCol4Values = ['one', 'two', 'three', 'four', 'five'];
690725
worksheet.spliceColumns(3, 1, newCol3Values, newCol4Values);
691726

727+
// 嵌套列
728+
// 使用这个API来构建嵌套列(实现多级表头)
729+
worksheet.makeColumns([
730+
{
731+
id: 1,
732+
title: 'Some',
733+
},
734+
{id: 2, title: 'Qwe'},
735+
{id: 3, title: 'Foo'},
736+
{
737+
id: 4,
738+
title: 'Zoo',
739+
children: [
740+
{ id: 41, title: 'Zoo 1' },
741+
{ id: 42, title: 'Zoo 2' },
742+
{ id: 44, title: 'Zoo 3' },
743+
{ id: 45, title: 'Zoo 4' },
744+
]
745+
}
746+
]);
692747
```
693748

694749
## [](#目录)<!-- Link generated with jump2header -->
@@ -2083,11 +2138,21 @@ await workbook.xlsx.load(data, {password:'123456'});
20832138
const workbook = createAndFillWorkbook();
20842139
await workbook.xlsx.writeFile(filename);
20852140

2141+
// 写入文件, 带密码保护的
2142+
const workbook = createAndFillWorkbook();
2143+
await workbook.xlsx.writeFile(filename, {password: '123456'});
2144+
20862145
// 写入流
20872146
await workbook.xlsx.write(stream);
20882147

2148+
// 写入流, 带密码保护的
2149+
await workbook.xlsx.write(stream, {password: '123456'}));
2150+
20892151
// 写入 buffer
20902152
const buffer = await workbook.xlsx.writeBuffer();
2153+
2154+
// 写入 buffer, 带密码保护的
2155+
const buffer = await workbook.xlsx.writeBuffer({password: '123456'});
20912156
```
20922157

20932158
### CSV[](#目录)<!-- Link generated with jump2header -->

index.d.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,15 +1163,43 @@ export interface Worksheet {
11631163
* Cut one or more columns (columns to the right are shifted left)
11641164
* and optionally insert more
11651165
*
1166-
* If column properties have been definde, they will be cut or moved accordingly
1166+
* If column properties have been define, they will be cut or moved accordingly
11671167
*
11681168
* Known Issue: If a splice causes any merged cells to move, the results may be unpredictable
11691169
*
1170-
* Also: If the worksheet has more rows than values in the colulmn inserts,
1170+
* Also: If the worksheet has more rows than values in the column inserts,
11711171
* the rows will still be shifted as if the values existed
11721172
*/
11731173
spliceColumns(start: number, count: number, ...insert: any[][]): void;
11741174

1175+
/**
1176+
* Generate nested columns
1177+
* Example:
1178+
*
1179+
* const headers = [
1180+
* {id: 1,title: 'name'},
1181+
{id: 2, title: 'Qwe'},
1182+
{id: 3, title: 'Foo'},
1183+
{
1184+
id: 4,
1185+
title: 'ABC',
1186+
children: [
1187+
{id: 41, title: 'Zoo 1'},
1188+
{id: 42, title: 'Zoo 2'},
1189+
{id: 44, title: 'Zoo 3'},
1190+
],
1191+
}]
1192+
*
1193+
* const workbook = new ExcelJS.Workbook();
1194+
* const worksheet = workbook.addWorksheet('Sheet1');
1195+
* worksheet.makeColumns(headers);
1196+
*
1197+
*
1198+
* Note: This API allows you to quickly build an excel with multiple table headers.
1199+
* TODO: Can you set the background color together?
1200+
*/
1201+
makeColumns(input: any[][]): any[][];
1202+
11751203
/**
11761204
* Add column headers and define column keys and widths.
11771205
*
@@ -1451,6 +1479,12 @@ export interface XlsxWriteOptions extends stream.xlsx.WorkbookWriterOptions {
14511479
* The option passed to JsZip#generateAsync(options)
14521480
*/
14531481
zip: Partial<JSZipGeneratorOptions>;
1482+
1483+
/**
1484+
* @desc Password for decryption
1485+
* optional
1486+
*/
1487+
password: string;
14541488
}
14551489

14561490
export interface XlsxReadOptions {

lib/doc/worksheet.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const Table = require('./table');
1010
const DataValidations = require('./data-validations');
1111
const Encryptor = require('../utils/encryptor');
1212
const {copyStyle} = require('../utils/copy-style');
13+
const ColumnFlatter = require('../utils/column-flatter');
1314

1415
// Worksheet requirements
1516
// Operate as sheet inside workbook or standalone
@@ -922,6 +923,37 @@ class Worksheet {
922923
}, {});
923924
this.conditionalFormattings = value.conditionalFormattings;
924925
}
926+
927+
/**
928+
* @desc Generates a nested column
929+
*/
930+
makeColumns(input) {
931+
const flatter = new ColumnFlatter(input);
932+
const merges = flatter.getMerges();
933+
const rows = flatter.getRows();
934+
935+
this.columns = flatter.getColumns();
936+
937+
// For the time being, do not write dead freeze, can be set by the developers themselves
938+
// this.views.push({state: 'frozen', ySplit: rows.length});
939+
940+
this.addRows(
941+
rows.map(row => {
942+
return row.map(item => {
943+
if (!item) return null;
944+
if (item.title) return item.title;
945+
if (item.id) return item.id;
946+
return null;
947+
});
948+
})
949+
);
950+
951+
merges.forEach(item => {
952+
this.mergeCells(item);
953+
});
954+
955+
return {rows};
956+
}
925957
}
926958

927959
module.exports = Worksheet;

0 commit comments

Comments
 (0)