feat: format numeric and price fields to 2 decimal places

This commit is contained in:
Christian Vidal Wolf
2026-03-29 14:46:05 +02:00
parent dd72b927c7
commit 034640c869
2 changed files with 112 additions and 9 deletions
+62 -3
View File
@@ -74,7 +74,35 @@ export default function App() {
const processedRows = rawRows.map(row => { const processedRows = rawRows.map(row => {
const articleNo = String(row[articleNoIdx]); const articleNo = String(row[articleNoIdx]);
return syncedData[articleNo] || row; const finalRow = syncedData[articleNo] || row;
// Format numeric/price fields to 2 decimal places
return finalRow.map((val, idx) => {
if (val === undefined || val === null || val === '') return val;
const header = (rawHeaders[idx] || '').toLowerCase();
// Skip Article No, Barcodes, and other code-like fields
if (header.includes('id') || header.includes('no') || header.includes('code') ||
header.includes('art.') || header.includes('barcode') || header.includes('article')) {
return val;
}
const priceKeywords = ['price', 'eur', 'cost', 'msrp', 'net', 'gross', 'netto', 'brutto', 'pp', 'pph', 'uvp', 'vpe', 'stk'];
const isPriceCol = priceKeywords.some(kw => header.includes(kw));
if (typeof val === 'number') {
return Number(val.toFixed(2));
}
if (typeof val === 'string') {
const normalized = val.trim().replace(',', '.');
const num = parseFloat(normalized);
if (!isNaN(num) && (isPriceCol || val.includes('.') || val.includes(','))) {
return num.toFixed(2);
}
}
return val;
});
}); });
setAppState({ setAppState({
@@ -117,9 +145,40 @@ export default function App() {
const data = XLSX.utils.sheet_to_json<any[]>(ws, { header: 1 }); const data = XLSX.utils.sheet_to_json<any[]>(ws, { header: 1 });
if (data.length > 0) { if (data.length > 0) {
const rawHeaders = data[0];
const rawRows = data.slice(1);
const processedRows = rawRows.map(row => {
return row.map((val, idx) => {
if (val === undefined || val === null || val === '') return val;
const header = (rawHeaders[idx] || '').toLowerCase();
if (header.includes('id') || header.includes('no') || header.includes('code') ||
header.includes('art.') || header.includes('barcode') || header.includes('article')) {
return val;
}
const priceKeywords = ['price', 'eur', 'cost', 'msrp', 'net', 'gross', 'netto', 'brutto', 'pp', 'pph', 'uvp', 'vpe', 'stk'];
const isPriceCol = priceKeywords.some(kw => header.includes(kw));
if (typeof val === 'number') {
return Number(val.toFixed(2));
}
if (typeof val === 'string') {
const normalized = val.trim().replace(',', '.');
const num = parseFloat(normalized);
if (!isNaN(num) && (isPriceCol || val.includes('.') || val.includes(','))) {
return num.toFixed(2);
}
}
return val;
});
});
setAppState({ setAppState({
headers: data[0], headers: rawHeaders,
data: data.slice(1), data: processedRows,
fileName: file.name, fileName: file.name,
fileDate: new Date(), fileDate: new Date(),
hasUnsavedChanges: false hasUnsavedChanges: false
+50 -6
View File
@@ -15,13 +15,50 @@ export function MatrixView({ data, headers }: MatrixViewProps) {
return data.slice(start, start + pageSize); return data.slice(start, start + pageSize);
}, [data, page, pageSize]); }, [data, page, pageSize]);
const formatCellValue = (val: any, header: string = '') => {
if (val === undefined || val === null || val === '') return '';
// Convert header to lowercase for checks
const h = header.toLowerCase();
// Do NOT format columns that are clearly IDs, barcodes, or codes
if (h.includes('id') || h.includes('no') || h.includes('code') || h.includes('art.') || h.includes('barcode') || h.includes('article')) {
return val;
}
// List of keywords that typically indicate a price or numeric value to format
const priceKeywords = ['price', 'eur', 'cost', 'msrp', 'net', 'gross', 'netto', 'brutto', 'pp', 'pph', 'uvp', 'vpe', 'stk'];
const isPriceCol = priceKeywords.some(kw => h.includes(kw));
// Handle numbers
if (typeof val === 'number') {
return val.toFixed(2);
}
// Handle strings that represent numbers
if (typeof val === 'string') {
// Normalize decimals (handle comma as decimal separator)
const normalized = val.trim().replace(',', '.');
const num = parseFloat(normalized);
if (!isNaN(num)) {
// Format if it's a price column OR if it already has a decimal separator (.)
if (isPriceCol || val.includes('.') || val.includes(',')) {
return num.toFixed(2);
}
}
}
return val;
};
const totalPages = Math.ceil(data.length / pageSize); const totalPages = Math.ceil(data.length / pageSize);
return ( return (
<div className="flex flex-col h-full bg-slate-800 rounded-xl border border-slate-700 shadow-xl overflow-hidden"> <div className="flex flex-col h-full bg-slate-800 rounded-xl border border-slate-700 shadow-xl overflow-hidden">
<div className="p-4 border-b border-slate-700 bg-slate-800/50"> <div className="p-4 border-b border-slate-700 bg-slate-800/50">
<h2 className="text-lg font-semibold text-white">Matrix View</h2> <h2 className="text-lg font-semibold text-white">Matrix View</h2>
<p className="text-sm text-slate-400">All data fields in their original order.</p> <p className="text-sm text-slate-400">All data fields formatted to 2 decimal places for numbers/prices.</p>
</div> </div>
<div className="overflow-auto flex-1"> <div className="overflow-auto flex-1">
@@ -38,11 +75,18 @@ export function MatrixView({ data, headers }: MatrixViewProps) {
<tbody className="divide-y divide-slate-700/50"> <tbody className="divide-y divide-slate-700/50">
{paginatedData.map((row, rowIndex) => ( {paginatedData.map((row, rowIndex) => (
<tr key={rowIndex} className="hover:bg-slate-700/30 transition-colors"> <tr key={rowIndex} className="hover:bg-slate-700/30 transition-colors">
{headers.map((_, colIndex) => ( {headers.map((header, colIndex) => {
<td key={colIndex} className="px-4 py-3 text-slate-300 max-w-[200px] truncate" title={String(row[colIndex] || '')}> const formattedValue = formatCellValue(row[colIndex], header);
{row[colIndex]} return (
</td> <td
))} key={colIndex}
className="px-4 py-3 text-slate-300 max-w-[200px] truncate"
title={String(row[colIndex] || '')}
>
{formattedValue}
</td>
);
})}
</tr> </tr>
))} ))}
{paginatedData.length === 0 && ( {paginatedData.length === 0 && (