@@ -9,11 +9,36 @@ import {
9
9
import { MergeProps } from "./types" ;
10
10
import { PaginatedObjectContentProps } from "./types/contents" ;
11
11
import {
12
+ ExcelAdvancedOptionsBase64 ,
13
+ ExcelAdvancedOptionsBuffer ,
14
+ ExcelAdvancedOptionsDownload ,
15
+ ExcelAdvancedOptionsFilePath ,
16
+ ExcelOptionsBase64 ,
17
+ ExcelOptionsBuffer ,
18
+ ExcelOptionsDownload ,
19
+ ExcelOptionsFilePath ,
12
20
ExportationType ,
21
+ ExportationTypeBase64 ,
22
+ ExportationTypeBuffer ,
23
+ ExportationTypeDownload ,
24
+ ExportationTypeFilePath ,
13
25
ExportMeExcelAdvancedProps ,
14
26
ExportMeExcelProps ,
15
27
} from "./types/functions" ;
16
28
29
+ const transformData = (
30
+ data : Record < string , any > [ ]
31
+ ) : PaginatedObjectContentProps [ ] => {
32
+ if (
33
+ ! ! data [ 0 ] ?. content &&
34
+ Array . isArray ( data [ 0 ] ?. content ) &&
35
+ ! ! data [ 0 ] ?. sheetName
36
+ )
37
+ return data as PaginatedObjectContentProps [ ] ;
38
+
39
+ return [ { content : data , sheetName : "Sheet 1" } ] ;
40
+ } ;
41
+
17
42
const executeXLSX = (
18
43
data : XLSX . CellObject [ ] [ ] ,
19
44
columnWidths ?: number [ ] ,
@@ -31,11 +56,11 @@ const executeXLSX = (
31
56
return ws ;
32
57
} ;
33
58
34
- const exportFile = (
59
+ function exportFile (
35
60
exportAs : ExportationType ,
36
61
wb : XLSX . WorkBook ,
37
62
fileName : string
38
- ) = > {
63
+ ) : Promise < string | ArrayBuffer | void > {
39
64
if ( exportAs . type === "base64" ) {
40
65
return XLSX . write ( wb , { type : "base64" , bookType : "xlsx" } ) ;
41
66
}
@@ -45,24 +70,39 @@ const exportFile = (
45
70
}
46
71
47
72
if ( exportAs . type === "download" ) {
48
- return XLSX . writeFile ( wb , `${ fileName } .xlsx` ) ;
73
+ XLSX . writeFile ( wb , `${ fileName } .xlsx` ) ;
74
+ return ;
49
75
}
50
76
51
77
if ( exportAs . type === "filepath" ) {
52
- return XLSX . writeFile ( wb , exportAs . path ) ;
78
+ XLSX . writeFile ( wb , exportAs . path ) ;
79
+ return ;
53
80
}
54
81
55
- return null ;
56
- } ;
57
-
58
- export const exportmeExcelAdvanced = ( {
82
+ return ;
83
+ }
84
+
85
+ export function exportmeExcelAdvanced (
86
+ options : ExcelAdvancedOptionsBase64
87
+ ) : Promise < string > ;
88
+ export function exportmeExcelAdvanced (
89
+ options : ExcelAdvancedOptionsBuffer
90
+ ) : Promise < ArrayBuffer > ;
91
+ export function exportmeExcelAdvanced (
92
+ options : ExcelAdvancedOptionsDownload
93
+ ) : Promise < void > ;
94
+ export function exportmeExcelAdvanced (
95
+ options : ExcelAdvancedOptionsFilePath
96
+ ) : Promise < void > ;
97
+
98
+ export function exportmeExcelAdvanced ( {
59
99
fileName,
60
100
data,
61
101
options,
62
102
exportAs,
63
103
merges,
64
104
loggingMatrix,
65
- } : ExportMeExcelAdvancedProps ) = > {
105
+ } : ExportMeExcelAdvancedProps ) : Promise < string | ArrayBuffer | void > {
66
106
const {
67
107
bodyStyle = { } ,
68
108
columnWidths = [ ] ,
@@ -103,28 +143,30 @@ export const exportmeExcelAdvanced = ({
103
143
console . info ( `💡 Excel-Ent:Logging-Matrix: ${ JSON . stringify ( rowsAdapter ) } ` ) ;
104
144
}
105
145
106
- return exportFile ( exportAs , wb , fileName ) ;
107
- } ;
108
-
109
- const transformData = (
110
- data : Record < string , any > [ ]
111
- ) : PaginatedObjectContentProps [ ] => {
112
- if (
113
- ! ! data [ 0 ] ?. content &&
114
- Array . isArray ( data [ 0 ] ?. content ) &&
115
- ! ! data [ 0 ] ?. sheetName
116
- )
117
- return data as PaginatedObjectContentProps [ ] ;
146
+ if ( exportAs . type === "filepath" ) {
147
+ return exportFile ( exportAs as ExportationTypeFilePath , wb , fileName ) ;
148
+ } else if ( exportAs . type === "download" ) {
149
+ return exportFile ( exportAs as ExportationTypeDownload , wb , fileName ) ;
150
+ } else if ( exportAs . type === "buffer" ) {
151
+ return exportFile ( exportAs as ExportationTypeBuffer , wb , fileName ) ;
152
+ } else {
153
+ return exportFile ( exportAs as ExportationTypeBase64 , wb , fileName ) ;
154
+ }
155
+ }
118
156
119
- return [ { content : data , sheetName : "Sheet 1" } ] ;
120
- } ;
157
+ export function exportmeExcel ( options : ExcelOptionsBase64 ) : Promise < string > ;
158
+ export function exportmeExcel (
159
+ options : ExcelOptionsBuffer
160
+ ) : Promise < ArrayBuffer > ;
161
+ export function exportmeExcel ( options : ExcelOptionsDownload ) : Promise < void > ;
162
+ export function exportmeExcel ( options : ExcelOptionsFilePath ) : Promise < void > ;
121
163
122
- export const exportmeExcel = ( {
164
+ export function exportmeExcel ( {
123
165
data,
124
166
fileName,
125
167
exportAs,
126
168
options,
127
- } : ExportMeExcelProps ) = > {
169
+ } : ExportMeExcelProps ) : Promise < string | ArrayBuffer | void > {
128
170
const {
129
171
bodyStyle = { } ,
130
172
columnWidths = [ ] ,
@@ -150,6 +192,7 @@ export const exportmeExcel = ({
150
192
( item : Record < string , any > , index : number ) =>
151
193
Object . keys ( item ) . map ( ( key ) => {
152
194
const isRowPainted = stripedRows && index % 2 === 0 ;
195
+
153
196
if ( typeof item [ key ] === "number" ) {
154
197
return {
155
198
v : item [ key ] ,
@@ -187,5 +230,13 @@ export const exportmeExcel = ({
187
230
188
231
wb . Props = sheetProps ;
189
232
190
- return exportFile ( exportAs , wb , fileName ) ;
191
- } ;
233
+ if ( exportAs . type === "filepath" ) {
234
+ return exportFile ( exportAs as ExportationTypeFilePath , wb , fileName ) ;
235
+ } else if ( exportAs . type === "download" ) {
236
+ return exportFile ( exportAs as ExportationTypeDownload , wb , fileName ) ;
237
+ } else if ( exportAs . type === "buffer" ) {
238
+ return exportFile ( exportAs as ExportationTypeBuffer , wb , fileName ) ;
239
+ } else {
240
+ return exportFile ( exportAs as ExportationTypeBase64 , wb , fileName ) ;
241
+ }
242
+ }
0 commit comments