Add QExcell cell alignment
This commit is contained in:
@@ -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
@@ -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") {
|
||||
|
||||
Reference in New Issue
Block a user