@@ -87,23 +87,27 @@ async function convertJsonToStyledExcel(jsonData, defaultStyle) {
8787 const workbook = new ExcelJS . Workbook ( )
8888 Object . keys ( jsonData ) . forEach ( ( sheetName ) => {
8989 const worksheet = workbook . addWorksheet ( sheetName )
90- Object . entries ( jsonData [ sheetName ] . headers ) . forEach ( ( [ headerText , style ] ) => {
91- const headerRow = worksheet . addRow ( [ headerText ] )
92- const headerStyle = style
93- headerRow . getCell ( 1 ) . style = headerStyle
94- } )
90+ if ( jsonData [ sheetName ] . headers ) {
91+ Object . entries ( jsonData [ sheetName ] . headers ) . forEach ( ( [ headerText , style ] ) => {
92+ const headerRow = worksheet . addRow ( [ headerText ] )
93+ const headerStyle = style
94+ headerRow . getCell ( 1 ) . style = headerStyle
95+ } )
96+ }
9597
9698 // Get column headers from the first row of data
97- const columnHeaders = Object . values ( jsonData [ sheetName ] . keysValue ) . map ( ( keyValueObj ) => keyValueObj . value )
99+ const columnHeaders = jsonData [ sheetName ] . keysValue ? Object . values ( jsonData [ sheetName ] . keysValue ) . map ( ( keyValueObj ) => keyValueObj . value ) : Object . keys ( jsonData [ sheetName ] . data [ 0 ] )
98100 const headerRow = worksheet . addRow ( columnHeaders )
99101
100102 // Apply styles from "keysValue" to the header row
101- Object . values ( jsonData [ sheetName ] . keysValue ) . forEach ( ( keyValueObj , index ) => {
102- const cell = headerRow . getCell ( index + 1 )
103- if ( keyValueObj . style ) {
104- cell . style = keyValueObj . style
105- }
106- } )
103+ if ( jsonData [ sheetName ] . keysValue ) {
104+ Object . values ( jsonData [ sheetName ] . keysValue ) . forEach ( ( keyValueObj , index ) => {
105+ const cell = headerRow . getCell ( index + 1 )
106+ if ( keyValueObj . style ) {
107+ cell . style = keyValueObj . style
108+ }
109+ } )
110+ }
107111
108112 const headers = Object . keys ( jsonData [ sheetName ] . data [ 0 ] )
109113 // Iterate over rows from the JSON data
@@ -118,10 +122,31 @@ async function convertJsonToStyledExcel(jsonData, defaultStyle) {
118122 ...row [ key ] . style ,
119123 }
120124 }
125+ if ( row [ key ] . input ) {
126+ const formulae = row [ key ] . input . options ? [ `"${ row [ key ] . input . options . join ( ',' ) } "` ] : [ ] ;
127+ const validationType = row [ key ] . input . type || '' ;
128+
129+ const dropdownColumnIndex = colIndex + 1 ;
130+ // Apply data validation for all rows in this column (from the second row onwards)
131+ worksheet . getColumn ( dropdownColumnIndex ) . eachCell ( { includeEmpty : true } , ( cell , rowNumber ) => {
132+ if ( rowNumber > 1 ) { // Skip the header row
133+ cell . dataValidation = {
134+ type : validationType ,
135+ allowBlank : false ,
136+ formulae : formulae ,
137+ showErrorMessage : true ,
138+ errorTitle : 'Invalid Selection' ,
139+ error : 'Please select a value from the dropdown' ,
140+ } ;
141+ }
142+ } ) ;
143+ }
144+
121145 } )
122146 } )
147+
123148 worksheet . columns . forEach ( ( column ) => {
124- column . width = defaultStyle . width || 10
149+ column . width = defaultStyle ? defaultStyle . width : 10
125150 } )
126151 } )
127152
0 commit comments