Skip to content

Commit 85cdc3a

Browse files
common styled format added
1 parent ef37e19 commit 85cdc3a

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/index.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

4153
router.post('/api/jsonToExcel', async (req, res) => {
4254
try {
@@ -189,6 +201,41 @@ 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+
224+
colIndex += cell.colspan || 1;
225+
} else {
226+
colIndex++;
227+
}
228+
});
229+
});
230+
worksheet.columns.forEach(column => {
231+
column.width = 20; // Set a default width
232+
});
233+
}
234+
const buffer = await workbook.xlsx.writeBuffer()
235+
return buffer
236+
}
237+
238+
192239
// const servicePrefix = process.env.SERVICE_PREFIX || '/'
193240
app.use('/', router)
194241

0 commit comments

Comments
 (0)