Skip to content

Commit d310b38

Browse files
committed
improve type
1 parent 343d3b3 commit d310b38

File tree

5 files changed

+53
-18
lines changed

5 files changed

+53
-18
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
122122

123123
## Author
124124

125-
Nguyen Van A - [@nguyenvana](https://github.com/nguyenvana)
125+
Pham Van Nguyen - [@npv2k1](https://github.com/npv2k1)
126126

127127
## Support
128128

example/index.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ async function basicExample() {
99
useSharedStrings: true,
1010
};
1111
const workbook = new stream.xlsx.WorkbookWriter(options);
12-
const cursor = new ExcelCursor(workbook, 'Basic Example');
12+
const cursor = new ExcelCursor({
13+
workbook,
14+
sheetName: 'Basic Data Input',
15+
});
1316

1417
cursor
1518
.move('A1')
@@ -32,15 +35,18 @@ async function formattingExample() {
3235
useSharedStrings: true,
3336
};
3437
const workbook = new stream.xlsx.WorkbookWriter(options);
35-
const cursor = new ExcelCursor(workbook, 'Formatting');
38+
const cursor = new ExcelCursor({
39+
workbook,
40+
sheetName: 'Formatting Example',
41+
});
3642

3743
cursor
3844
.move('A1')
3945
.setData('Formatting Example')
4046
.formatCell({
4147
font: { bold: true, size: 14 },
4248
alignment: { horizontal: 'center' },
43-
fill: { type: 'pattern', pattern: 'solid', fgColor: { argb: 'FF90CAF9' } }
49+
fill: { type: 'pattern', pattern: 'solid', fgColor: { argb: 'FF90CAF9' } },
4450
})
4551
.nextRow(2)
4652
.setData('Bold Text')
@@ -63,7 +69,10 @@ async function formulasExample() {
6369
useSharedStrings: true,
6470
};
6571
const workbook = new stream.xlsx.WorkbookWriter(options);
66-
const cursor = new ExcelCursor(workbook, 'Formulas');
72+
const cursor = new ExcelCursor({
73+
workbook,
74+
sheetName: 'Formulas and Comments',
75+
});
6776

6877
// Set up some numbers
6978
cursor

src/core/ExcelCursor.ts

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,42 @@
1-
import { stream } from 'exceljs';
1+
import { stream, Style, Workbook } from 'exceljs';
22
import { isNil, isString, merge } from 'lodash';
33

44
import { parseAddress, positionToAddress } from '../helpers/excel.helper';
5-
import { Cell, CellFormat, CellPosition, Workbook, Worksheet } from '../types';
5+
import { Cell, CellFormat, CellPosition, Worksheet } from '../types';
6+
import * as os from 'os';
7+
import * as path from 'path';
8+
export type ExcelCursorOptions = {
9+
filename?: string;
10+
workbook?: stream.xlsx.WorkbookWriter | Workbook;
11+
sheetName?: string;
12+
isStream?: boolean;
13+
};
614

715
export class ExcelCursor {
8-
private workbook: stream.xlsx.WorkbookWriter;
16+
private workbook: stream.xlsx.WorkbookWriter | Workbook;
917
private worksheet: Worksheet;
1018
private position: CellPosition = { row: 1, col: 1 };
1119
private lastRow = 1;
1220
private lastCol = 1;
1321

14-
constructor(workbook: stream.xlsx.WorkbookWriter, sheetName?: string) {
15-
this.workbook = workbook;
22+
constructor(options?: ExcelCursorOptions) {
23+
const { workbook, sheetName, filename, isStream } = options ?? {};
24+
this.workbook = new Workbook();
25+
26+
if (workbook) {
27+
this.workbook = workbook;
28+
} else if (isStream) {
29+
this.workbook = new stream.xlsx.WorkbookWriter({
30+
filename: filename || path.join(os.tmpdir(), `excel-cursor-stream-${Date.now()}.xlsx`),
31+
useStyles: true,
32+
useSharedStrings: true,
33+
});
34+
}
35+
36+
// Reset position and tracking
37+
this.position = { row: 1, col: 1 };
38+
this.lastRow = 1;
39+
this.lastCol = 1;
1640

1741
if (sheetName && this.workbook.getWorksheet(sheetName)) {
1842
this.worksheet = this.workbook.getWorksheet(sheetName);
@@ -164,7 +188,7 @@ export class ExcelCursor {
164188
}
165189

166190
// Format ô hiện tại hoặc ô có địa chỉ bất kỳ
167-
formatCell(format: any, address?: string): ExcelCursor {
191+
formatCell(format: Partial<Style>, address?: string): ExcelCursor {
168192
const cell = this.getCell(address);
169193

170194
if (!cell.style) {
@@ -307,7 +331,9 @@ export class ExcelCursor {
307331
}
308332

309333
async commit(): Promise<void> {
310-
await this.workbook.commit();
334+
if (this.workbook instanceof stream.xlsx.WorkbookWriter) {
335+
await this.workbook.commit();
336+
}
311337
}
312338

313339
// Method hỗ trợ tạo vùng từ vị trí hiện tại với n hàng và m cột
@@ -327,7 +353,7 @@ export class ExcelCursor {
327353
}
328354

329355
// Áp dụng style cho vùng
330-
applyStyleToRange(format: CellFormat, startAddress: string, endAddress: string): ExcelCursor {
356+
applyStyleToRange(format: Partial<Style>, startAddress: string, endAddress: string): ExcelCursor {
331357
const startPos = this.parseAddress(startAddress);
332358
const endPos = this.parseAddress(endAddress);
333359

src/types/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Cell, Column, Row, Workbook, Worksheet } from 'exceljs';
1+
import { Cell, Column, Row, Worksheet } from 'exceljs';
22

33
export interface CellPosition {
44
row: number;
@@ -34,4 +34,4 @@ export interface CellFormat {
3434
numFmt?: string;
3535
}
3636

37-
export type { Cell, Column, Row, Workbook, Worksheet };
37+
export type { Cell, Column, Row, Worksheet };

tsconfig.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"jsx": "react",
3+
// "jsx": "react",
44
"module": "CommonJS",
55
"target": "es2022",
66
"declaration": true,
@@ -12,7 +12,7 @@
1212
"sourceMap": true,
1313
"outDir": "./dist",
1414
"moduleResolution": "node",
15-
"baseUrl": "./",
15+
"baseUrl": "./src",
1616
"incremental": true,
1717
"skipLibCheck": true,
1818
"strictNullChecks": false,
@@ -27,6 +27,6 @@
2727
"ts-node": {
2828
"swc": true
2929
},
30-
"include": ["**/*.ts", "**/*.tsx"],
30+
"include": ["src/**/*.ts", "src/**/*.tsx"],
3131
"exclude": ["node_modules", "dist"]
3232
}

0 commit comments

Comments
 (0)