1- import { utils , WorkBook , WorkSheet , write , writeFile } from 'xlsx'
2- import { IColumn , IContent , IJsonSheet , IJsonSheetRow , ISettings , IWorksheetColumnWidth } from './types'
3-
4- function getContentProperty ( content : IContent , property : string ) : string | number | boolean | Date | IContent {
5-
6- function accessContentProperties ( content : IContent , properties : string [ ] ) : string | number | boolean | Date | IContent {
1+ import { utils , WorkBook , WorkSheet , write , writeFile } from 'xlsx'
2+ import { IColumn , IContent , IJsonSheet , IJsonSheetRow , ISettings , IWorksheetColumnWidth } from './types'
73
4+ function getContentProperty ( content : IContent , property : string ) : string | number | boolean | Date | IContent {
5+ function accessContentProperties ( content : IContent , properties : string [ ] ) : string | number | boolean | Date | IContent {
86 const value = content [ properties [ 0 ] ]
97
108 if ( properties . length === 1 ) {
11- return value ?? ""
9+ return value ?? ''
1210 }
1311
1412 if ( value === undefined || typeof value === 'string' || typeof value === 'boolean' ||
15- typeof value === 'number' || value instanceof Date ) {
16- return ""
13+ typeof value === 'number' || value instanceof Date ) {
14+ return ''
1715 }
1816
1917 return accessContentProperties ( value , properties . slice ( 1 ) )
2018 }
2119
22- const properties = property . split ( "." )
20+ const properties = property . split ( '.' )
2321 return accessContentProperties ( content , properties )
2422}
2523
26- function getJsonSheetRow ( content : IContent , columns : IColumn [ ] ) : IJsonSheetRow {
27-
28- let jsonSheetRow : IJsonSheetRow = { }
24+ function getJsonSheetRow ( content : IContent , columns : IColumn [ ] ) : IJsonSheetRow {
25+ const jsonSheetRow : IJsonSheetRow = { }
2926 columns . forEach ( ( column ) => {
30- if ( typeof column . value === " function" ) {
27+ if ( typeof column . value === ' function' ) {
3128 jsonSheetRow [ column . label ] = column . value ( content )
3229 } else {
3330 jsonSheetRow [ column . label ] = getContentProperty ( content , column . value )
@@ -36,21 +33,19 @@ function getJsonSheetRow(content: IContent, columns: IColumn[]): IJsonSheetRow {
3633 return jsonSheetRow
3734}
3835
39- function getWorksheetColumnWidths ( worksheet : WorkSheet , extraLength : number = 1 ) : IWorksheetColumnWidth [ ] {
40-
36+ function getWorksheetColumnWidths ( worksheet : WorkSheet , extraLength : number = 1 ) : IWorksheetColumnWidth [ ] {
4137 const columnRange = utils . decode_range ( worksheet [ '!ref' ] ?? '' )
4238
4339 // Column letters present in the workbook, e.g. A, B, C
44- let columnLetters : string [ ] = [ ]
40+ const columnLetters : string [ ] = [ ]
4541 for ( let C = columnRange . s . c ; C <= columnRange . e . c ; C ++ ) {
4642 const address = utils . encode_col ( C )
4743 columnLetters . push ( address )
4844 }
4945
5046 return columnLetters . map ( ( column ) => {
51-
5247 // Cells that belong to this column
53- let columnCells : string [ ] = Object . keys ( worksheet ) . filter ( ( cell ) => {
48+ const columnCells : string [ ] = Object . keys ( worksheet ) . filter ( ( cell ) => {
5449 return cell . charAt ( 0 ) === column
5550 } )
5651
@@ -59,12 +54,11 @@ function getWorksheetColumnWidths(worksheet: WorkSheet, extraLength: number = 1)
5954 ? previousCell : currentCell
6055 } )
6156
62- return { width : worksheet [ maxWidthCell ] . v . length + extraLength }
57+ return { width : worksheet [ maxWidthCell ] . v . length + extraLength }
6358 } )
6459}
6560
66- function getWorksheet ( jsonSheet : IJsonSheet , settings : ISettings ) : WorkSheet {
67-
61+ function getWorksheet ( jsonSheet : IJsonSheet , settings : ISettings ) : WorkSheet {
6862 const jsonSheetRows = jsonSheet . content . map ( ( contentItem ) => {
6963 return getJsonSheetRow ( contentItem , jsonSheet . columns )
7064 } )
@@ -75,18 +69,16 @@ function getWorksheet(jsonSheet: IJsonSheet, settings: ISettings): WorkSheet {
7569 return worksheet
7670}
7771
78- function writeWorkbook ( workbook : WorkBook , settings : ISettings = { } ) : Buffer | undefined {
79-
72+ function writeWorkbook ( workbook : WorkBook , settings : ISettings = { } ) : Buffer | undefined {
8073 const filename = `${ settings . fileName ?? 'Spreadsheet' } .xlsx`
8174 const writeOptions = settings . writeOptions ?? { }
8275
8376 return writeOptions . type === 'buffer' ? write ( workbook , writeOptions )
8477 : writeFile ( workbook , filename , writeOptions )
8578}
8679
87- function xlsx ( jsonSheets : IJsonSheet [ ] , settings : ISettings = { } ) : Buffer | undefined {
88-
89- if ( ! jsonSheets . length ) {
80+ function xlsx ( jsonSheets : IJsonSheet [ ] , settings : ISettings = { } ) : Buffer | undefined {
81+ if ( jsonSheets . length === 0 ) {
9082 return
9183 }
9284
@@ -102,7 +94,7 @@ function xlsx(jsonSheets: IJsonSheet[], settings: ISettings = {}): Buffer | unde
10294}
10395
10496export default xlsx
105- export { getContentProperty , getJsonSheetRow , getWorksheetColumnWidths }
97+ export { getContentProperty , getJsonSheetRow , getWorksheetColumnWidths }
10698module . exports = xlsx
10799module . exports . getContentProperty = getContentProperty
108100module . exports . getJsonSheetRow = getJsonSheetRow
0 commit comments