Preserve QExcell font families
This commit is contained in:
@@ -77,7 +77,7 @@ describe("excellOffice OOXML helpers", () => {
|
||||
|
||||
it("сохраняет базовое форматирование ячеек XLSX в XML листа", () => {
|
||||
const cellFormats = {
|
||||
A1: { bold: true, fontSize: 14, textColor: "0f5fae", fillColor: "fff2cc", horizontalAlign: "center" as const },
|
||||
A1: { bold: true, fontSize: 14, fontFamily: "Arial", textColor: "0f5fae", fillColor: "fff2cc", horizontalAlign: "center" as const },
|
||||
B2: { italics: true, underline: true, strike: true },
|
||||
C3: { fillColor: "bad-color" }
|
||||
};
|
||||
@@ -112,6 +112,27 @@ describe("excellOffice OOXML helpers", () => {
|
||||
expect(stylesXml).toContain("<strike/>");
|
||||
});
|
||||
|
||||
it("записывает семейство шрифта ячейки XLSX в styles.xml", async () => {
|
||||
const blob = await exportXlsxBlob({
|
||||
id: "workbook",
|
||||
title: "Font.xlsx",
|
||||
updatedAt: "2026-06-01T00:00:00.000Z",
|
||||
activeSheet: "sheet-1",
|
||||
sheets: [
|
||||
{
|
||||
id: "sheet-1",
|
||||
name: "Sheet 1",
|
||||
cells: { A1: "Arial" },
|
||||
cellFormats: { A1: { fontFamily: "Arial" } }
|
||||
}
|
||||
]
|
||||
});
|
||||
const zip = await JSZip.loadAsync(await blob.arrayBuffer());
|
||||
const stylesXml = await zip.file("xl/styles.xml")?.async("string");
|
||||
|
||||
expect(stylesXml).toContain('<name val="Arial"/>');
|
||||
});
|
||||
|
||||
it("читает базовое форматирование ячеек XLSX из styles.xml и XML листа", () => {
|
||||
const styleTable = xlsxStylesXmlToCellFormatTable({
|
||||
styleSheet: {
|
||||
@@ -119,7 +140,7 @@ describe("excellOffice OOXML helpers", () => {
|
||||
numFmt: { numFmtId: "164", formatCode: "dd.mm.yyyy" }
|
||||
},
|
||||
fonts: {
|
||||
font: [{}, { b: {}, i: {}, u: {}, strike: {}, sz: { val: "14" }, color: { rgb: "FF0F5FAE" } }]
|
||||
font: [{}, { b: {}, i: {}, u: {}, strike: {}, sz: { val: "14" }, name: { val: "Arial" }, color: { rgb: "FF0F5FAE" } }]
|
||||
},
|
||||
fills: {
|
||||
fill: [{}, {}, { patternFill: { fgColor: { rgb: "FFFFF2CC" } } }]
|
||||
@@ -140,6 +161,7 @@ describe("excellOffice OOXML helpers", () => {
|
||||
underline: true,
|
||||
strike: true,
|
||||
fontSize: 14,
|
||||
fontFamily: "Arial",
|
||||
textColor: "0F5FAE",
|
||||
fillColor: "FFF2CC",
|
||||
horizontalAlign: "center",
|
||||
@@ -167,6 +189,7 @@ describe("excellOffice OOXML helpers", () => {
|
||||
underline: true,
|
||||
strike: true,
|
||||
fontSize: 14,
|
||||
fontFamily: "Arial",
|
||||
textColor: "0F5FAE",
|
||||
fillColor: "FFF2CC",
|
||||
horizontalAlign: "center",
|
||||
@@ -371,7 +394,7 @@ describe("excellOffice OOXML helpers", () => {
|
||||
mergedCells: [{ start: "A1", end: "C1" }],
|
||||
hyperlinks: { A1: "https://example.com/budget" },
|
||||
cellFormats: {
|
||||
A1: { bold: true, strike: true, fontSize: 14, textColor: "0F5FAE", fillColor: "FFF2CC", horizontalAlign: "center" },
|
||||
A1: { bold: true, strike: true, fontSize: 14, fontFamily: "Arial", textColor: "0F5FAE", fillColor: "FFF2CC", horizontalAlign: "center" },
|
||||
AC20: { underline: true },
|
||||
D20: { numberFormat: "dd.mm.yyyy" },
|
||||
E20: { numberFormat: "0%" }
|
||||
@@ -405,7 +428,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, strike: true, fontSize: 14, textColor: "0F5FAE", fillColor: "FFF2CC", horizontalAlign: "center" },
|
||||
A1: { bold: true, strike: true, fontSize: 14, fontFamily: "Arial", textColor: "0F5FAE", fillColor: "FFF2CC", horizontalAlign: "center" },
|
||||
AC20: { underline: true },
|
||||
D20: { numberFormat: "dd.mm.yyyy" },
|
||||
E20: { numberFormat: "0%" }
|
||||
|
||||
+13
-4
@@ -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 : "";
|
||||
|
||||
Reference in New Issue
Block a user