penambahan export pada cost management dan summary detail

This commit is contained in:
2024-06-28 06:42:42 +07:00
parent 8e0a1abcda
commit 99391c88bb
7 changed files with 1369 additions and 1279 deletions

View File

@@ -84,4 +84,70 @@ export class TableexcelService {
});
this.saveAsExcelFile(excelBuffer, excelFileName);
}
public exportAsExcelFileCostManage(
json: any[],
excelFileName: string,
columns: string[]
): void {
// Filter the json data based on the columns
const filteredJson = json.map((item) => {
const filteredItem = {};
columns.forEach((column) => {
filteredItem[column] = item[column];
});
return filteredItem;
});
// Calculate totals for estimation_cost and total_use
const totalEstimationCost = filteredJson.reduce((sum, item) => sum + (item['estimation_cost'] || 0), 0);
const totalUse = filteredJson.reduce((sum, item) => sum + (item['total_use'] || 0), 0);
// Create the worksheet
const worksheet: XLSX.WorkSheet = XLSX.utils.json_to_sheet(filteredJson);
const columnWidths = [
{ wch: 40 },
{ wch: 30 },
{ wch: 20 },
{ wch: 30 },
{ wch: 30 },
{ wch: 30 },
];
worksheet["!cols"] = columnWidths;
// Add header
const header = [
"Building",
"Room",
"Category",
"Total Use",
"Estimation Cost",
"Date",
];
XLSX.utils.sheet_add_aoa(worksheet, [header]);
// Add totals row
const totalsRow = [
"Totals",
"",
"",
`Total: ${totalUse} Kwh`,
`Total: ${totalEstimationCost}`,
""
];
XLSX.utils.sheet_add_aoa(worksheet, [totalsRow], { origin: -1 });
// Create the workbook
const workbook: XLSX.WorkBook = {
Sheets: { data: worksheet },
SheetNames: ["data"],
};
// Write the workbook and save it
const excelBuffer: any = XLSX.write(workbook, {
bookType: "xlsx",
type: "array",
});
this.saveAsExcelFile(excelBuffer, excelFileName);
}
}