feat: implement project export to Excel with API endpoint and UI button
This commit is contained in:
58
export-projects-to-excel.mjs
Normal file
58
export-projects-to-excel.mjs
Normal file
@@ -0,0 +1,58 @@
|
||||
import * as XLSX from 'xlsx';
|
||||
import { getAllProjects } from './src/lib/queries/projects.js';
|
||||
|
||||
function exportProjectsToExcel() {
|
||||
try {
|
||||
// Get all projects
|
||||
const projects = getAllProjects();
|
||||
|
||||
// Group projects by status
|
||||
const groupedProjects = projects.reduce((acc, project) => {
|
||||
const status = project.project_status || 'unknown';
|
||||
if (!acc[status]) {
|
||||
acc[status] = [];
|
||||
}
|
||||
acc[status].push({
|
||||
'Nazwa projektu': project.project_name,
|
||||
'Adres': project.address || '',
|
||||
'Działka': project.plot || '',
|
||||
'WP': project.wp || '',
|
||||
'Data zakończenia': project.finish_date || ''
|
||||
});
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
// Polish status translations for sheet names
|
||||
const statusTranslations = {
|
||||
'registered': 'Zarejestrowany',
|
||||
'in_progress_design': 'W realizacji (projektowanie)',
|
||||
'in_progress_construction': 'W realizacji (budowa)',
|
||||
'fulfilled': 'Zakończony',
|
||||
'cancelled': 'Wycofany',
|
||||
'unknown': 'Nieznany'
|
||||
};
|
||||
|
||||
// Create workbook
|
||||
const workbook = XLSX.utils.book_new();
|
||||
|
||||
// Create a sheet for each status
|
||||
Object.keys(groupedProjects).forEach(status => {
|
||||
const sheetName = statusTranslations[status] || status;
|
||||
const worksheet = XLSX.utils.json_to_sheet(groupedProjects[status]);
|
||||
XLSX.utils.book_append_sheet(workbook, worksheet, sheetName);
|
||||
});
|
||||
|
||||
// Write to file
|
||||
const filename = `projects_export_${new Date().toISOString().split('T')[0]}.xlsx`;
|
||||
XLSX.writeFile(workbook, filename);
|
||||
|
||||
console.log(`Excel file created: ${filename}`);
|
||||
console.log(`Sheets created for statuses: ${Object.keys(groupedProjects).join(', ')}`);
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error exporting projects to Excel:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// Run the export
|
||||
exportProjectsToExcel();
|
||||
Reference in New Issue
Block a user