Preserve QExcell worksheet zoom in XLSX

This commit is contained in:
Курнат Андрей
2026-06-01 19:21:01 +03:00
parent 1117d8bbf0
commit f4da4dc283
4 changed files with 86 additions and 8 deletions
+36 -6
View File
@@ -15,6 +15,7 @@ export interface ImportedExcellWorkbook {
columnWidths?: Record<string, number>;
rowHeights?: Record<string, number>;
showGridLines?: boolean;
zoomScale?: number;
cells: Record<string, string>;
mergedCells?: MergedCellRange[];
hyperlinks?: Record<string, string>;
@@ -68,6 +69,7 @@ interface ParsedSheet {
columnWidths?: Record<string, number>;
rowHeights?: Record<string, number>;
showGridLines?: boolean;
zoomScale?: number;
cells: Record<string, string>;
mergedCells?: MergedCellRange[];
hyperlinks?: Record<string, string>;
@@ -276,7 +278,8 @@ export async function exportXlsxBlob(workbook: Workbook): Promise<Blob> {
sheet.tabColor,
sheet.columnWidths,
sheet.rowHeights,
sheet.showGridLines
sheet.showGridLines,
sheet.zoomScale
)
);
const relationshipsXml = buildWorksheetRelationshipsXml(
@@ -315,6 +318,7 @@ export function parsedWorksheetToSheet(
const columnWidths = worksheetXmlToColumnWidths(worksheetXml);
const rowHeights = worksheetXmlToRowHeights(worksheetXml);
const showGridLines = worksheetXmlToShowGridLines(worksheetXml);
const zoomScale = worksheetXmlToZoomScale(worksheetXml);
return {
id: sheet.id || `sheet-${index + 1}`,
@@ -326,6 +330,7 @@ export function parsedWorksheetToSheet(
...(Object.keys(columnWidths).length > 0 ? { columnWidths } : {}),
...(Object.keys(rowHeights).length > 0 ? { rowHeights } : {}),
...(showGridLines === false ? { showGridLines } : {}),
...(zoomScale ? { zoomScale } : {}),
cells: worksheetXmlToCells(worksheetXml, sharedStrings),
mergedCells: worksheetXmlToMergedCells(worksheetXml),
hyperlinks: worksheetXmlToHyperlinks(worksheetXml, relationshipsXml),
@@ -456,6 +461,13 @@ export function worksheetXmlToShowGridLines(worksheetXml: unknown): boolean | un
return !isFalseXmlBoolean(value);
}
export function worksheetXmlToZoomScale(worksheetXml: unknown) {
const worksheet = childRecord(worksheetXml, "worksheet");
const sheetViews = childRecord(worksheet, "sheetViews");
const sheetView = asRecord(toArray(sheetViews.sheetView)[0]);
return normalizedSheetZoomScale(sheetView.zoomScale);
}
export function worksheetXmlToAutoFilter(worksheetXml: unknown) {
const worksheet = childRecord(worksheetXml, "worksheet");
const autoFilter = childRecord(worksheet, "autoFilter");
@@ -681,7 +693,8 @@ export function buildWorksheetXml(
tabColor?: string,
columnWidths: Record<string, number> = {},
rowHeights: Record<string, number> = {},
showGridLines?: boolean
showGridLines?: boolean,
zoomScale?: number
) {
const normalizedRowHeights = normalizedSheetRowHeights(rowHeights);
const rows = sortedWorksheetEntries(cells, cellFormats).reduce<Record<number, Array<[string, string]>>>((grouped, entry) => {
@@ -712,7 +725,7 @@ export function buildWorksheetXml(
const hyperlinksXml = buildHyperlinksXml(hyperlinks);
const drawingXml = drawingRelationshipId ? `<drawing r:id="${escapeXmlAttribute(drawingRelationshipId)}"/>` : "";
const sheetPrXml = buildSheetPrXml(tabColor);
const sheetViewsXml = buildSheetViewsXml(frozenPane, showGridLines);
const sheetViewsXml = buildSheetViewsXml(frozenPane, showGridLines, zoomScale);
const colsXml = buildColumnWidthsXml(columnWidths);
return xmlDeclaration(
@@ -773,13 +786,20 @@ export function buildHyperlinksXml(hyperlinks: Record<string, string> = {}) {
return `<hyperlinks>${hyperlinkXml}</hyperlinks>`;
}
export function buildSheetViewsXml(frozenPane?: SheetFrozenPane, showGridLines?: boolean) {
export function buildSheetViewsXml(frozenPane?: SheetFrozenPane, showGridLines?: boolean, zoomScale?: number) {
const pane = normalizedFrozenPane(frozenPane);
if (!pane && showGridLines !== false) {
const normalizedZoomScale = normalizedSheetZoomScale(zoomScale);
if (!pane && showGridLines !== false && !normalizedZoomScale) {
return "";
}
const sheetViewAttributes = ["workbookViewId=\"0\"", showGridLines === false ? 'showGridLines="0"' : ""].filter(Boolean).join(" ");
const sheetViewAttributes = [
"workbookViewId=\"0\"",
showGridLines === false ? 'showGridLines="0"' : "",
normalizedZoomScale ? `zoomScale="${normalizedZoomScale}"` : ""
]
.filter(Boolean)
.join(" ");
if (!pane) {
return `<sheetViews><sheetView ${sheetViewAttributes}/></sheetViews>`;
}
@@ -1545,6 +1565,16 @@ function normalizedSheetRowHeights(rowHeights: Record<string, number> = {}) {
}, {});
}
function normalizedSheetZoomScale(value: unknown) {
const number = Number(value);
if (!Number.isFinite(number)) {
return 0;
}
const zoomScale = Math.round(number);
return zoomScale >= 10 && zoomScale <= 400 && zoomScale !== 100 ? zoomScale : 0;
}
function buildRowHeightAttributes(height?: number) {
return height ? ` ht="${formatSheetDimension(height)}" customHeight="1"` : "";
}