Preserve QExcell font sizes

This commit is contained in:
Курнат Андрей
2026-06-01 06:57:48 +03:00
parent 9f5a003bae
commit fec79a8a93
6 changed files with 59 additions and 15 deletions
+6 -4
View File
@@ -76,7 +76,7 @@ describe("excellOffice OOXML helpers", () => {
it("сохраняет базовое форматирование ячеек XLSX в XML листа", () => {
const cellFormats = {
A1: { bold: true, textColor: "0f5fae", fillColor: "fff2cc", horizontalAlign: "center" as const },
A1: { bold: true, fontSize: 14, textColor: "0f5fae", fillColor: "fff2cc", horizontalAlign: "center" as const },
B2: { italics: true, underline: true },
C3: { fillColor: "bad-color" }
};
@@ -97,7 +97,7 @@ describe("excellOffice OOXML helpers", () => {
numFmt: { numFmtId: "164", formatCode: "dd.mm.yyyy" }
},
fonts: {
font: [{}, { b: {}, i: {}, u: {}, color: { rgb: "FF0F5FAE" } }]
font: [{}, { b: {}, i: {}, u: {}, sz: { val: "14" }, color: { rgb: "FF0F5FAE" } }]
},
fills: {
fill: [{}, {}, { patternFill: { fgColor: { rgb: "FFFFF2CC" } } }]
@@ -116,6 +116,7 @@ describe("excellOffice OOXML helpers", () => {
bold: true,
italics: true,
underline: true,
fontSize: 14,
textColor: "0F5FAE",
fillColor: "FFF2CC",
horizontalAlign: "center",
@@ -141,6 +142,7 @@ describe("excellOffice OOXML helpers", () => {
bold: true,
italics: true,
underline: true,
fontSize: 14,
textColor: "0F5FAE",
fillColor: "FFF2CC",
horizontalAlign: "center",
@@ -345,7 +347,7 @@ describe("excellOffice OOXML helpers", () => {
mergedCells: [{ start: "A1", end: "C1" }],
hyperlinks: { A1: "https://example.com/budget" },
cellFormats: {
A1: { bold: true, textColor: "0F5FAE", fillColor: "FFF2CC", horizontalAlign: "center" },
A1: { bold: true, fontSize: 14, textColor: "0F5FAE", fillColor: "FFF2CC", horizontalAlign: "center" },
AC20: { underline: true },
D20: { numberFormat: "dd.mm.yyyy" },
E20: { numberFormat: "0%" }
@@ -379,7 +381,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, textColor: "0F5FAE", fillColor: "FFF2CC", horizontalAlign: "center" },
A1: { bold: true, fontSize: 14, textColor: "0F5FAE", fillColor: "FFF2CC", horizontalAlign: "center" },
AC20: { underline: true },
D20: { numberFormat: "dd.mm.yyyy" },
E20: { numberFormat: "0%" }
+17 -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.textColor ?? ""}|${normalized.fillColor ?? ""}|${normalized.horizontalAlign ?? ""}|${normalized.numberFormat ?? ""}`;
return `${normalized.bold ? "1" : "0"}|${normalized.italics ? "1" : "0"}|${normalized.underline ? "1" : "0"}|${normalized.fontSize ?? ""}|${normalized.textColor ?? ""}|${normalized.fillColor ?? ""}|${normalized.horizontalAlign ?? ""}|${normalized.numberFormat ?? ""}`;
}
function fontStyleKey(format: CellFormat) {
return format.bold || format.italics || format.underline || format.textColor
? `${format.bold ? "1" : "0"}|${format.italics ? "1" : "0"}|${format.underline ? "1" : "0"}|${format.textColor ?? ""}`
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 ?? ""}`
: "";
}
@@ -950,10 +950,12 @@ function normalizedCellFormat(format: CellFormat | undefined): CellFormat | null
const fillColor = normalizeFillColor(format?.fillColor);
const horizontalAlign = normalizedHorizontalAlign(format?.horizontalAlign);
const numberFormat = normalizedNumberFormat(format?.numberFormat);
const fontSize = normalizedFontSize(format?.fontSize);
const normalized: CellFormat = {
...(format?.bold ? { bold: true } : {}),
...(format?.italics ? { italics: true } : {}),
...(format?.underline ? { underline: true } : {}),
...(fontSize ? { fontSize } : {}),
...(textColor ? { textColor } : {}),
...(fillColor ? { fillColor } : {}),
...(horizontalAlign ? { horizontalAlign } : {}),
@@ -966,11 +968,13 @@ function normalizedCellFormat(format: CellFormat | undefined): CellFormat | null
function fontXmlToCellFormat(font: unknown): CellFormat {
const record = asRecord(font);
const textColor = normalizeFillColor(String(childRecord(record, "color").rgb || ""));
const fontSize = normalizedFontSize(numberAttribute(childRecord(record, "sz").val));
return (
normalizedCellFormat({
bold: hasXmlElement(record, "b"),
italics: hasXmlElement(record, "i"),
underline: hasXmlElement(record, "u"),
...(fontSize ? { fontSize } : {}),
...(textColor ? { textColor } : {})
}) ?? {}
);
@@ -989,7 +993,7 @@ function baseFontXml() {
function formatFontXml(format: CellFormat) {
const flags = `${format.bold ? "<b/>" : ""}${format.italics ? "<i/>" : ""}${format.underline ? "<u/>" : ""}`;
const color = format.textColor ? `<color rgb="FF${format.textColor}"/>` : '<color theme="1"/>';
return `<font>${flags}<sz val="11"/>${color}<name val="Calibri"/><family val="2"/></font>`;
return `<font>${flags}<sz val="${format.fontSize ?? 11}"/>${color}<name val="Calibri"/><family val="2"/></font>`;
}
function formatFillXml(fillColor: string) {
@@ -1005,6 +1009,15 @@ function normalizeFillColor(value?: string) {
return /^[0-9A-F]{6}$/.test(raw) ? raw : "";
}
function normalizedFontSize(value?: number) {
if (!Number.isFinite(value) || !value || value <= 0) {
return undefined;
}
const normalized = Math.round(value * 2) / 2;
return normalized === 11 ? undefined : normalized;
}
function normalizedHorizontalAlign(value?: string) {
const normalized = value?.trim().toLowerCase() ?? "";
return normalized === "left" || normalized === "center" || normalized === "right" ? normalized : "";