1
1
import * as fs from 'fs-extra' ;
2
2
import * as path from 'path' ;
3
3
import * as Excel from 'exceljs' ;
4
+ import { coral } from 'color-name' ;
4
5
5
6
export class ExcelFormatter {
6
7
private _workBook : Excel . Workbook = undefined ;
@@ -42,18 +43,42 @@ export class ExcelFormatter {
42
43
public async WriteFileToExcel ( header : Object [ ] , data : Object [ ] , sheetName : string , fileName : string ) : Promise < object > ;
43
44
public async WriteFileToExcel ( header : object [ ] , data : object [ ] , sheetName ?: string , fileName ?: string ) : Promise < object > {
44
45
try {
46
+ const time : string = `${ new Date ( ) . getFullYear ( ) } -${ new Date ( ) . getMonth ( ) } -${ new Date ( ) . getDate ( ) } -${ new Date ( ) . getMinutes ( ) } ` ;
45
47
sheetName = sheetName || 'default-sheet' ;
46
- fileName = `${ fileName } -output` || `excel-output` ;
48
+ fileName = `${ fileName } -output- ${ time } ` || `excel-output- ${ time } ` ;
47
49
// WorkSheet name must to be created
48
50
const workSheet = this . _workBook . addWorksheet ( sheetName ) ;
49
51
// define Header of all columns
50
52
workSheet . columns = header ;
51
53
// declare output Structure
52
54
this . SetWorkSheetHeader ( header ) ;
53
55
// Import data
54
- data . forEach ( ( item ) => {
55
- workSheet . addRow ( item ) ;
56
- } ) ;
56
+
57
+ for ( let i = 0 ; i < data . length ; i ++ ) {
58
+ workSheet . addRow ( data [ i ] ) ;
59
+
60
+ if ( data [ i ] [ 'image' ] ) {
61
+ const _image = data [ i ] [ 'image' ] ;
62
+ let image = this . _workBook . addImage ( {
63
+ base64 : `data:image/jpeg;base64, ${ _image } ` ,
64
+ extension : 'jpeg' ,
65
+ } ) ;
66
+ workSheet . addImage ( image , `C${ i + 2 } :C${ i + 2 } ` ) ;
67
+ } else if ( data [ i ] [ 'image1' ] && data [ i ] [ 'image2' ] ) {
68
+ const _imageFirst = data [ i ] [ 'image1' ] ;
69
+ const _imageSecond = data [ i ] [ 'image2' ] ;
70
+ let image1 = this . _workBook . addImage ( {
71
+ base64 : `data:image/jpeg;base64, ${ _imageFirst } ` ,
72
+ extension : 'jpeg' ,
73
+ } ) ;
74
+ let image2 = this . _workBook . addImage ( {
75
+ base64 : `data:image/jpeg;base64, ${ _imageSecond } ` ,
76
+ extension : 'jpeg' ,
77
+ } ) ;
78
+ workSheet . addImage ( image1 , `C${ i + 2 } :C${ i + 2 } ` ) ;
79
+ workSheet . addImage ( image2 , `D${ i + 2 } :D${ i + 2 } ` ) ;
80
+ }
81
+ }
57
82
58
83
await this . _workBook . xlsx . writeFile ( `${ fileName } .xlsx` ) ;
59
84
@@ -71,15 +96,21 @@ export class ExcelFormatter {
71
96
* @param {string } - workbook name
72
97
* @param {string } - worksheet name
73
98
*/
74
- public async ReadFileFromExcel ( fileName : string , sheetName : string ) : Promise < object [ ] > {
99
+ public async ReadFileFromExcel ( fileName : string , sheetName : string , headers ?: Object [ ] ) : Promise < object [ ] > {
75
100
try {
76
101
// find and load workbook first
77
102
const _path : string = path . join ( __dirname , `../../${ fileName } .xlsx` ) ;
78
103
const wb = await this . _workBook . xlsx . readFile ( _path ) ;
79
104
// import sheet
80
105
const sheet = wb . getWorksheet ( sheetName ) ;
81
106
// data processing define
82
- const map = this . GetWorkSheetHeader ( ) ;
107
+ let map = this . GetWorkSheetHeader ( ) ;
108
+
109
+ if ( Object . keys ( map ) . length <= 0 ) {
110
+ this . SetWorkSheetHeader ( headers ) ;
111
+ map = this . GetWorkSheetHeader ( ) ;
112
+ }
113
+
83
114
const mapKeys : string [ ] = Object . keys ( map ) ;
84
115
let cols = [ ] ;
85
116
let results = [ ] ;
@@ -92,12 +123,12 @@ export class ExcelFormatter {
92
123
} ) ;
93
124
94
125
cols . forEach ( ( col , index , array ) => {
95
- // remove default undefined where generated by exceljs
96
- col . slice ( 1 ) ;
126
+ let newCols = col . slice ( 1 ) ;
97
127
// map data
98
128
let temp = { } ;
99
- col . forEach ( ( val , index , array ) => {
100
- temp [ mapKeys [ index - 1 ] ] = val ;
129
+ newCols . forEach ( ( val , index , array ) => {
130
+ // remove default undefined where generated by exceljs
131
+ temp [ mapKeys [ index ] ] = val ;
101
132
} ) ;
102
133
results . push ( temp ) ;
103
134
} ) ;
0 commit comments