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
+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") {