Rapport_React/src/views/horizonreport/exportXlsx.js

119 lines
4.3 KiB
JavaScript

const IDENTITY = [
'RBM/ZBM Name',
'Emp Code',
'Role Name',
'Position Name',
'Manager 1 Code',
'Manager 1 Name',
'Manager 2 Code',
'Manager 2 Name',
];
function dayLabel(iso) {
const [year, month, day] = String(iso).split('-');
return `${day}-${month}-${year}`;
}
function textCell(sheet, row, column, value) {
const cell = sheet.getCell(row, column);
cell.value = value === null || value === undefined ? '' : String(value);
cell.alignment = { vertical: 'middle', horizontal: 'center', wrapText: true };
return cell;
}
export async function exportFieldWorkReport(report, filename) {
const { default: ExcelJS } = await import('exceljs');
const workbook = new ExcelJS.Workbook();
const sheet = workbook.addWorksheet('Field Work');
const days = Array.isArray(report?.days) ? report.days : [];
const rows = Array.isArray(report?.rows) ? report.rows : [];
const identityCount = IDENTITY.length;
const lastColumn = identityCount + days.length * 2 + 1;
textCell(sheet, 1, 1, 'Monthly Field Work Report');
sheet.getCell(1, 1).font = { bold: true };
sheet.getCell(1, 1).alignment = { vertical: 'middle', horizontal: 'left' };
IDENTITY.forEach((label, index) => {
const column = index + 1;
textCell(sheet, 2, column, label);
sheet.mergeCells(2, column, 3, column);
sheet.getCell(2, column).font = { bold: true };
});
days.forEach((day, index) => {
const column = identityCount + index * 2 + 1;
textCell(sheet, 2, column, dayLabel(day));
sheet.mergeCells(2, column, 2, column + 1);
sheet.getCell(2, column).font = { bold: true };
textCell(sheet, 3, column, 'Link');
textCell(sheet, 3, column + 1, 'Time');
sheet.getCell(3, column).font = { bold: true };
sheet.getCell(3, column + 1).font = { bold: true };
});
textCell(sheet, 2, lastColumn, 'RBM/ZBM wise Total');
sheet.mergeCells(2, lastColumn, 3, lastColumn);
sheet.getCell(2, lastColumn).font = { bold: true };
rows.forEach((row, rowIndex) => {
const excelRow = rowIndex + 4;
const values = [
row.name,
row.employee_code,
row.role_name,
row.position_name,
row.manager_1_code,
row.manager_1_name,
row.manager_2_code,
row.manager_2_name,
];
values.forEach((value, index) => {
const cell = textCell(sheet, excelRow, index + 1, value);
if (index === 0 || index === 2 || index === 3 || index === 5 || index === 7) {
cell.alignment = { vertical: 'middle', horizontal: 'left' };
}
});
days.forEach((day, index) => {
const column = identityCount + index * 2 + 1;
const cell = row.days?.[day] || { status: 'blank', link: '', time: '' };
if (cell.status === 'link' && cell.link) {
const linkCell = sheet.getCell(excelRow, column);
linkCell.value = { text: 'Link', hyperlink: cell.link };
linkCell.font = { color: { argb: 'FF0563C1' }, underline: true };
linkCell.alignment = { vertical: 'middle', horizontal: 'center' };
textCell(sheet, excelRow, column + 1, cell.time || '');
} else if (cell.status === 'no') {
sheet.mergeCells(excelRow, column, excelRow, column + 1);
textCell(sheet, excelRow, column, 'No');
} else {
sheet.mergeCells(excelRow, column, excelRow, column + 1);
textCell(sheet, excelRow, column, '');
}
});
textCell(sheet, excelRow, lastColumn, row.total ?? 0);
});
if (rows.length) {
const totalRow = rows.length + 4;
const columnTotal = rows.reduce((sum, row) => sum + Number(row.total || 0), 0);
textCell(sheet, totalRow, 1, 'Total');
sheet.getCell(totalRow, 1).font = { bold: true };
sheet.getCell(totalRow, 1).alignment = { vertical: 'middle', horizontal: 'left' };
textCell(sheet, totalRow, lastColumn, columnTotal);
sheet.getCell(totalRow, lastColumn).font = { bold: true };
}
for (let column = 1; column <= lastColumn; column += 1) {
sheet.getColumn(column).width = column <= identityCount ? 22 : 14;
}
const buffer = await workbook.xlsx.writeBuffer();
const blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
const anchor = document.createElement('a');
anchor.href = URL.createObjectURL(blob);
anchor.download = filename;
document.body.appendChild(anchor);
anchor.click();
document.body.removeChild(anchor);
URL.revokeObjectURL(anchor.href);
}