1- import { stream } from 'exceljs' ;
1+ import { stream , Style , Workbook } from 'exceljs' ;
22import { isNil , isString , merge } from 'lodash' ;
33
44import { 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
715export 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
0 commit comments