From 3a9140bc376be493e7507e5cc0b955d81868b5f4 Mon Sep 17 00:00:00 2001 From: Peacock Date: Mon, 2 Mar 2026 08:17:30 +0100 Subject: [PATCH] Fix formatting errors --- src/taskpane/components/App.tsx | 2 +- src/taskpane/components/ColumnMapper.tsx | 8 +++ src/taskpane/excelLogic.ts | 75 +++++++++++++++--------- 3 files changed, 55 insertions(+), 30 deletions(-) diff --git a/src/taskpane/components/App.tsx b/src/taskpane/components/App.tsx index 656df87..02559a4 100644 --- a/src/taskpane/components/App.tsx +++ b/src/taskpane/components/App.tsx @@ -39,7 +39,7 @@ const App: React.FC = () => { colorNew: "#d4edda", // light green colorChanged: "#fff3cd", // light yellow/orange colorDeleted: "#f8d7da", // light red - colorDuplicate: "#ff0000ff", // light orange for duplicates + colorDuplicate: "#ffe6cc", // light orange for duplicates }); useEffect(() => { diff --git a/src/taskpane/components/ColumnMapper.tsx b/src/taskpane/components/ColumnMapper.tsx index 4df342a..e458715 100644 --- a/src/taskpane/components/ColumnMapper.tsx +++ b/src/taskpane/components/ColumnMapper.tsx @@ -150,6 +150,14 @@ const ColumnMapper: React.FC = ({ style={{ width: "60px", padding: "0", cursor: "pointer", height: "30px", border: "none" }} /> + + onSettingsChange({ ...settings, colorDuplicate: e.target.value })} + style={{ width: "60px", padding: "0", cursor: "pointer", height: "30px", border: "none" }} + /> + diff --git a/src/taskpane/excelLogic.ts b/src/taskpane/excelLogic.ts index 18c625e..b8e66ff 100644 --- a/src/taskpane/excelLogic.ts +++ b/src/taskpane/excelLogic.ts @@ -242,7 +242,8 @@ export async function consolidateData(mappings: SheetMappingStatus[], settings: if (listExists) { try { // Existierende Liste aktualisieren - const table = targetSheet.tables.getItem("KonsolidierteKabel"); + // Wir nutzen getItemAt(0) statt eines festen Namens, da der Name "KonsolidierteKabel" bei umbenannten Sicherungskopien blockiert sein kann. + const table = targetSheet.tables.getItemAt(0); const bodyRange = table.getDataBodyRange(); bodyRange.load("values, rowCount, columnCount"); await context.sync(); @@ -253,11 +254,14 @@ export async function consolidateData(mappings: SheetMappingStatus[], settings: const formatDeletedQueue: number[] = []; const formatDuplicateQueue: number[] = []; // NEU: Queue für Duplikate - const incomingMap = new Map(); + const incomingMap = new Map(); for (const row of finalData) { const kNr = String(row[0] || "").trim(); if (kNr) { - incomingMap.set(kNr, row); + if (!incomingMap.has(kNr)) { + incomingMap.set(kNr, []); + } + incomingMap.get(kNr)!.push(row); } } @@ -271,37 +275,46 @@ export async function consolidateData(mappings: SheetMappingStatus[], settings: if (!kNr) continue; if (incomingMap.has(kNr)) { - // Geändert prüfen - const inVals = incomingMap.get(kNr)!; - for (let c = 0; c < TARGET_COLUMNS.length; c++) { - const oldVal = String(row[c] || "").trim(); - const newVal = String(inVals[c] || "").trim(); - if (oldVal !== newVal) { - existingValues[r][c] = newVal; - formatChangedQueue.push({ row: r, col: c }); + const inValsArray = incomingMap.get(kNr)!; + if (inValsArray.length > 0) { + const inVals = inValsArray.shift()!; // pop the first one + // Geändert prüfen + for (let c = 0; c < TARGET_COLUMNS.length; c++) { + const oldVal = String(row[c] || "").trim(); + const newVal = String(inVals[c] || "").trim(); + if (oldVal !== newVal) { + existingValues[r][c] = newVal; + formatChangedQueue.push({ row: r, col: c }); + } } + + // Prüfen ob Bemerkung aktualisiert werden muss (z.B. neues Duplikat) + const bemerkungColIndex = fullHeaders.length - 1; + const inBemerkung = String(inVals[bemerkungColIndex] || "").trim(); + existingValues[r][bemerkungColIndex] = inBemerkung; + + if (inBemerkung.startsWith("Duplikat")) { + formatDuplicateQueue.push(r); + } + + if (inValsArray.length === 0) { + incomingMap.delete(kNr); + } + } else { + // Should theoretically not happen if we delete when 0, but safe fallback + formatDeletedQueue.push(r); } - - // Prüfen ob Bemerkung aktualisiert werden muss (z.B. neues Duplikat) - const bemerkungColIndex = fullHeaders.length - 1; - const inBemerkung = String(inVals[bemerkungColIndex] || "").trim(); - existingValues[r][bemerkungColIndex] = inBemerkung; - - if (inBemerkung.startsWith("Duplikat")) { - formatDuplicateQueue.push(r); - } - - incomingMap.delete(kNr); } else { // Entfallen - // Optional die Zeile einfärben formatDeletedQueue.push(r); } } - // Verbleibend = Neue Kabel - incomingMap.forEach((newRow) => { - newRows.push(newRow); + // Verbleibend = Neue Kabel (und überschüssige Duplikate) + incomingMap.forEach((newRowsArray) => { + newRowsArray.forEach((newRow) => { + newRows.push(newRow); + }); }); // Werte in die bestehende Tabelle zurückschreiben @@ -320,6 +333,10 @@ export async function consolidateData(mappings: SheetMappingStatus[], settings: // Formate anwenden const currentBody = table.getDataBodyRange(); + // Alte Formatierungen zurücksetzen, damit behobene Konflikte wieder normal aussehen + currentBody.format.fill.clear(); + currentBody.format.font.strikethrough = false; + // Geänderte Zellen markieren for (const cell of formatChangedQueue) { currentBody.getCell(cell.row, cell.col).format.fill.color = settings.colorChanged; @@ -380,11 +397,11 @@ export async function consolidateData(mappings: SheetMappingStatus[], settings: await context.sync(); const table = targetSheet.tables.add(targetRange, true /* hasHeaders */); - table.name = "KonsolidierteKabel"; table.style = "TableStyleLight9"; table.showFilterButton = true; - // Markiere komplett neu entfernt auf Userwunsch + // WICHTIG: Excel überschreibt manuelle Formate, wenn wir den TableStyle nicht zuerst synchronisieren! + await context.sync(); // Duplikate beim Neu-Erstellen einfärben: const bodyRange = table.getDataBodyRange(); @@ -402,7 +419,7 @@ export async function consolidateData(mappings: SheetMappingStatus[], settings: return rowsConsolidated; } catch (error) { console.error("Fehler beim Erstellen der Kabelliste:", error); - throw new Error("Fehler beim Erstellen der 'Kabelliste'. Möglicherweise ist die Arbeitsmappe schreibgeschützt."); + throw new Error("Fehler beim Erstellen der 'Kabelliste'. Möglicherweise ist die Arbeitsmappe schreibgeschützt.\n" + error); } } });