Add QExcell cell alignment

This commit is contained in:
Курнат Андрей
2026-06-01 06:22:37 +03:00
parent 9c48e1d8b1
commit 24206eb8bb
6 changed files with 82 additions and 17 deletions
+5 -5
View File
@@ -69,11 +69,11 @@ export const initialState: QOfficeState = {
},
mergedCells: [{ start: "A8", end: "E8" }],
cellFormats: {
A1: { bold: true, textColor: "FFFFFF", fillColor: "0F5FAE" },
B1: { bold: true, textColor: "FFFFFF", fillColor: "0F5FAE" },
C1: { bold: true, textColor: "FFFFFF", fillColor: "0F5FAE" },
D1: { bold: true, textColor: "FFFFFF", fillColor: "0F5FAE" },
E1: { bold: true, textColor: "FFFFFF", fillColor: "0F5FAE" },
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" },
B2: { numberFormat: "# ##0.00 ₽" },
C2: { numberFormat: "# ##0.00 ₽" },
D2: { numberFormat: "# ##0.00 ₽" },
+33 -1
View File
@@ -1,6 +1,9 @@
import { useMemo, useRef, useState } from "react";
import type { CSSProperties } from "react";
import {
AlignCenter,
AlignLeft,
AlignRight,
BarChart3,
Bold,
Download,
@@ -73,6 +76,12 @@ const chartTypeOptions: Array<{ value: SheetChartType; label: string }> = [
{ value: "pie", label: "Круговая" }
];
const horizontalAlignOptions = [
{ value: "left", label: "По левому краю", icon: AlignLeft },
{ value: "center", label: "По центру", icon: AlignCenter },
{ value: "right", label: "По правому краю", icon: AlignRight }
] as const;
function normalizedLinkInput(value: string | null) {
const raw = value?.trim();
if (!raw) {
@@ -97,6 +106,7 @@ function normalizedFillColor(value?: string) {
function normalizedCellFormat(format: CellFormat): CellFormat | undefined {
const textColor = normalizedFillColor(format.textColor);
const fillColor = normalizedFillColor(format.fillColor);
const horizontalAlign = normalizedHorizontalAlign(format.horizontalAlign);
const numberFormat = format.numberFormat?.trim();
const normalized: CellFormat = {
...(format.bold ? { bold: true } : {}),
@@ -104,6 +114,7 @@ function normalizedCellFormat(format: CellFormat): CellFormat | undefined {
...(format.underline ? { underline: true } : {}),
...(textColor ? { textColor } : {}),
...(fillColor ? { fillColor } : {}),
...(horizontalAlign ? { horizontalAlign } : {}),
...(numberFormat ? { numberFormat } : {})
};
@@ -116,10 +127,15 @@ function cellInputStyle(format?: CellFormat): CSSProperties {
...(format?.italics ? { fontStyle: "italic" } : {}),
...(format?.underline ? { textDecoration: "underline" } : {}),
...(format?.textColor ? { color: `#${format.textColor}` } : {}),
...(format?.fillColor ? { backgroundColor: `#${format.fillColor}` } : {})
...(format?.fillColor ? { backgroundColor: `#${format.fillColor}` } : {}),
...(format?.horizontalAlign ? { textAlign: format.horizontalAlign } : {})
};
}
function normalizedHorizontalAlign(value?: string) {
return value === "left" || value === "center" || value === "right" ? value : "";
}
function normalizedChartRange(value: string) {
const raw = value.trim().replace(/\$/g, "").toUpperCase();
const [start, end = start] = raw.split(":");
@@ -461,6 +477,22 @@ export function QExcell({ workbook, onChange }: QExcellProps) {
>
<Underline size={16} />
</button>
{horizontalAlignOptions.map((option) => {
const Icon = option.icon;
const isActive = selectedCellFormat.horizontalAlign === option.value;
return (
<button
key={option.value}
className={`icon-button format-toggle ${isActive ? "is-active" : ""}`.trim()}
type="button"
onClick={() => updateCellFormat(selectedCell, { horizontalAlign: isActive ? undefined : option.value })}
title={option.label}
aria-label={option.label}
>
<Icon size={16} />
</button>
);
})}
<div className="fill-swatches" aria-label="Заливка">
<PaintBucket size={16} aria-hidden="true" />
{fillSwatches.map((swatch) => (
+28 -6
View File
@@ -76,7 +76,7 @@ describe("excellOffice OOXML helpers", () => {
it("сохраняет базовое форматирование ячеек XLSX в XML листа", () => {
const cellFormats = {
A1: { bold: true, textColor: "0f5fae", fillColor: "fff2cc" },
A1: { bold: true, textColor: "0f5fae", fillColor: "fff2cc", horizontalAlign: "center" as const },
B2: { italics: true, underline: true },
C3: { fillColor: "bad-color" }
};
@@ -103,12 +103,24 @@ describe("excellOffice OOXML helpers", () => {
fill: [{}, {}, { patternFill: { fgColor: { rgb: "FFFFF2CC" } } }]
},
cellXfs: {
xf: [{ fontId: "0", fillId: "0" }, { fontId: "1", fillId: "2", numFmtId: "164" }, { fontId: "0", fillId: "0", numFmtId: "14" }]
xf: [
{ fontId: "0", fillId: "0" },
{ fontId: "1", fillId: "2", numFmtId: "164", alignment: { horizontal: "center" } },
{ fontId: "0", fillId: "0", numFmtId: "14" }
]
}
}
});
expect(styleTable[1]).toEqual({ bold: true, italics: true, underline: true, textColor: "0F5FAE", fillColor: "FFF2CC", numberFormat: "dd.mm.yyyy" });
expect(styleTable[1]).toEqual({
bold: true,
italics: true,
underline: true,
textColor: "0F5FAE",
fillColor: "FFF2CC",
horizontalAlign: "center",
numberFormat: "dd.mm.yyyy"
});
expect(styleTable[2]).toEqual({ numberFormat: "m/d/yy" });
expect(
worksheetXmlToCellFormats(
@@ -124,7 +136,17 @@ describe("excellOffice OOXML helpers", () => {
},
styleTable
)
).toEqual({ A1: { bold: true, italics: true, underline: true, textColor: "0F5FAE", fillColor: "FFF2CC", numberFormat: "dd.mm.yyyy" } });
).toEqual({
A1: {
bold: true,
italics: true,
underline: true,
textColor: "0F5FAE",
fillColor: "FFF2CC",
horizontalAlign: "center",
numberFormat: "dd.mm.yyyy"
}
});
});
it("преобразует XML листа в модель QExcell", () => {
@@ -323,7 +345,7 @@ describe("excellOffice OOXML helpers", () => {
mergedCells: [{ start: "A1", end: "C1" }],
hyperlinks: { A1: "https://example.com/budget" },
cellFormats: {
A1: { bold: true, textColor: "0F5FAE", fillColor: "FFF2CC" },
A1: { bold: true, textColor: "0F5FAE", fillColor: "FFF2CC", horizontalAlign: "center" },
AC20: { underline: true },
D20: { numberFormat: "dd.mm.yyyy" },
E20: { numberFormat: "0%" }
@@ -357,7 +379,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" },
A1: { bold: true, textColor: "0F5FAE", fillColor: "FFF2CC", horizontalAlign: "center" },
AC20: { underline: true },
D20: { numberFormat: "dd.mm.yyyy" },
E20: { numberFormat: "0%" }
+13 -3
View File
@@ -850,7 +850,8 @@ export function xlsxStylesXmlToCellFormatTable(stylesXml?: unknown): Array<CellF
const fontFormat = fonts[numberAttribute(record.fontId)];
const fillFormat = fills[numberAttribute(record.fillId)];
const numberFormat = normalizedNumberFormat(numberFormats[numberAttribute(record.numFmtId)]);
return normalizedCellFormat({ ...fontFormat, ...fillFormat, ...(numberFormat ? { numberFormat } : {}) }) ?? undefined;
const horizontalAlign = normalizedHorizontalAlign(String(childRecord(record, "alignment").horizontal || ""));
return normalizedCellFormat({ ...fontFormat, ...fillFormat, ...(horizontalAlign ? { horizontalAlign } : {}), ...(numberFormat ? { numberFormat } : {}) }) ?? undefined;
});
}
@@ -893,8 +894,10 @@ function buildStylesXml(styleRegistry = createCellStyleRegistry([])) {
const applyFont = fontId > 0 ? ' applyFont="1"' : "";
const applyFill = fillId > 0 ? ' applyFill="1"' : "";
const applyAlignment = format.horizontalAlign ? ' applyAlignment="1"' : "";
const applyNumberFormat = numFmtId > 0 ? ' applyNumberFormat="1"' : "";
return `<xf numFmtId="${numFmtId}" fontId="${fontId}" fillId="${fillId}" borderId="0" xfId="0"${applyFont}${applyFill}${applyNumberFormat}/>`;
const alignmentXml = format.horizontalAlign ? `<alignment horizontal="${format.horizontalAlign}"/>` : "";
return `<xf numFmtId="${numFmtId}" fontId="${fontId}" fillId="${fillId}" borderId="0" xfId="0"${applyFont}${applyFill}${applyAlignment}${applyNumberFormat}>${alignmentXml}</xf>`;
});
const numFmtsXml =
@@ -933,7 +936,7 @@ function cellFormatKey(format: CellFormat | undefined) {
return "";
}
return `${normalized.bold ? "1" : "0"}|${normalized.italics ? "1" : "0"}|${normalized.underline ? "1" : "0"}|${normalized.textColor ?? ""}|${normalized.fillColor ?? ""}|${normalized.numberFormat ?? ""}`;
return `${normalized.bold ? "1" : "0"}|${normalized.italics ? "1" : "0"}|${normalized.underline ? "1" : "0"}|${normalized.textColor ?? ""}|${normalized.fillColor ?? ""}|${normalized.horizontalAlign ?? ""}|${normalized.numberFormat ?? ""}`;
}
function fontStyleKey(format: CellFormat) {
@@ -945,6 +948,7 @@ function fontStyleKey(format: CellFormat) {
function normalizedCellFormat(format: CellFormat | undefined): CellFormat | null {
const textColor = normalizeFillColor(format?.textColor);
const fillColor = normalizeFillColor(format?.fillColor);
const horizontalAlign = normalizedHorizontalAlign(format?.horizontalAlign);
const numberFormat = normalizedNumberFormat(format?.numberFormat);
const normalized: CellFormat = {
...(format?.bold ? { bold: true } : {}),
@@ -952,6 +956,7 @@ function normalizedCellFormat(format: CellFormat | undefined): CellFormat | null
...(format?.underline ? { underline: true } : {}),
...(textColor ? { textColor } : {}),
...(fillColor ? { fillColor } : {}),
...(horizontalAlign ? { horizontalAlign } : {}),
...(numberFormat ? { numberFormat } : {})
};
@@ -1000,6 +1005,11 @@ function normalizeFillColor(value?: string) {
return /^[0-9A-F]{6}$/.test(raw) ? raw : "";
}
function normalizedHorizontalAlign(value?: string) {
const normalized = value?.trim().toLowerCase() ?? "";
return normalized === "left" || normalized === "center" || normalized === "right" ? normalized : "";
}
function normalizedNumberFormat(value?: string) {
const raw = value?.trim() ?? "";
if (!raw || raw.toLowerCase() === "general") {
+1
View File
@@ -31,6 +31,7 @@ export interface CellFormat {
underline?: boolean;
textColor?: string;
fillColor?: string;
horizontalAlign?: "left" | "center" | "right";
numberFormat?: string;
}