Preserve QExcell strikethrough formatting

This commit is contained in:
Курнат Андрей
2026-06-01 07:19:23 +03:00
parent ea26f5c55d
commit c2eaff87c6
5 changed files with 51 additions and 12 deletions
+28 -4
View File
@@ -1,4 +1,5 @@
import { describe, expect, it } from "vitest";
import JSZip from "jszip";
import {
buildChartXml,
buildCellXml,
@@ -77,7 +78,7 @@ describe("excellOffice OOXML helpers", () => {
it("сохраняет базовое форматирование ячеек XLSX в XML листа", () => {
const cellFormats = {
A1: { bold: true, fontSize: 14, textColor: "0f5fae", fillColor: "fff2cc", horizontalAlign: "center" as const },
B2: { italics: true, underline: true },
B2: { italics: true, underline: true, strike: true },
C3: { fillColor: "bad-color" }
};
const registry = createCellStyleRegistry([cellFormats]);
@@ -90,6 +91,27 @@ describe("excellOffice OOXML helpers", () => {
expect(emptyCellXml).toContain('<c r="C3" s="1"/>');
});
it("записывает зачеркивание ячейки XLSX в styles.xml", async () => {
const blob = await exportXlsxBlob({
id: "workbook",
title: "Strike.xlsx",
updatedAt: "2026-06-01T00:00:00.000Z",
activeSheet: "sheet-1",
sheets: [
{
id: "sheet-1",
name: "Sheet 1",
cells: { A1: "Deleted" },
cellFormats: { A1: { strike: true } }
}
]
});
const zip = await JSZip.loadAsync(await blob.arrayBuffer());
const stylesXml = await zip.file("xl/styles.xml")?.async("string");
expect(stylesXml).toContain("<strike/>");
});
it("читает базовое форматирование ячеек XLSX из styles.xml и XML листа", () => {
const styleTable = xlsxStylesXmlToCellFormatTable({
styleSheet: {
@@ -97,7 +119,7 @@ describe("excellOffice OOXML helpers", () => {
numFmt: { numFmtId: "164", formatCode: "dd.mm.yyyy" }
},
fonts: {
font: [{}, { b: {}, i: {}, u: {}, sz: { val: "14" }, color: { rgb: "FF0F5FAE" } }]
font: [{}, { b: {}, i: {}, u: {}, strike: {}, sz: { val: "14" }, color: { rgb: "FF0F5FAE" } }]
},
fills: {
fill: [{}, {}, { patternFill: { fgColor: { rgb: "FFFFF2CC" } } }]
@@ -116,6 +138,7 @@ describe("excellOffice OOXML helpers", () => {
bold: true,
italics: true,
underline: true,
strike: true,
fontSize: 14,
textColor: "0F5FAE",
fillColor: "FFF2CC",
@@ -142,6 +165,7 @@ describe("excellOffice OOXML helpers", () => {
bold: true,
italics: true,
underline: true,
strike: true,
fontSize: 14,
textColor: "0F5FAE",
fillColor: "FFF2CC",
@@ -347,7 +371,7 @@ describe("excellOffice OOXML helpers", () => {
mergedCells: [{ start: "A1", end: "C1" }],
hyperlinks: { A1: "https://example.com/budget" },
cellFormats: {
A1: { bold: true, fontSize: 14, textColor: "0F5FAE", fillColor: "FFF2CC", horizontalAlign: "center" },
A1: { bold: true, strike: true, fontSize: 14, textColor: "0F5FAE", fillColor: "FFF2CC", horizontalAlign: "center" },
AC20: { underline: true },
D20: { numberFormat: "dd.mm.yyyy" },
E20: { numberFormat: "0%" }
@@ -381,7 +405,7 @@ describe("excellOffice OOXML helpers", () => {
expect(imported.sheets[0].mergedCells).toEqual([{ start: "A1", end: "C1" }]);
expect(imported.sheets[0].hyperlinks).toEqual({ A1: "https://example.com/budget" });
expect(imported.sheets[0].cellFormats).toEqual({
A1: { bold: true, fontSize: 14, textColor: "0F5FAE", fillColor: "FFF2CC", horizontalAlign: "center" },
A1: { bold: true, strike: true, fontSize: 14, textColor: "0F5FAE", fillColor: "FFF2CC", horizontalAlign: "center" },
AC20: { underline: true },
D20: { numberFormat: "dd.mm.yyyy" },
E20: { numberFormat: "0%" }
+6 -4
View File
@@ -936,12 +936,12 @@ function cellFormatKey(format: CellFormat | undefined) {
return "";
}
return `${normalized.bold ? "1" : "0"}|${normalized.italics ? "1" : "0"}|${normalized.underline ? "1" : "0"}|${normalized.fontSize ?? ""}|${normalized.textColor ?? ""}|${normalized.fillColor ?? ""}|${normalized.horizontalAlign ?? ""}|${normalized.numberFormat ?? ""}`;
return `${normalized.bold ? "1" : "0"}|${normalized.italics ? "1" : "0"}|${normalized.underline ? "1" : "0"}|${normalized.strike ? "1" : "0"}|${normalized.fontSize ?? ""}|${normalized.textColor ?? ""}|${normalized.fillColor ?? ""}|${normalized.horizontalAlign ?? ""}|${normalized.numberFormat ?? ""}`;
}
function fontStyleKey(format: CellFormat) {
return format.bold || format.italics || format.underline || format.fontSize || format.textColor
? `${format.bold ? "1" : "0"}|${format.italics ? "1" : "0"}|${format.underline ? "1" : "0"}|${format.fontSize ?? ""}|${format.textColor ?? ""}`
return format.bold || format.italics || format.underline || format.strike || format.fontSize || format.textColor
? `${format.bold ? "1" : "0"}|${format.italics ? "1" : "0"}|${format.underline ? "1" : "0"}|${format.strike ? "1" : "0"}|${format.fontSize ?? ""}|${format.textColor ?? ""}`
: "";
}
@@ -955,6 +955,7 @@ function normalizedCellFormat(format: CellFormat | undefined): CellFormat | null
...(format?.bold ? { bold: true } : {}),
...(format?.italics ? { italics: true } : {}),
...(format?.underline ? { underline: true } : {}),
...(format?.strike ? { strike: true } : {}),
...(fontSize ? { fontSize } : {}),
...(textColor ? { textColor } : {}),
...(fillColor ? { fillColor } : {}),
@@ -974,6 +975,7 @@ function fontXmlToCellFormat(font: unknown): CellFormat {
bold: hasXmlElement(record, "b"),
italics: hasXmlElement(record, "i"),
underline: hasXmlElement(record, "u"),
strike: hasXmlElement(record, "strike"),
...(fontSize ? { fontSize } : {}),
...(textColor ? { textColor } : {})
}) ?? {}
@@ -991,7 +993,7 @@ function baseFontXml() {
}
function formatFontXml(format: CellFormat) {
const flags = `${format.bold ? "<b/>" : ""}${format.italics ? "<i/>" : ""}${format.underline ? "<u/>" : ""}`;
const flags = `${format.bold ? "<b/>" : ""}${format.italics ? "<i/>" : ""}${format.underline ? "<u/>" : ""}${format.strike ? "<strike/>" : ""}`;
const color = format.textColor ? `<color rgb="FF${format.textColor}"/>` : '<color theme="1"/>';
return `<font>${flags}<sz val="${format.fontSize ?? 11}"/>${color}<name val="Calibri"/><family val="2"/></font>`;
}