@@ -37,6 +37,18 @@ router.post('/lambda/json-to-excel/styled', async (req, res) => {
3737 }
3838} )
3939
40+ router . post ( '/lambda/json-to-excel/common-styled' , async ( req , res ) => {
41+ console . log ( 'styled working' )
42+ try {
43+ const jsonData = req . body . excel
44+ const excelData = await convertJsonToCommonStyledExcel ( jsonData )
45+ const url = await uploadToAWS ( req . body . config , excelData )
46+ return res . json ( { url } )
47+ } catch ( error ) {
48+ console . log ( 'error' , error )
49+ res . status ( 400 ) . json ( { message : 'error in your request payload' , error : error . message , rawError : error } )
50+ }
51+ } )
4052
4153router . post ( '/api/jsonToExcel' , async ( req , res ) => {
4254 try {
@@ -189,6 +201,52 @@ async function convertJsonToStyledExcel(jsonData, defaultStyle) {
189201 return buffer
190202}
191203
204+ async function convertJsonToCommonStyledExcel ( data ) {
205+ const workbook = new ExcelJS . Workbook ( ) ;
206+ for ( const [ sheetName , sheetRows ] of Object . entries ( data ) ) {
207+ const worksheet = workbook . addWorksheet ( sheetName ) ;
208+ sheetRows . forEach ( ( row , rowIndex ) => {
209+ let colIndex = 1 ;
210+ row . forEach ( cell => {
211+ const currentCell = worksheet . getCell ( rowIndex + 1 , colIndex ) ;
212+ currentCell . value = cell . value ;
213+ if ( cell . style ) {
214+ Object . assign ( currentCell . style , cell . style ) ;
215+ }
216+ if ( cell . colspan || cell . rowspan ) {
217+ const startRow = rowIndex + 1 ;
218+ const startCol = colIndex ;
219+ const endRow = startRow + ( cell . rowspan || 1 ) - 1 ;
220+ const endCol = startCol + ( cell . colspan || 1 ) - 1 ;
221+
222+ worksheet . mergeCells ( startRow , startCol , endRow , endCol ) ;
223+ colIndex += ( cell . colspan || 1 ) ;
224+ }
225+ else if ( cell . dropdown ) {
226+ const formulae = [ `"${ cell . dropdown . join ( "," ) } "` ] ;
227+ worksheet . getCell ( rowIndex + 1 , colIndex ) . dataValidation = {
228+ type : 'list' ,
229+ allowBlank : false ,
230+ formulae : formulae ,
231+ showErrorMessage : true ,
232+ errorTitle : 'Invalid Selection' ,
233+ error : 'Please select a value from the dropdown' ,
234+ } ;
235+ colIndex ++ ;
236+ } else {
237+ colIndex ++ ;
238+ }
239+ } ) ;
240+ } ) ;
241+ worksheet . columns . forEach ( column => {
242+ column . width = column . width ? column . width : 20 ; // Set a default width
243+ } ) ;
244+ }
245+ const buffer = await workbook . xlsx . writeBuffer ( ) ;
246+ return buffer
247+ }
248+
249+
192250// const servicePrefix = process.env.SERVICE_PREFIX || '/'
193251app . use ( '/' , router )
194252
0 commit comments