Preserve QExcell border colors

This commit is contained in:
Курнат Андрей
2026-06-01 08:00:09 +03:00
parent 56f2a4d6e4
commit 95be5e3a8a
5 changed files with 74 additions and 17 deletions
+22 -9
View File
@@ -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) {