@@ -95,8 +95,43 @@ async function convertJsonToStyledExcel(jsonData, defaultStyle) {
9595 } )
9696 }
9797
98- // Get column headers from the first row of data
99- const columnHeaders = jsonData [ sheetName ] . keysValue ? Object . values ( jsonData [ sheetName ] . keysValue ) . map ( ( keyValueObj ) => keyValueObj . value ) : Object . keys ( jsonData [ sheetName ] . data [ 0 ] )
98+ // Add subHeaders and corresponding subHeadersData after headers, if they exist
99+ if ( jsonData [ sheetName ] . subHeaders ) {
100+ const subHeaderValues = Object . values ( jsonData [ sheetName ] . subHeaders ) . map ( subHeaderObj => subHeaderObj . value ) ;
101+
102+ // Add subHeaders in the next row
103+ worksheet . addRow ( subHeaderValues ) ;
104+
105+ // Apply styles for subHeaders
106+ Object . values ( jsonData [ sheetName ] . subHeaders ) . forEach ( ( subHeaderObj , index ) => {
107+ const subHeaderCell = worksheet . getRow ( worksheet . rowCount ) . getCell ( index + 1 ) ; // Row number dynamically
108+ if ( subHeaderObj . style ) {
109+ subHeaderCell . style = subHeaderObj . style ;
110+ }
111+ } ) ;
112+
113+ // Now add the subHeadersData corresponding to each subHeader, if available
114+ if ( jsonData [ sheetName ] . subHeadersData ) {
115+ const subHeaderDataObject = jsonData [ sheetName ] . subHeadersData [ 0 ] ; // Access the first object in the array
116+ const subHeaderDataValues = Object . keys ( subHeaderDataObject ) . map ( key => {
117+ return subHeaderDataObject [ key ] . value ;
118+ } ) ;
119+ worksheet . addRow ( subHeaderDataValues ) ;
120+
121+ Object . keys ( jsonData [ sheetName ] . subHeadersData ) . forEach ( ( key , index ) => {
122+ const subHeaderDataCell = worksheet . getRow ( worksheet . rowCount ) . getCell ( index + 1 ) ;
123+ if ( jsonData [ sheetName ] . subHeadersData [ key ] . style ) {
124+ subHeaderDataCell . style = jsonData [ sheetName ] . subHeadersData [ key ] . style ;
125+ }
126+ } ) ;
127+ }
128+ }
129+
130+ // Now add the KeyValue headers from 'keysValue' (if available)
131+ const columnHeaders = jsonData [ sheetName ] . keysValue
132+ ? Object . values ( jsonData [ sheetName ] . keysValue ) . map ( ( keyValueObj ) => keyValueObj . value )
133+ : Object . keys ( jsonData [ sheetName ] . data [ 0 ] ) ;
134+
100135 const headerRow = worksheet . addRow ( columnHeaders )
101136
102137 // Apply styles from "keysValue" to the header row
0 commit comments