Preserve QExcell font sizes
This commit is contained in:
@@ -69,11 +69,11 @@ export const initialState: QOfficeState = {
|
||||
},
|
||||
mergedCells: [{ start: "A8", end: "E8" }],
|
||||
cellFormats: {
|
||||
A1: { bold: true, textColor: "FFFFFF", fillColor: "0F5FAE", horizontalAlign: "center" },
|
||||
B1: { bold: true, textColor: "FFFFFF", fillColor: "0F5FAE", horizontalAlign: "center" },
|
||||
C1: { bold: true, textColor: "FFFFFF", fillColor: "0F5FAE", horizontalAlign: "center" },
|
||||
D1: { bold: true, textColor: "FFFFFF", fillColor: "0F5FAE", horizontalAlign: "center" },
|
||||
E1: { bold: true, textColor: "FFFFFF", fillColor: "0F5FAE", horizontalAlign: "center" },
|
||||
A1: { bold: true, fontSize: 12, textColor: "FFFFFF", fillColor: "0F5FAE", horizontalAlign: "center" },
|
||||
B1: { bold: true, fontSize: 12, textColor: "FFFFFF", fillColor: "0F5FAE", horizontalAlign: "center" },
|
||||
C1: { bold: true, fontSize: 12, textColor: "FFFFFF", fillColor: "0F5FAE", horizontalAlign: "center" },
|
||||
D1: { bold: true, fontSize: 12, textColor: "FFFFFF", fillColor: "0F5FAE", horizontalAlign: "center" },
|
||||
E1: { bold: true, fontSize: 12, textColor: "FFFFFF", fillColor: "0F5FAE", horizontalAlign: "center" },
|
||||
B2: { numberFormat: "# ##0.00 ₽" },
|
||||
C2: { numberFormat: "# ##0.00 ₽" },
|
||||
D2: { numberFormat: "# ##0.00 ₽" },
|
||||
|
||||
@@ -81,6 +81,7 @@ const horizontalAlignOptions = [
|
||||
{ value: "center", label: "По центру", icon: AlignCenter },
|
||||
{ value: "right", label: "По правому краю", icon: AlignRight }
|
||||
] as const;
|
||||
const fontSizeOptions = [10, 11, 12, 14, 16, 18, 20, 24];
|
||||
|
||||
function normalizedLinkInput(value: string | null) {
|
||||
const raw = value?.trim();
|
||||
@@ -108,10 +109,12 @@ function normalizedCellFormat(format: CellFormat): CellFormat | undefined {
|
||||
const fillColor = normalizedFillColor(format.fillColor);
|
||||
const horizontalAlign = normalizedHorizontalAlign(format.horizontalAlign);
|
||||
const numberFormat = format.numberFormat?.trim();
|
||||
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 } : {}),
|
||||
@@ -126,6 +129,7 @@ function cellInputStyle(format?: CellFormat): CSSProperties {
|
||||
...(format?.bold ? { fontWeight: 700 } : {}),
|
||||
...(format?.italics ? { fontStyle: "italic" } : {}),
|
||||
...(format?.underline ? { textDecoration: "underline" } : {}),
|
||||
...(format?.fontSize ? { fontSize: `${format.fontSize}pt` } : {}),
|
||||
...(format?.textColor ? { color: `#${format.textColor}` } : {}),
|
||||
...(format?.fillColor ? { backgroundColor: `#${format.fillColor}` } : {}),
|
||||
...(format?.horizontalAlign ? { textAlign: format.horizontalAlign } : {})
|
||||
@@ -136,6 +140,15 @@ function normalizedHorizontalAlign(value?: string) {
|
||||
return value === "left" || value === "center" || value === "right" ? value : "";
|
||||
}
|
||||
|
||||
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 normalizedChartRange(value: string) {
|
||||
const raw = value.trim().replace(/\$/g, "").toUpperCase();
|
||||
const [start, end = start] = raw.split(":");
|
||||
@@ -493,6 +506,21 @@ export function QExcell({ workbook, onChange }: QExcellProps) {
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
<select
|
||||
value={selectedCellFormat.fontSize ?? ""}
|
||||
onChange={(event) =>
|
||||
updateCellFormat(selectedCell, { fontSize: event.target.value ? Number(event.target.value) : undefined })
|
||||
}
|
||||
aria-label="Размер шрифта"
|
||||
title="Размер шрифта"
|
||||
>
|
||||
<option value="">Размер</option>
|
||||
{fontSizeOptions.map((fontSize) => (
|
||||
<option key={fontSize} value={fontSize}>
|
||||
{fontSize}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
<div className="fill-swatches" aria-label="Заливка">
|
||||
<PaintBucket size={16} aria-hidden="true" />
|
||||
{fillSwatches.map((swatch) => (
|
||||
|
||||
@@ -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
@@ -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 : "";
|
||||
|
||||
@@ -29,6 +29,7 @@ export interface CellFormat {
|
||||
bold?: boolean;
|
||||
italics?: boolean;
|
||||
underline?: boolean;
|
||||
fontSize?: number;
|
||||
textColor?: string;
|
||||
fillColor?: string;
|
||||
horizontalAlign?: "left" | "center" | "right";
|
||||
|
||||
Reference in New Issue
Block a user