Preserve QExcell active sheet in XLSX
This commit is contained in:
+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