Preserve QExcell cell borders

This commit is contained in:
Курнат Андрей
2026-06-01 07:55:45 +03:00
parent 27eae3afac
commit 56f2a4d6e4
6 changed files with 90 additions and 10 deletions
+35 -3
View File
@@ -844,11 +844,13 @@ export function xlsxStylesXmlToCellFormatTable(stylesXml?: unknown): Array<CellF
const numberFormats = xlsxNumberFormats(styleSheet);
const fonts = toArray(childRecord(styleSheet, "fonts").font).map(fontXmlToCellFormat);
const fills = toArray(childRecord(styleSheet, "fills").fill).map(fillXmlToCellFormat);
const borders = toArray(childRecord(styleSheet, "borders").border).map(borderXmlToCellFormat);
return toArray(childRecord(styleSheet, "cellXfs").xf).map((xf) => {
const record = asRecord(xf);
const fontFormat = fonts[numberAttribute(record.fontId)];
const fillFormat = fills[numberAttribute(record.fillId)];
const borderFormat = borders[numberAttribute(record.borderId)];
const numberFormat = normalizedNumberFormat(numberFormats[numberAttribute(record.numFmtId)]);
const alignment = childRecord(record, "alignment");
const horizontalAlign = normalizedHorizontalAlign(String(alignment.horizontal || ""));
@@ -857,6 +859,7 @@ export function xlsxStylesXmlToCellFormatTable(stylesXml?: unknown): Array<CellF
return normalizedCellFormat({
...fontFormat,
...fillFormat,
...borderFormat,
...(horizontalAlign ? { horizontalAlign } : {}),
...(verticalAlign ? { verticalAlign } : {}),
...(wrapText ? { wrapText: true } : {}),
@@ -868,6 +871,7 @@ export function xlsxStylesXmlToCellFormatTable(stylesXml?: unknown): Array<CellF
function buildStylesXml(styleRegistry = createCellStyleRegistry([])) {
const fonts = [baseFontXml()];
const fills = ['<fill><patternFill patternType="none"/></fill>', '<fill><patternFill patternType="gray125"/></fill>'];
const borders = [baseBorderXml()];
const fontIdsByKey: Record<string, number> = {};
const fillIdsByColor: Record<string, number> = {};
const numberFormatIdsByCode: Record<string, number> = {};
@@ -893,6 +897,14 @@ function buildStylesXml(styleRegistry = createCellStyleRegistry([])) {
}
}
let borderId = 0;
if (format.border) {
borderId = 1;
if (borders.length === 1) {
borders.push(formatBorderXml());
}
}
let numFmtId = 0;
if (format.numberFormat) {
numFmtId = numberFormatIdsByCode[format.numberFormat] ?? customNumberFormatStartId + customNumberFormats.length;
@@ -904,6 +916,7 @@ function buildStylesXml(styleRegistry = createCellStyleRegistry([])) {
const applyFont = fontId > 0 ? ' applyFont="1"' : "";
const applyFill = fillId > 0 ? ' applyFill="1"' : "";
const applyBorder = borderId > 0 ? ' applyBorder="1"' : "";
const applyAlignment = format.horizontalAlign || format.verticalAlign || format.wrapText ? ' applyAlignment="1"' : "";
const applyNumberFormat = numFmtId > 0 ? ' applyNumberFormat="1"' : "";
const alignmentAttributes = [
@@ -914,7 +927,7 @@ function buildStylesXml(styleRegistry = createCellStyleRegistry([])) {
.filter(Boolean)
.join(" ");
const alignmentXml = alignmentAttributes ? `<alignment ${alignmentAttributes}/>` : "";
return `<xf numFmtId="${numFmtId}" fontId="${fontId}" fillId="${fillId}" borderId="0" xfId="0"${applyFont}${applyFill}${applyAlignment}${applyNumberFormat}>${alignmentXml}</xf>`;
return `<xf numFmtId="${numFmtId}" fontId="${fontId}" fillId="${fillId}" borderId="${borderId}" xfId="0"${applyFont}${applyFill}${applyBorder}${applyAlignment}${applyNumberFormat}>${alignmentXml}</xf>`;
});
const numFmtsXml =
@@ -925,7 +938,7 @@ function buildStylesXml(styleRegistry = createCellStyleRegistry([])) {
: "";
return xmlDeclaration(
`<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">${numFmtsXml}<fonts count="${fonts.length}">${fonts.join("")}</fonts><fills count="${fills.length}">${fills.join("")}</fills><borders count="1"><border><left/><right/><top/><bottom/><diagonal/></border></borders><cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"/></cellStyleXfs><cellXfs count="${cellXfs.length + 1}"><xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/>${cellXfs.join("")}</cellXfs><cellStyles count="1"><cellStyle name="Normal" xfId="0" builtinId="0"/></cellStyles></styleSheet>`
`<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">${numFmtsXml}<fonts count="${fonts.length}">${fonts.join("")}</fonts><fills count="${fills.length}">${fills.join("")}</fills><borders count="${borders.length}">${borders.join("")}</borders><cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"/></cellStyleXfs><cellXfs count="${cellXfs.length + 1}"><xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/>${cellXfs.join("")}</cellXfs><cellStyles count="1"><cellStyle name="Normal" xfId="0" builtinId="0"/></cellStyles></styleSheet>`
);
}
@@ -953,7 +966,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.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.numberFormat ?? ""}`;
}
function fontStyleKey(format: CellFormat) {
@@ -982,6 +995,7 @@ function normalizedCellFormat(format: CellFormat | undefined): CellFormat | null
...(horizontalAlign ? { horizontalAlign } : {}),
...(verticalAlign ? { verticalAlign } : {}),
...(format?.wrapText ? { wrapText: true } : {}),
...(format?.border ? { border: true } : {}),
...(numberFormat ? { numberFormat } : {})
};
@@ -1012,6 +1026,16 @@ function fillXmlToCellFormat(fill: unknown): CellFormat {
return fillColor ? { fillColor } : {};
}
function borderXmlToCellFormat(border: unknown): CellFormat {
const record = asRecord(border);
const hasBorder = ["left", "right", "top", "bottom"].some((side) => {
const style = String(childRecord(record, side).style || childRecord(record, side)["@_style"] || "").trim().toLowerCase();
return style && style !== "none";
});
return hasBorder ? { border: true } : {};
}
function baseFontXml() {
return '<font><sz val="11"/><color theme="1"/><name val="Calibri"/><family val="2"/></font>';
}
@@ -1026,6 +1050,14 @@ function formatFillXml(fillColor: string) {
return `<fill><patternFill patternType="solid"><fgColor rgb="FF${fillColor}"/><bgColor indexed="64"/></patternFill></fill>`;
}
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 normalizeFillColor(value?: string) {
const raw = value?.trim().replace(/^#/, "").toUpperCase() ?? "";
if (/^[0-9A-F]{8}$/.test(raw)) {