Preserve QExcell vertical alignment
This commit is contained in:
@@ -4,6 +4,9 @@ import {
|
||||
AlignCenter,
|
||||
AlignLeft,
|
||||
AlignRight,
|
||||
AlignVerticalJustifyCenter,
|
||||
AlignVerticalJustifyEnd,
|
||||
AlignVerticalJustifyStart,
|
||||
BarChart3,
|
||||
Bold,
|
||||
Download,
|
||||
@@ -83,6 +86,11 @@ const horizontalAlignOptions = [
|
||||
{ value: "center", label: "По центру", icon: AlignCenter },
|
||||
{ value: "right", label: "По правому краю", icon: AlignRight }
|
||||
] as const;
|
||||
const verticalAlignOptions = [
|
||||
{ value: "top", label: "Выровнять по верхнему краю", icon: AlignVerticalJustifyStart },
|
||||
{ value: "middle", label: "Выровнять по середине", icon: AlignVerticalJustifyCenter },
|
||||
{ value: "bottom", label: "Выровнять по нижнему краю", icon: AlignVerticalJustifyEnd }
|
||||
] as const;
|
||||
const fontFamilyOptions = ["Aptos", "Arial", "Courier New", "Times New Roman"];
|
||||
const fontSizeOptions = [10, 11, 12, 14, 16, 18, 20, 24];
|
||||
|
||||
@@ -111,6 +119,7 @@ function normalizedCellFormat(format: CellFormat): CellFormat | undefined {
|
||||
const textColor = normalizedFillColor(format.textColor);
|
||||
const fillColor = normalizedFillColor(format.fillColor);
|
||||
const horizontalAlign = normalizedHorizontalAlign(format.horizontalAlign);
|
||||
const verticalAlign = normalizedVerticalAlign(format.verticalAlign);
|
||||
const numberFormat = format.numberFormat?.trim();
|
||||
const fontSize = normalizedFontSize(format.fontSize);
|
||||
const fontFamily = normalizedFontFamily(format.fontFamily);
|
||||
@@ -124,6 +133,7 @@ function normalizedCellFormat(format: CellFormat): CellFormat | undefined {
|
||||
...(textColor ? { textColor } : {}),
|
||||
...(fillColor ? { fillColor } : {}),
|
||||
...(horizontalAlign ? { horizontalAlign } : {}),
|
||||
...(verticalAlign ? { verticalAlign } : {}),
|
||||
...(format.wrapText ? { wrapText: true } : {}),
|
||||
...(numberFormat ? { numberFormat } : {})
|
||||
};
|
||||
@@ -146,10 +156,22 @@ function cellInputStyle(format?: CellFormat): CSSProperties {
|
||||
};
|
||||
}
|
||||
|
||||
function cellBoxStyle(format?: CellFormat): CSSProperties | undefined {
|
||||
return format?.verticalAlign ? { verticalAlign: cssVerticalAlignValue(format.verticalAlign) } : undefined;
|
||||
}
|
||||
|
||||
function normalizedHorizontalAlign(value?: string) {
|
||||
return value === "left" || value === "center" || value === "right" ? value : "";
|
||||
}
|
||||
|
||||
function normalizedVerticalAlign(value?: string) {
|
||||
return value === "top" || value === "middle" || value === "bottom" ? value : "";
|
||||
}
|
||||
|
||||
function cssVerticalAlignValue(value: CellFormat["verticalAlign"]) {
|
||||
return value === "middle" ? "middle" : value;
|
||||
}
|
||||
|
||||
function normalizedFontSize(value?: number) {
|
||||
if (!Number.isFinite(value) || !value || value <= 0) {
|
||||
return undefined;
|
||||
@@ -530,6 +552,22 @@ export function QExcell({ workbook, onChange }: QExcellProps) {
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
{verticalAlignOptions.map((option) => {
|
||||
const Icon = option.icon;
|
||||
const isActive = selectedCellFormat.verticalAlign === option.value;
|
||||
return (
|
||||
<button
|
||||
key={option.value}
|
||||
className={`icon-button format-toggle ${isActive ? "is-active" : ""}`.trim()}
|
||||
type="button"
|
||||
onClick={() => updateCellFormat(selectedCell, { verticalAlign: isActive ? undefined : option.value })}
|
||||
title={option.label}
|
||||
aria-label={option.label}
|
||||
>
|
||||
<Icon size={16} />
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
<button
|
||||
className={`icon-button format-toggle ${selectedCellFormat.wrapText ? "is-active" : ""}`.trim()}
|
||||
type="button"
|
||||
@@ -653,7 +691,8 @@ export function QExcell({ workbook, onChange }: QExcellProps) {
|
||||
return (
|
||||
<td
|
||||
key={id}
|
||||
className={`${selectedCell === id ? "is-selected" : ""} ${merge ? "is-merged" : ""} ${hyperlink ? "is-linked" : ""} ${cellFormat ? "is-formatted" : ""} ${cellFormat?.wrapText ? "is-wrapped" : ""}`.trim()}
|
||||
className={`${selectedCell === id ? "is-selected" : ""} ${merge ? "is-merged" : ""} ${hyperlink ? "is-linked" : ""} ${cellFormat ? "is-formatted" : ""} ${cellFormat?.wrapText ? "is-wrapped" : ""} ${cellFormat?.verticalAlign ? `is-valign-${cellFormat.verticalAlign}` : ""}`.trim()}
|
||||
style={cellBoxStyle(cellFormat)}
|
||||
colSpan={merge?.columnSpan}
|
||||
rowSpan={merge?.rowSpan}
|
||||
>
|
||||
|
||||
@@ -155,6 +155,28 @@ describe("excellOffice OOXML helpers", () => {
|
||||
expect(stylesXml).toContain('wrapText="1"');
|
||||
});
|
||||
|
||||
it("записывает вертикальное выравнивание ячейки XLSX в styles.xml", async () => {
|
||||
const blob = await exportXlsxBlob({
|
||||
id: "workbook",
|
||||
title: "VerticalAlign.xlsx",
|
||||
updatedAt: "2026-06-01T00:00:00.000Z",
|
||||
activeSheet: "sheet-1",
|
||||
sheets: [
|
||||
{
|
||||
id: "sheet-1",
|
||||
name: "Sheet 1",
|
||||
cells: { A1: "Centered" },
|
||||
cellFormats: { A1: { verticalAlign: "middle" } }
|
||||
}
|
||||
]
|
||||
});
|
||||
const zip = await JSZip.loadAsync(await blob.arrayBuffer());
|
||||
const stylesXml = await zip.file("xl/styles.xml")?.async("string");
|
||||
|
||||
expect(stylesXml).toContain('applyAlignment="1"');
|
||||
expect(stylesXml).toContain('vertical="center"');
|
||||
});
|
||||
|
||||
it("читает базовое форматирование ячеек XLSX из styles.xml и XML листа", () => {
|
||||
const styleTable = xlsxStylesXmlToCellFormatTable({
|
||||
styleSheet: {
|
||||
@@ -170,7 +192,7 @@ describe("excellOffice OOXML helpers", () => {
|
||||
cellXfs: {
|
||||
xf: [
|
||||
{ fontId: "0", fillId: "0" },
|
||||
{ fontId: "1", fillId: "2", numFmtId: "164", alignment: { horizontal: "center", wrapText: "1" } },
|
||||
{ fontId: "1", fillId: "2", numFmtId: "164", alignment: { horizontal: "center", vertical: "center", wrapText: "1" } },
|
||||
{ fontId: "0", fillId: "0", numFmtId: "14" }
|
||||
]
|
||||
}
|
||||
@@ -187,6 +209,7 @@ describe("excellOffice OOXML helpers", () => {
|
||||
textColor: "0F5FAE",
|
||||
fillColor: "FFF2CC",
|
||||
horizontalAlign: "center",
|
||||
verticalAlign: "middle",
|
||||
wrapText: true,
|
||||
numberFormat: "dd.mm.yyyy"
|
||||
});
|
||||
@@ -216,6 +239,7 @@ describe("excellOffice OOXML helpers", () => {
|
||||
textColor: "0F5FAE",
|
||||
fillColor: "FFF2CC",
|
||||
horizontalAlign: "center",
|
||||
verticalAlign: "middle",
|
||||
wrapText: true,
|
||||
numberFormat: "dd.mm.yyyy"
|
||||
}
|
||||
@@ -418,7 +442,7 @@ describe("excellOffice OOXML helpers", () => {
|
||||
mergedCells: [{ start: "A1", end: "C1" }],
|
||||
hyperlinks: { A1: "https://example.com/budget" },
|
||||
cellFormats: {
|
||||
A1: { bold: true, strike: true, fontSize: 14, fontFamily: "Arial", textColor: "0F5FAE", fillColor: "FFF2CC", horizontalAlign: "center", wrapText: true },
|
||||
A1: { bold: true, strike: true, fontSize: 14, fontFamily: "Arial", textColor: "0F5FAE", fillColor: "FFF2CC", horizontalAlign: "center", verticalAlign: "middle", wrapText: true },
|
||||
AC20: { underline: true },
|
||||
D20: { numberFormat: "dd.mm.yyyy" },
|
||||
E20: { numberFormat: "0%" }
|
||||
@@ -452,7 +476,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, fontFamily: "Arial", textColor: "0F5FAE", fillColor: "FFF2CC", horizontalAlign: "center", wrapText: true },
|
||||
A1: { bold: true, strike: true, fontSize: 14, fontFamily: "Arial", textColor: "0F5FAE", fillColor: "FFF2CC", horizontalAlign: "center", verticalAlign: "middle", wrapText: true },
|
||||
AC20: { underline: true },
|
||||
D20: { numberFormat: "dd.mm.yyyy" },
|
||||
E20: { numberFormat: "0%" }
|
||||
|
||||
+20
-2
@@ -852,11 +852,13 @@ export function xlsxStylesXmlToCellFormatTable(stylesXml?: unknown): Array<CellF
|
||||
const numberFormat = normalizedNumberFormat(numberFormats[numberAttribute(record.numFmtId)]);
|
||||
const alignment = childRecord(record, "alignment");
|
||||
const horizontalAlign = normalizedHorizontalAlign(String(alignment.horizontal || ""));
|
||||
const verticalAlign = normalizedVerticalAlign(String(alignment.vertical || ""));
|
||||
const wrapText = booleanAttribute(alignment.wrapText);
|
||||
return normalizedCellFormat({
|
||||
...fontFormat,
|
||||
...fillFormat,
|
||||
...(horizontalAlign ? { horizontalAlign } : {}),
|
||||
...(verticalAlign ? { verticalAlign } : {}),
|
||||
...(wrapText ? { wrapText: true } : {}),
|
||||
...(numberFormat ? { numberFormat } : {})
|
||||
}) ?? undefined;
|
||||
@@ -902,10 +904,11 @@ function buildStylesXml(styleRegistry = createCellStyleRegistry([])) {
|
||||
|
||||
const applyFont = fontId > 0 ? ' applyFont="1"' : "";
|
||||
const applyFill = fillId > 0 ? ' applyFill="1"' : "";
|
||||
const applyAlignment = format.horizontalAlign || format.wrapText ? ' applyAlignment="1"' : "";
|
||||
const applyAlignment = format.horizontalAlign || format.verticalAlign || format.wrapText ? ' applyAlignment="1"' : "";
|
||||
const applyNumberFormat = numFmtId > 0 ? ' applyNumberFormat="1"' : "";
|
||||
const alignmentAttributes = [
|
||||
format.horizontalAlign ? `horizontal="${format.horizontalAlign}"` : "",
|
||||
format.verticalAlign ? `vertical="${xlsxVerticalAlignValue(format.verticalAlign)}"` : "",
|
||||
format.wrapText ? 'wrapText="1"' : ""
|
||||
]
|
||||
.filter(Boolean)
|
||||
@@ -950,7 +953,7 @@ 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.fontFamily ?? ""}|${normalized.textColor ?? ""}|${normalized.fillColor ?? ""}|${normalized.horizontalAlign ?? ""}|${normalized.wrapText ? "1" : "0"}|${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.verticalAlign ?? ""}|${normalized.wrapText ? "1" : "0"}|${normalized.numberFormat ?? ""}`;
|
||||
}
|
||||
|
||||
function fontStyleKey(format: CellFormat) {
|
||||
@@ -963,6 +966,7 @@ function normalizedCellFormat(format: CellFormat | undefined): CellFormat | null
|
||||
const textColor = normalizeFillColor(format?.textColor);
|
||||
const fillColor = normalizeFillColor(format?.fillColor);
|
||||
const horizontalAlign = normalizedHorizontalAlign(format?.horizontalAlign);
|
||||
const verticalAlign = normalizedVerticalAlign(format?.verticalAlign);
|
||||
const numberFormat = normalizedNumberFormat(format?.numberFormat);
|
||||
const fontSize = normalizedFontSize(format?.fontSize);
|
||||
const fontFamily = normalizedFontFamily(format?.fontFamily);
|
||||
@@ -976,6 +980,7 @@ function normalizedCellFormat(format: CellFormat | undefined): CellFormat | null
|
||||
...(textColor ? { textColor } : {}),
|
||||
...(fillColor ? { fillColor } : {}),
|
||||
...(horizontalAlign ? { horizontalAlign } : {}),
|
||||
...(verticalAlign ? { verticalAlign } : {}),
|
||||
...(format?.wrapText ? { wrapText: true } : {}),
|
||||
...(numberFormat ? { numberFormat } : {})
|
||||
};
|
||||
@@ -1049,6 +1054,19 @@ function normalizedHorizontalAlign(value?: string) {
|
||||
return normalized === "left" || normalized === "center" || normalized === "right" ? normalized : "";
|
||||
}
|
||||
|
||||
function normalizedVerticalAlign(value?: string) {
|
||||
const normalized = value?.trim().toLowerCase() ?? "";
|
||||
if (normalized === "center" || normalized === "middle") {
|
||||
return "middle";
|
||||
}
|
||||
|
||||
return normalized === "top" || normalized === "bottom" ? normalized : "";
|
||||
}
|
||||
|
||||
function xlsxVerticalAlignValue(value: CellFormat["verticalAlign"]) {
|
||||
return value === "middle" ? "center" : value;
|
||||
}
|
||||
|
||||
function booleanAttribute(value: unknown) {
|
||||
const normalized = String(value ?? "").trim().toLowerCase();
|
||||
return normalized === "1" || normalized === "true";
|
||||
|
||||
@@ -732,6 +732,31 @@ button {
|
||||
height: 58px;
|
||||
}
|
||||
|
||||
.sheet-grid td.is-valign-top input {
|
||||
padding-top: 0;
|
||||
padding-bottom: 8px;
|
||||
}
|
||||
|
||||
.sheet-grid td.is-valign-middle input {
|
||||
padding-top: 0;
|
||||
padding-bottom: 0;
|
||||
}
|
||||
|
||||
.sheet-grid td.is-valign-bottom input {
|
||||
padding-top: 8px;
|
||||
padding-bottom: 0;
|
||||
}
|
||||
|
||||
.sheet-grid td.is-valign-middle textarea {
|
||||
padding-top: 14px;
|
||||
padding-bottom: 14px;
|
||||
}
|
||||
|
||||
.sheet-grid td.is-valign-bottom textarea {
|
||||
padding-top: 22px;
|
||||
padding-bottom: 2px;
|
||||
}
|
||||
|
||||
.sheet-grid input,
|
||||
.sheet-grid textarea {
|
||||
width: 100%;
|
||||
|
||||
@@ -35,6 +35,7 @@ export interface CellFormat {
|
||||
textColor?: string;
|
||||
fillColor?: string;
|
||||
horizontalAlign?: "left" | "center" | "right";
|
||||
verticalAlign?: "top" | "middle" | "bottom";
|
||||
wrapText?: boolean;
|
||||
numberFormat?: string;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user