Preserve QExcell gridline visibility in XLSX
This commit is contained in:
@@ -27,6 +27,7 @@ import {
|
||||
worksheetXmlToHyperlinks,
|
||||
worksheetXmlToMergedCells,
|
||||
worksheetXmlToRowHeights,
|
||||
worksheetXmlToShowGridLines,
|
||||
worksheetXmlToTabColor,
|
||||
xlsxStylesXmlToCellFormatTable,
|
||||
xlsxThemeXmlToColors,
|
||||
@@ -650,6 +651,43 @@ describe("excellOffice OOXML helpers", () => {
|
||||
expect(imported.sheets[0].frozenPane).toEqual({ columns: 1, rows: 1, topLeftCell: "B2", activePane: "bottomRight" });
|
||||
});
|
||||
|
||||
it("читает и записывает скрытие сетки листа XLSX через sheetView showGridLines", async () => {
|
||||
expect(
|
||||
worksheetXmlToShowGridLines({
|
||||
worksheet: {
|
||||
sheetViews: {
|
||||
sheetView: { showGridLines: "0" }
|
||||
}
|
||||
}
|
||||
})
|
||||
).toBe(false);
|
||||
expect(buildSheetViewsXml(undefined, false)).toBe('<sheetViews><sheetView workbookViewId="0" showGridLines="0"/></sheetViews>');
|
||||
expect(buildSheetViewsXml({ rows: 1 }, false)).toBe(
|
||||
'<sheetViews><sheetView workbookViewId="0" showGridLines="0"><pane ySplit="1" topLeftCell="A2" activePane="bottomLeft" state="frozen"/></sheetView></sheetViews>'
|
||||
);
|
||||
|
||||
const blob = await exportXlsxBlob({
|
||||
id: "book-1",
|
||||
title: "Grid lines.xlsx",
|
||||
updatedAt: "2026-06-01T00:00:00.000Z",
|
||||
activeSheet: "sheet-1",
|
||||
sheets: [
|
||||
{
|
||||
id: "sheet-1",
|
||||
name: "Grid",
|
||||
showGridLines: false,
|
||||
cells: { A1: "No grid" }
|
||||
}
|
||||
]
|
||||
});
|
||||
const zip = await JSZip.loadAsync(await blob.arrayBuffer());
|
||||
const sheetXml = (await zip.file("xl/worksheets/sheet1.xml")?.async("string")) ?? "";
|
||||
const imported = await importXlsxFile(new File([blob], "Grid lines.xlsx", { type: blob.type }));
|
||||
|
||||
expect(sheetXml).toContain('<sheetViews><sheetView workbookViewId="0" showGridLines="0"/></sheetViews>');
|
||||
expect(imported.sheets[0].showGridLines).toBe(false);
|
||||
});
|
||||
|
||||
it("читает и записывает автофильтр XLSX через autoFilter ref", async () => {
|
||||
expect(
|
||||
worksheetXmlToAutoFilter({
|
||||
|
||||
+35
-7
@@ -14,6 +14,7 @@ export interface ImportedExcellWorkbook {
|
||||
tabColor?: string;
|
||||
columnWidths?: Record<string, number>;
|
||||
rowHeights?: Record<string, number>;
|
||||
showGridLines?: boolean;
|
||||
cells: Record<string, string>;
|
||||
mergedCells?: MergedCellRange[];
|
||||
hyperlinks?: Record<string, string>;
|
||||
@@ -66,6 +67,7 @@ interface ParsedSheet {
|
||||
tabColor?: string;
|
||||
columnWidths?: Record<string, number>;
|
||||
rowHeights?: Record<string, number>;
|
||||
showGridLines?: boolean;
|
||||
cells: Record<string, string>;
|
||||
mergedCells?: MergedCellRange[];
|
||||
hyperlinks?: Record<string, string>;
|
||||
@@ -273,7 +275,8 @@ export async function exportXlsxBlob(workbook: Workbook): Promise<Blob> {
|
||||
sheet.autoFilter,
|
||||
sheet.tabColor,
|
||||
sheet.columnWidths,
|
||||
sheet.rowHeights
|
||||
sheet.rowHeights,
|
||||
sheet.showGridLines
|
||||
)
|
||||
);
|
||||
const relationshipsXml = buildWorksheetRelationshipsXml(
|
||||
@@ -311,6 +314,7 @@ export function parsedWorksheetToSheet(
|
||||
const tabColor = worksheetXmlToTabColor(worksheetXml, themeColors);
|
||||
const columnWidths = worksheetXmlToColumnWidths(worksheetXml);
|
||||
const rowHeights = worksheetXmlToRowHeights(worksheetXml);
|
||||
const showGridLines = worksheetXmlToShowGridLines(worksheetXml);
|
||||
|
||||
return {
|
||||
id: sheet.id || `sheet-${index + 1}`,
|
||||
@@ -321,6 +325,7 @@ export function parsedWorksheetToSheet(
|
||||
...(tabColor ? { tabColor } : {}),
|
||||
...(Object.keys(columnWidths).length > 0 ? { columnWidths } : {}),
|
||||
...(Object.keys(rowHeights).length > 0 ? { rowHeights } : {}),
|
||||
...(showGridLines === false ? { showGridLines } : {}),
|
||||
cells: worksheetXmlToCells(worksheetXml, sharedStrings),
|
||||
mergedCells: worksheetXmlToMergedCells(worksheetXml),
|
||||
hyperlinks: worksheetXmlToHyperlinks(worksheetXml, relationshipsXml),
|
||||
@@ -439,6 +444,18 @@ export function worksheetXmlToFrozenPane(worksheetXml: unknown): SheetFrozenPane
|
||||
};
|
||||
}
|
||||
|
||||
export function worksheetXmlToShowGridLines(worksheetXml: unknown): boolean | undefined {
|
||||
const worksheet = childRecord(worksheetXml, "worksheet");
|
||||
const sheetViews = childRecord(worksheet, "sheetViews");
|
||||
const sheetView = asRecord(toArray(sheetViews.sheetView)[0]);
|
||||
const value = sheetView.showGridLines;
|
||||
if (value === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return !isFalseXmlBoolean(value);
|
||||
}
|
||||
|
||||
export function worksheetXmlToAutoFilter(worksheetXml: unknown) {
|
||||
const worksheet = childRecord(worksheetXml, "worksheet");
|
||||
const autoFilter = childRecord(worksheet, "autoFilter");
|
||||
@@ -663,7 +680,8 @@ export function buildWorksheetXml(
|
||||
autoFilter?: string,
|
||||
tabColor?: string,
|
||||
columnWidths: Record<string, number> = {},
|
||||
rowHeights: Record<string, number> = {}
|
||||
rowHeights: Record<string, number> = {},
|
||||
showGridLines?: boolean
|
||||
) {
|
||||
const normalizedRowHeights = normalizedSheetRowHeights(rowHeights);
|
||||
const rows = sortedWorksheetEntries(cells, cellFormats).reduce<Record<number, Array<[string, string]>>>((grouped, entry) => {
|
||||
@@ -694,7 +712,7 @@ export function buildWorksheetXml(
|
||||
const hyperlinksXml = buildHyperlinksXml(hyperlinks);
|
||||
const drawingXml = drawingRelationshipId ? `<drawing r:id="${escapeXmlAttribute(drawingRelationshipId)}"/>` : "";
|
||||
const sheetPrXml = buildSheetPrXml(tabColor);
|
||||
const sheetViewsXml = buildSheetViewsXml(frozenPane);
|
||||
const sheetViewsXml = buildSheetViewsXml(frozenPane, showGridLines);
|
||||
const colsXml = buildColumnWidthsXml(columnWidths);
|
||||
|
||||
return xmlDeclaration(
|
||||
@@ -755,13 +773,18 @@ export function buildHyperlinksXml(hyperlinks: Record<string, string> = {}) {
|
||||
return `<hyperlinks>${hyperlinkXml}</hyperlinks>`;
|
||||
}
|
||||
|
||||
export function buildSheetViewsXml(frozenPane?: SheetFrozenPane) {
|
||||
export function buildSheetViewsXml(frozenPane?: SheetFrozenPane, showGridLines?: boolean) {
|
||||
const pane = normalizedFrozenPane(frozenPane);
|
||||
if (!pane) {
|
||||
if (!pane && showGridLines !== false) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const attributes = [
|
||||
const sheetViewAttributes = ["workbookViewId=\"0\"", showGridLines === false ? 'showGridLines="0"' : ""].filter(Boolean).join(" ");
|
||||
if (!pane) {
|
||||
return `<sheetViews><sheetView ${sheetViewAttributes}/></sheetViews>`;
|
||||
}
|
||||
|
||||
const paneAttributes = [
|
||||
pane.columns > 0 ? `xSplit="${pane.columns}"` : "",
|
||||
pane.rows > 0 ? `ySplit="${pane.rows}"` : "",
|
||||
`topLeftCell="${escapeXmlAttribute(pane.topLeftCell)}"`,
|
||||
@@ -771,7 +794,7 @@ export function buildSheetViewsXml(frozenPane?: SheetFrozenPane) {
|
||||
.filter(Boolean)
|
||||
.join(" ");
|
||||
|
||||
return `<sheetViews><sheetView workbookViewId="0"><pane ${attributes}/></sheetView></sheetViews>`;
|
||||
return `<sheetViews><sheetView ${sheetViewAttributes}><pane ${paneAttributes}/></sheetView></sheetViews>`;
|
||||
}
|
||||
|
||||
export function buildSheetPrXml(tabColor?: string) {
|
||||
@@ -1451,6 +1474,11 @@ function booleanAttribute(value: unknown) {
|
||||
return normalized === "1" || normalized === "true";
|
||||
}
|
||||
|
||||
function isFalseXmlBoolean(value: unknown) {
|
||||
const normalized = String(value ?? "").trim().toLowerCase();
|
||||
return normalized === "0" || normalized === "false";
|
||||
}
|
||||
|
||||
function normalizedNumberFormat(value?: string) {
|
||||
const raw = value?.trim() ?? "";
|
||||
if (!raw || raw.toLowerCase() === "general") {
|
||||
|
||||
@@ -24,6 +24,7 @@ export interface Sheet {
|
||||
tabColor?: string;
|
||||
columnWidths?: Record<string, number>;
|
||||
rowHeights?: Record<string, number>;
|
||||
showGridLines?: boolean;
|
||||
cells: Record<string, string>;
|
||||
mergedCells?: MergedCellRange[];
|
||||
hyperlinks?: Record<string, string>;
|
||||
|
||||
Reference in New Issue
Block a user