Preserve QExcell hidden sheets in XLSX

This commit is contained in:
Курнат Андрей
2026-06-01 12:45:33 +03:00
parent 8517bd6391
commit 227915d09d
4 changed files with 70 additions and 9 deletions
+35 -7
View File
@@ -8,6 +8,7 @@ export interface ImportedExcellWorkbook {
sheets: Array<{
id: string;
name: string;
visibility?: "hidden" | "veryHidden";
cells: Record<string, string>;
mergedCells?: MergedCellRange[];
hyperlinks?: Record<string, string>;
@@ -47,12 +48,14 @@ type FastXmlParserModule = {
interface WorkbookSheetRef {
id: string;
name: string;
visibility?: "hidden" | "veryHidden";
relationshipId: string;
}
interface ParsedSheet {
id: string;
name: string;
visibility?: "hidden" | "veryHidden";
cells: Record<string, string>;
mergedCells?: MergedCellRange[];
hyperlinks?: Record<string, string>;
@@ -224,7 +227,16 @@ export async function exportXlsxBlob(workbook: Workbook): Promise<Blob> {
zip.file("_rels/.rels", buildRootRelationshipsXml());
zip.file("docProps/app.xml", buildAppPropertiesXml());
zip.file("docProps/core.xml", buildCorePropertiesXml());
zip.file("xl/workbook.xml", buildWorkbookXml(sheets.map((sheet, index) => sheet.name.trim() || `Лист ${index + 1}`), activeSheetIndex));
zip.file(
"xl/workbook.xml",
buildWorkbookXml(
sheets.map((sheet, index) => ({
name: sheet.name.trim() || `Лист ${index + 1}`,
...(sheet.visibility ? { visibility: sheet.visibility } : {})
})),
activeSheetIndex
)
);
zip.file("xl/_rels/workbook.xml.rels", buildWorkbookRelationshipsXml(sheets.length));
zip.file("xl/styles.xml", buildStylesXml(styleRegistry));
@@ -267,6 +279,7 @@ export function parsedWorksheetToSheet(
return {
id: sheet.id || `sheet-${index + 1}`,
name: sheet.name || `Лист ${index + 1}`,
...(sheet.visibility ? { visibility: sheet.visibility } : {}),
cells: worksheetXmlToCells(worksheetXml, sharedStrings),
mergedCells: worksheetXmlToMergedCells(worksheetXml),
hyperlinks: worksheetXmlToHyperlinks(worksheetXml, relationshipsXml),
@@ -632,14 +645,28 @@ export function workbookSheetRefs(workbookXml: unknown): WorkbookSheetRef[] {
return toArray(sheets.sheet).map((sheet, index) => {
const record = asRecord(sheet);
const visibility = workbookSheetVisibility(record.state);
return {
id: `sheet-${String(record.sheetId || index + 1)}`,
name: String(record.name || `Лист ${index + 1}`),
...(visibility ? { visibility } : {}),
relationshipId: String(record["r:id"] || "")
};
});
}
function workbookSheetVisibility(value: unknown): WorkbookSheetRef["visibility"] {
const normalized = String(value ?? "").trim().toLowerCase();
if (normalized === "hidden") {
return "hidden";
}
if (normalized === "veryhidden") {
return "veryHidden";
}
return undefined;
}
export function workbookActiveSheetId(workbookXml: unknown, sheets: Array<{ id: string }>): string {
const workbook = childRecord(workbookXml, "workbook");
const bookViews = childRecord(workbook, "bookViews");
@@ -705,17 +732,18 @@ function buildRootRelationshipsXml() {
);
}
function buildWorkbookXml(sheetNames: string[], activeSheetIndex = 0) {
const activeTab = Math.min(Math.max(0, activeSheetIndex), Math.max(0, sheetNames.length - 1));
function buildWorkbookXml(sheets: Array<{ name: string; visibility?: WorkbookSheetRef["visibility"] }>, activeSheetIndex = 0) {
const activeTab = Math.min(Math.max(0, activeSheetIndex), Math.max(0, sheets.length - 1));
const bookViews = `<bookViews><workbookView activeTab="${activeTab}"/></bookViews>`;
const sheets = sheetNames
.map((name, index) => {
return `<sheet name="${escapeXmlAttribute(name)}" sheetId="${index + 1}" r:id="rId${index + 1}"/>`;
const sheetXml = sheets
.map((sheet, index) => {
const visibility = sheet.visibility ? ` state="${sheet.visibility}"` : "";
return `<sheet name="${escapeXmlAttribute(sheet.name)}" sheetId="${index + 1}" r:id="rId${index + 1}"${visibility}/>`;
})
.join("");
return xmlDeclaration(
`<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">${bookViews}<sheets>${sheets}</sheets></workbook>`
`<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">${bookViews}<sheets>${sheetXml}</sheets></workbook>`
);
}