Preserve QExcell vertical alignment
This commit is contained in:
+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";
|
||||
|
||||
Reference in New Issue
Block a user