Fix formatting errors

This commit is contained in:
2026-03-02 08:17:30 +01:00
parent 63cff880a5
commit 3a9140bc37
3 changed files with 55 additions and 30 deletions

View File

@@ -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<string, any[]>();
const incomingMap = new Map<string, any[][]>();
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);
}
}
});