Preserve QExcell active sheet in XLSX
This commit is contained in:
@@ -433,7 +433,7 @@ export function QExcell({ workbook, onChange }: QExcellProps) {
|
||||
onChange({
|
||||
...workbook,
|
||||
title: imported.title,
|
||||
activeSheet: imported.sheets[0]?.id ?? "sheet-1",
|
||||
activeSheet: imported.activeSheet ?? imported.sheets[0]?.id ?? "sheet-1",
|
||||
sheets: imported.sheets.length > 0 ? imported.sheets : [{ id: "sheet-1", name: "Лист 1", cells: {}, mergedCells: [] }]
|
||||
});
|
||||
} catch (error) {
|
||||
|
||||
@@ -12,6 +12,7 @@ import {
|
||||
importXlsxFile,
|
||||
chartDataPoints,
|
||||
chartXmlToSheetChart,
|
||||
workbookActiveSheetId,
|
||||
workbookRelationshipTargets,
|
||||
workbookSheetRefs,
|
||||
worksheetXmlToCells,
|
||||
@@ -519,6 +520,39 @@ describe("excellOffice OOXML helpers", () => {
|
||||
}
|
||||
})
|
||||
).toEqual({ rId3: "worksheets/sheet7.xml" });
|
||||
|
||||
expect(
|
||||
workbookActiveSheetId(
|
||||
{
|
||||
workbook: {
|
||||
bookViews: { workbookView: { activeTab: "1" } }
|
||||
}
|
||||
},
|
||||
[
|
||||
{ id: "sheet-1" },
|
||||
{ id: "sheet-7" }
|
||||
]
|
||||
)
|
||||
).toBe("sheet-7");
|
||||
});
|
||||
|
||||
it("сохраняет и импортирует активный лист XLSX через workbookView activeTab", async () => {
|
||||
const blob = await exportXlsxBlob({
|
||||
id: "book-1",
|
||||
title: "Активный лист.xlsx",
|
||||
updatedAt: "2026-06-01T00:00:00.000Z",
|
||||
activeSheet: "sheet-2",
|
||||
sheets: [
|
||||
{ id: "sheet-1", name: "Первый", cells: { A1: "Первый" } },
|
||||
{ id: "sheet-2", name: "Второй", cells: { A1: "Второй" } }
|
||||
]
|
||||
});
|
||||
const zip = await JSZip.loadAsync(await blob.arrayBuffer());
|
||||
const workbookXml = await zip.file("xl/workbook.xml")?.async("string");
|
||||
const imported = await importXlsxFile(new File([blob], "Активный лист.xlsx", { type: blob.type }));
|
||||
|
||||
expect(workbookXml).toContain('<workbookView activeTab="1"/>');
|
||||
expect(imported.activeSheet).toBe("sheet-2");
|
||||
});
|
||||
|
||||
it("создает OOXML chart part для диаграммы XLSX", () => {
|
||||
|
||||
+21
-3
@@ -4,6 +4,7 @@ import { officeTitleFromFileName, readOfficeFileAsArrayBuffer, XLSX_MIME_TYPE }
|
||||
|
||||
export interface ImportedExcellWorkbook {
|
||||
title: string;
|
||||
activeSheet?: string;
|
||||
sheets: Array<{
|
||||
id: string;
|
||||
name: string;
|
||||
@@ -201,9 +202,11 @@ export async function importXlsxFile(file: File): Promise<ImportedExcellWorkbook
|
||||
return { ...parsedSheet, charts };
|
||||
})
|
||||
);
|
||||
const activeSheet = workbookActiveSheetId(workbookXml, sheets);
|
||||
|
||||
return {
|
||||
title: officeTitleFromFileName(file.name, "Книга QExcell.xlsx"),
|
||||
...(activeSheet ? { activeSheet } : {}),
|
||||
sheets
|
||||
};
|
||||
}
|
||||
@@ -215,12 +218,13 @@ export async function exportXlsxBlob(workbook: Workbook): Promise<Blob> {
|
||||
|
||||
const styleRegistry = createCellStyleRegistry(sheets.map((sheet) => sheet.cellFormats));
|
||||
const chartParts = chartPartRefsForSheets(sheets);
|
||||
const activeSheetIndex = Math.max(0, sheets.findIndex((sheet) => sheet.id === workbook.activeSheet));
|
||||
|
||||
zip.file("[Content_Types].xml", buildContentTypesXml(sheets.length, chartParts));
|
||||
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}`)));
|
||||
zip.file("xl/workbook.xml", buildWorkbookXml(sheets.map((sheet, index) => sheet.name.trim() || `Лист ${index + 1}`), activeSheetIndex));
|
||||
zip.file("xl/_rels/workbook.xml.rels", buildWorkbookRelationshipsXml(sheets.length));
|
||||
zip.file("xl/styles.xml", buildStylesXml(styleRegistry));
|
||||
|
||||
@@ -636,6 +640,18 @@ export function workbookSheetRefs(workbookXml: unknown): WorkbookSheetRef[] {
|
||||
});
|
||||
}
|
||||
|
||||
export function workbookActiveSheetId(workbookXml: unknown, sheets: Array<{ id: string }>): string {
|
||||
const workbook = childRecord(workbookXml, "workbook");
|
||||
const bookViews = childRecord(workbook, "bookViews");
|
||||
const workbookView = asRecord(toArray(bookViews.workbookView)[0]);
|
||||
const activeTab = Number.parseInt(String(workbookView.activeTab ?? workbookView["@_activeTab"] ?? ""), 10);
|
||||
if (!Number.isInteger(activeTab) || activeTab < 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return sheets[activeTab]?.id ?? "";
|
||||
}
|
||||
|
||||
export function workbookRelationshipTargets(relationshipsXml: unknown) {
|
||||
const relationships = childRecord(relationshipsXml, "Relationships");
|
||||
const targets: Record<string, string> = {};
|
||||
@@ -689,7 +705,9 @@ function buildRootRelationshipsXml() {
|
||||
);
|
||||
}
|
||||
|
||||
function buildWorkbookXml(sheetNames: string[]) {
|
||||
function buildWorkbookXml(sheetNames: string[], activeSheetIndex = 0) {
|
||||
const activeTab = Math.min(Math.max(0, activeSheetIndex), Math.max(0, sheetNames.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}"/>`;
|
||||
@@ -697,7 +715,7 @@ function buildWorkbookXml(sheetNames: string[]) {
|
||||
.join("");
|
||||
|
||||
return xmlDeclaration(
|
||||
`<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheets>${sheets}</sheets></workbook>`
|
||||
`<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">${bookViews}<sheets>${sheets}</sheets></workbook>`
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user