Preserve QExcell font families

This commit is contained in:
Курнат Андрей
2026-06-01 07:23:32 +03:00
parent c2eaff87c6
commit f388489d2b
5 changed files with 67 additions and 12 deletions
+13 -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.strike ? "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.fontFamily ?? ""}|${normalized.textColor ?? ""}|${normalized.fillColor ?? ""}|${normalized.horizontalAlign ?? ""}|${normalized.numberFormat ?? ""}`;
}
function fontStyleKey(format: CellFormat) {
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 ?? ""}`
return format.bold || format.italics || format.underline || format.strike || format.fontSize || format.fontFamily || format.textColor
? `${format.bold ? "1" : "0"}|${format.italics ? "1" : "0"}|${format.underline ? "1" : "0"}|${format.strike ? "1" : "0"}|${format.fontSize ?? ""}|${format.fontFamily ?? ""}|${format.textColor ?? ""}`
: "";
}
@@ -951,12 +951,14 @@ function normalizedCellFormat(format: CellFormat | undefined): CellFormat | null
const horizontalAlign = normalizedHorizontalAlign(format?.horizontalAlign);
const numberFormat = normalizedNumberFormat(format?.numberFormat);
const fontSize = normalizedFontSize(format?.fontSize);
const fontFamily = normalizedFontFamily(format?.fontFamily);
const normalized: CellFormat = {
...(format?.bold ? { bold: true } : {}),
...(format?.italics ? { italics: true } : {}),
...(format?.underline ? { underline: true } : {}),
...(format?.strike ? { strike: true } : {}),
...(fontSize ? { fontSize } : {}),
...(fontFamily ? { fontFamily } : {}),
...(textColor ? { textColor } : {}),
...(fillColor ? { fillColor } : {}),
...(horizontalAlign ? { horizontalAlign } : {}),
@@ -970,6 +972,7 @@ 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));
const fontFamily = normalizedFontFamily(String(childRecord(record, "name").val || ""));
return (
normalizedCellFormat({
bold: hasXmlElement(record, "b"),
@@ -977,6 +980,7 @@ function fontXmlToCellFormat(font: unknown): CellFormat {
underline: hasXmlElement(record, "u"),
strike: hasXmlElement(record, "strike"),
...(fontSize ? { fontSize } : {}),
...(fontFamily ? { fontFamily } : {}),
...(textColor ? { textColor } : {})
}) ?? {}
);
@@ -995,7 +999,7 @@ function baseFontXml() {
function formatFontXml(format: CellFormat) {
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>`;
return `<font>${flags}<sz val="${format.fontSize ?? 11}"/>${color}<name val="${escapeXmlAttribute(format.fontFamily ?? "Calibri")}"/><family val="2"/></font>`;
}
function formatFillXml(fillColor: string) {
@@ -1020,6 +1024,11 @@ function normalizedFontSize(value?: number) {
return normalized === 11 ? undefined : normalized;
}
function normalizedFontFamily(value?: string) {
const normalized = value?.trim().replace(/["<>]/g, "").slice(0, 80) ?? "";
return normalized && normalized.toLowerCase() !== "calibri" ? normalized : "";
}
function normalizedHorizontalAlign(value?: string) {
const normalized = value?.trim().toLowerCase() ?? "";
return normalized === "left" || normalized === "center" || normalized === "right" ? normalized : "";