Preserve QExcell border colors
This commit is contained in:
@@ -188,7 +188,7 @@ describe("excellOffice OOXML helpers", () => {
|
||||
id: "sheet-1",
|
||||
name: "Sheet 1",
|
||||
cells: { A1: "Bordered" },
|
||||
cellFormats: { A1: { border: true } }
|
||||
cellFormats: { A1: { border: true, borderColor: "C2410C" } }
|
||||
}
|
||||
]
|
||||
});
|
||||
@@ -201,6 +201,7 @@ describe("excellOffice OOXML helpers", () => {
|
||||
expect(stylesXml).toContain('<right style="thin">');
|
||||
expect(stylesXml).toContain('<top style="thin">');
|
||||
expect(stylesXml).toContain('<bottom style="thin">');
|
||||
expect(stylesXml).toContain('<color rgb="FFC2410C"/>');
|
||||
});
|
||||
|
||||
it("читает базовое форматирование ячеек XLSX из styles.xml и XML листа", () => {
|
||||
@@ -216,7 +217,15 @@ describe("excellOffice OOXML helpers", () => {
|
||||
fill: [{}, {}, { patternFill: { fgColor: { rgb: "FFFFF2CC" } } }]
|
||||
},
|
||||
borders: {
|
||||
border: [{}, { left: { style: "thin" }, right: { style: "thin" }, top: { style: "thin" }, bottom: { style: "thin" } }]
|
||||
border: [
|
||||
{},
|
||||
{
|
||||
left: { style: "thin", color: { rgb: "FFC2410C" } },
|
||||
right: { style: "thin", color: { rgb: "FFC2410C" } },
|
||||
top: { style: "thin", color: { rgb: "FFC2410C" } },
|
||||
bottom: { style: "thin", color: { rgb: "FFC2410C" } }
|
||||
}
|
||||
]
|
||||
},
|
||||
cellXfs: {
|
||||
xf: [
|
||||
@@ -241,6 +250,7 @@ describe("excellOffice OOXML helpers", () => {
|
||||
verticalAlign: "middle",
|
||||
wrapText: true,
|
||||
border: true,
|
||||
borderColor: "C2410C",
|
||||
numberFormat: "dd.mm.yyyy"
|
||||
});
|
||||
expect(styleTable[2]).toEqual({ numberFormat: "m/d/yy" });
|
||||
@@ -272,6 +282,7 @@ describe("excellOffice OOXML helpers", () => {
|
||||
verticalAlign: "middle",
|
||||
wrapText: true,
|
||||
border: true,
|
||||
borderColor: "C2410C",
|
||||
numberFormat: "dd.mm.yyyy"
|
||||
}
|
||||
});
|
||||
@@ -473,7 +484,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", verticalAlign: "middle", wrapText: true, border: true },
|
||||
A1: { bold: true, strike: true, fontSize: 14, fontFamily: "Arial", textColor: "0F5FAE", fillColor: "FFF2CC", horizontalAlign: "center", verticalAlign: "middle", wrapText: true, border: true, borderColor: "C2410C" },
|
||||
AC20: { underline: true },
|
||||
D20: { numberFormat: "dd.mm.yyyy" },
|
||||
E20: { numberFormat: "0%" }
|
||||
@@ -507,7 +518,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", verticalAlign: "middle", wrapText: true, border: true },
|
||||
A1: { bold: true, strike: true, fontSize: 14, fontFamily: "Arial", textColor: "0F5FAE", fillColor: "FFF2CC", horizontalAlign: "center", verticalAlign: "middle", wrapText: true, border: true, borderColor: "C2410C" },
|
||||
AC20: { underline: true },
|
||||
D20: { numberFormat: "dd.mm.yyyy" },
|
||||
E20: { numberFormat: "0%" }
|
||||
|
||||
+22
-9
@@ -874,6 +874,7 @@ function buildStylesXml(styleRegistry = createCellStyleRegistry([])) {
|
||||
const borders = [baseBorderXml()];
|
||||
const fontIdsByKey: Record<string, number> = {};
|
||||
const fillIdsByColor: Record<string, number> = {};
|
||||
const borderIdsByColor: Record<string, number> = {};
|
||||
const numberFormatIdsByCode: Record<string, number> = {};
|
||||
const customNumberFormats: Array<[number, string]> = [];
|
||||
|
||||
@@ -899,9 +900,11 @@ function buildStylesXml(styleRegistry = createCellStyleRegistry([])) {
|
||||
|
||||
let borderId = 0;
|
||||
if (format.border) {
|
||||
borderId = 1;
|
||||
if (borders.length === 1) {
|
||||
borders.push(formatBorderXml());
|
||||
const borderKey = format.borderColor || "auto";
|
||||
borderId = borderIdsByColor[borderKey] ?? borders.length;
|
||||
if (!borderIdsByColor[borderKey]) {
|
||||
borderIdsByColor[borderKey] = borderId;
|
||||
borders.push(formatBorderXml(format.borderColor));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -966,7 +969,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.verticalAlign ?? ""}|${normalized.wrapText ? "1" : "0"}|${normalized.border ? "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.border ? "1" : "0"}|${normalized.borderColor ?? ""}|${normalized.numberFormat ?? ""}`;
|
||||
}
|
||||
|
||||
function fontStyleKey(format: CellFormat) {
|
||||
@@ -978,6 +981,7 @@ function fontStyleKey(format: CellFormat) {
|
||||
function normalizedCellFormat(format: CellFormat | undefined): CellFormat | null {
|
||||
const textColor = normalizeFillColor(format?.textColor);
|
||||
const fillColor = normalizeFillColor(format?.fillColor);
|
||||
const borderColor = normalizeFillColor(format?.borderColor);
|
||||
const horizontalAlign = normalizedHorizontalAlign(format?.horizontalAlign);
|
||||
const verticalAlign = normalizedVerticalAlign(format?.verticalAlign);
|
||||
const numberFormat = normalizedNumberFormat(format?.numberFormat);
|
||||
@@ -995,7 +999,8 @@ function normalizedCellFormat(format: CellFormat | undefined): CellFormat | null
|
||||
...(horizontalAlign ? { horizontalAlign } : {}),
|
||||
...(verticalAlign ? { verticalAlign } : {}),
|
||||
...(format?.wrapText ? { wrapText: true } : {}),
|
||||
...(format?.border ? { border: true } : {}),
|
||||
...(format?.border || borderColor ? { border: true } : {}),
|
||||
...(borderColor ? { borderColor } : {}),
|
||||
...(numberFormat ? { numberFormat } : {})
|
||||
};
|
||||
|
||||
@@ -1028,12 +1033,19 @@ function fillXmlToCellFormat(fill: unknown): CellFormat {
|
||||
|
||||
function borderXmlToCellFormat(border: unknown): CellFormat {
|
||||
const record = asRecord(border);
|
||||
const hasBorder = ["left", "right", "top", "bottom"].some((side) => {
|
||||
const borderedSides = ["left", "right", "top", "bottom"].filter((side) => {
|
||||
const style = String(childRecord(record, side).style || childRecord(record, side)["@_style"] || "").trim().toLowerCase();
|
||||
return style && style !== "none";
|
||||
});
|
||||
|
||||
return hasBorder ? { border: true } : {};
|
||||
const borderColor = borderedSides
|
||||
.map((side) => normalizeFillColor(String(childRecord(childRecord(record, side), "color").rgb || "")))
|
||||
.find(Boolean);
|
||||
|
||||
return normalizedCellFormat({
|
||||
border: borderedSides.length > 0,
|
||||
...(borderColor ? { borderColor } : {})
|
||||
}) ?? {};
|
||||
}
|
||||
|
||||
function baseFontXml() {
|
||||
@@ -1054,8 +1066,9 @@ function baseBorderXml() {
|
||||
return "<border><left/><right/><top/><bottom/><diagonal/></border>";
|
||||
}
|
||||
|
||||
function formatBorderXml() {
|
||||
return '<border><left style="thin"><color auto="1"/></left><right style="thin"><color auto="1"/></right><top style="thin"><color auto="1"/></top><bottom style="thin"><color auto="1"/></bottom><diagonal/></border>';
|
||||
function formatBorderXml(borderColor = "") {
|
||||
const colorXml = borderColor ? `<color rgb="FF${borderColor}"/>` : '<color auto="1"/>';
|
||||
return `<border><left style="thin">${colorXml}</left><right style="thin">${colorXml}</right><top style="thin">${colorXml}</top><bottom style="thin">${colorXml}</bottom><diagonal/></border>`;
|
||||
}
|
||||
|
||||
function normalizeFillColor(value?: string) {
|
||||
|
||||
Reference in New Issue
Block a user