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 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({
@@ -117,9 +145,40 @@ export default function App() {
const data = XLSX.utils.sheet_to_json<any[]>(ws, { header: 1 });
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({
headers: data[0],
data: data.slice(1),
headers: rawHeaders,
data: processedRows,
fileName: file.name,
fileDate: new Date(),
hasUnsavedChanges: false