Fehlerdatei: auch aus einer in Excel gespeicherten Datei
Excel legt bedingte Formatierungen und Auswahllisten, die auf ein anderes
Blatt verweisen ("_Listen"), in der Erweiterung x14 ab. ExcelJS liest
solche Regeln ohne Formel und bricht beim Zurueckschreiben ab ("Cannot
read properties of undefined (reading '0')") - die Fehlerdatei entstand
nicht, und der Import endete auf der Seite mit "nicht uebertragen".
Regeln ohne Formel werden vor dem Schreiben entfernt; scheitert das
Schreiben trotzdem, entsteht die Fehlerdatei ohne Faerbungen und
Pruefungen, mit Markierungen, Kommentaren und Fehlerliste.
Geprueft: Regressionstest mit einer Datei, wie Excel sie speichert
(x14-Regel und x14-Auswahlliste) - schlaegt ohne die Aenderung mit
demselben Fehler fehl wie auf dem Server; lint, tsc, 1001 Tests,
Uebergabe-Waechter.
This commit is contained in:
@@ -103,5 +103,47 @@ export async function fehlerdatei(original: ArrayBuffer, meldungen: readonly Mel
|
||||
(liste as unknown as { orderNo: number }).orderNo = -1;
|
||||
mappe.views = [{ activeTab: 0, x: 0, y: 0, width: 10000, height: 20000, firstSheet: 0, visibility: "visible" }];
|
||||
|
||||
return new Uint8Array(await mappe.xlsx.writeBuffer());
|
||||
for (const ws of mappe.worksheets) unvollstaendigeFormatierungEntfernen(ws);
|
||||
try {
|
||||
return new Uint8Array(await mappe.xlsx.writeBuffer());
|
||||
} catch {
|
||||
// Was ExcelJS aus einer in Excel gespeicherten Datei gelesen hat, lässt
|
||||
// sich nicht immer zurückschreiben. Dann ohne Färbungen und Prüfungen —
|
||||
// die Markierungen der Meldungen, die Kommentare und die Fehlerliste
|
||||
// bleiben, und mit ihnen das, wofür die Datei da ist.
|
||||
for (const ws of mappe.worksheets) {
|
||||
formatierungen(ws).length = 0;
|
||||
(ws as unknown as { dataValidations: { model: Record<string, unknown> } }).dataValidations.model = {};
|
||||
}
|
||||
return new Uint8Array(await mappe.xlsx.writeBuffer());
|
||||
}
|
||||
}
|
||||
|
||||
type Formatierung = { ref: string; rules: { type?: string; formulae?: unknown[] }[] };
|
||||
|
||||
/** Die bedingten Formatierungen eines Blatts; ExcelJS nennt das Feld nicht in seinen Typen. */
|
||||
function formatierungen(ws: ExcelJS.Worksheet): Formatierung[] {
|
||||
const w = ws as unknown as { conditionalFormattings?: Formatierung[] };
|
||||
w.conditionalFormattings ??= [];
|
||||
return w.conditionalFormattings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bedingte Formatierungen ohne Formel entfernen.
|
||||
*
|
||||
* Excel legt eine Regel, die auf ein anderes Blatt verweist (hier
|
||||
* „_Listen"), in einer Erweiterung ab. ExcelJS liest sie als Regel ohne
|
||||
* Formel und bricht beim Schreiben daran ab. Die Färbung entfällt in der
|
||||
* Fehlerdatei; der Import prüft dieselbe Regel ohnehin.
|
||||
*/
|
||||
function unvollstaendigeFormatierungEntfernen(ws: ExcelJS.Worksheet) {
|
||||
const liste = formatierungen(ws);
|
||||
const behalten = liste
|
||||
.map((f) => ({
|
||||
...f,
|
||||
rules: (f.rules ?? []).filter((r) => r.type !== "expression" || (Array.isArray(r.formulae) && r.formulae.length > 0 && r.formulae[0] != null)),
|
||||
}))
|
||||
.filter((f) => f.rules.length > 0);
|
||||
liste.length = 0;
|
||||
liste.push(...behalten);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import ExcelJS from "exceljs";
|
||||
import JSZip from "jszip";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { BLATT_FEHLER, fehlerdatei } from "@/lib/austausch/fehlerdatei";
|
||||
import { austauschdateiLesen } from "@/lib/austausch/lesen";
|
||||
@@ -24,7 +25,39 @@ async function oeffnen(daten: Uint8Array) {
|
||||
return mappe;
|
||||
}
|
||||
|
||||
/**
|
||||
* Eine Regel, wie Excel sie beim Speichern ablegt, wenn sie auf ein anderes
|
||||
* Blatt verweist: in der Erweiterung x14, die Formel als xm:f. ExcelJS liest
|
||||
* sie ohne Formel.
|
||||
*/
|
||||
async function wieVonExcelGespeichert(daten: ArrayBuffer): Promise<ArrayBuffer> {
|
||||
const zip = await JSZip.loadAsync(daten);
|
||||
const pfad = Object.keys(zip.files).find((p) => /^xl\/worksheets\/sheet\d+\.xml$/.test(p))!;
|
||||
const xml = await zip.file(pfad)!.async("string");
|
||||
const ext =
|
||||
'<extLst><ext uri="{78C0D931-6437-407d-A8EE-F0AAD7539E65}" xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main">' +
|
||||
'<x14:conditionalFormattings><x14:conditionalFormatting xmlns:xm="http://schemas.microsoft.com/office/excel/2006/main">' +
|
||||
'<x14:cfRule type="expression" priority="99" id="{00000000-0000-0000-0000-000000000001}">' +
|
||||
"<xm:f>ISNA(MATCH($B2,_Listen!$A$2:$A$5,0))</xm:f>" +
|
||||
'<x14:dxf><fill><patternFill><bgColor rgb="FFF8C9C9"/></patternFill></fill></x14:dxf></x14:cfRule>' +
|
||||
"<xm:sqref>E2:E10</xm:sqref></x14:conditionalFormatting></x14:conditionalFormattings></ext>" +
|
||||
// Ebenso eine Auswahlliste mit Bezug auf ein anderes Blatt.
|
||||
'<ext uri="{CCE6A557-97BC-4b89-ADB6-D9C93CAAB3DF}" xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main">' +
|
||||
'<x14:dataValidations count="1" xmlns:xm="http://schemas.microsoft.com/office/excel/2006/main">' +
|
||||
'<x14:dataValidation type="list" allowBlank="1" showErrorMessage="1"><x14:formula1><xm:f>_Listen!$A$2:$A$5</xm:f></x14:formula1>' +
|
||||
"<xm:sqref>F2:F10</xm:sqref></x14:dataValidation></x14:dataValidations></ext></extLst>";
|
||||
zip.file(pfad, xml.replace("</worksheet>", `${ext}</worksheet>`));
|
||||
return (await zip.generateAsync({ type: "arraybuffer" })) as ArrayBuffer;
|
||||
}
|
||||
|
||||
describe("Fehlerdatei", () => {
|
||||
it("entsteht auch aus einer in Excel gespeicherten Datei mit Regeln auf ein anderes Blatt", async () => {
|
||||
const original = await wieVonExcelGespeichert(await datei([[null, 1, "A", "B", "x"]]));
|
||||
const meldungen = (await austauschdateiLesen(original)).meldungen;
|
||||
const mappe = await oeffnen(await fehlerdatei(original, meldungen));
|
||||
expect(mappe.getWorksheet("Mitarbeiter")!.getCell(2, 5).note).toMatch(/Geschlecht/);
|
||||
});
|
||||
|
||||
it("markiert die Zellen der Meldungen und stellt die Fehlerliste nach vorne", async () => {
|
||||
const original = await datei([[null, 1, "A", "B", "x"]]);
|
||||
const gelesen = await austauschdateiLesen(original);
|
||||
|
||||
Reference in New Issue
Block a user