Read QExcell workbook theme colors

This commit is contained in:
Курнат Андрей
2026-06-01 12:10:27 +03:00
parent c751fda372
commit 190752a7eb
3 changed files with 87 additions and 17 deletions
+51 -15
View File
@@ -182,7 +182,8 @@ export async function importXlsxFile(file: File): Promise<ImportedExcellWorkbook
const workbookXml = parser.parse(await readZipText(zip, "xl/workbook.xml"));
const relationshipsXml = parser.parse(await readZipText(zip, "xl/_rels/workbook.xml.rels"));
const sharedStrings = await readSharedStrings(zip, parser);
const styleTable = xlsxStylesXmlToCellFormatTable(await readOptionalWorkbookStyles(zip, parser));
const themeColors = await readOptionalWorkbookThemeColors(zip, parser);
const styleTable = xlsxStylesXmlToCellFormatTable(await readOptionalWorkbookStyles(zip, parser), themeColors);
const sheetRefs = workbookSheetRefs(workbookXml);
const relationshipTargets = workbookRelationshipTargets(relationshipsXml);
const sheets = await Promise.all(
@@ -901,12 +902,15 @@ export function createCellStyleRegistry(cellFormatsBySheet: Array<Record<string,
return { formats, idsByKey };
}
export function xlsxStylesXmlToCellFormatTable(stylesXml?: unknown): Array<CellFormat | undefined> {
export function xlsxStylesXmlToCellFormatTable(
stylesXml?: unknown,
themeColors: string[] = defaultXlsxThemeColors
): Array<CellFormat | undefined> {
const styleSheet = childRecord(stylesXml, "styleSheet");
const numberFormats = xlsxNumberFormats(styleSheet);
const fonts = toArray(childRecord(styleSheet, "fonts").font).map((font, index) => fontXmlToCellFormat(font, index));
const fills = toArray(childRecord(styleSheet, "fills").fill).map(fillXmlToCellFormat);
const borders = toArray(childRecord(styleSheet, "borders").border).map(borderXmlToCellFormat);
const fonts = toArray(childRecord(styleSheet, "fonts").font).map((font, index) => fontXmlToCellFormat(font, index, themeColors));
const fills = toArray(childRecord(styleSheet, "fills").fill).map((fill) => fillXmlToCellFormat(fill, themeColors));
const borders = toArray(childRecord(styleSheet, "borders").border).map((border) => borderXmlToCellFormat(border, themeColors));
return toArray(childRecord(styleSheet, "cellXfs").xf).map((xf) => {
const record = asRecord(xf);
@@ -1069,9 +1073,9 @@ function normalizedCellFormat(format: CellFormat | undefined): CellFormat | null
return Object.keys(normalized).length > 0 ? normalized : null;
}
function fontXmlToCellFormat(font: unknown, index = 0): CellFormat {
function fontXmlToCellFormat(font: unknown, index = 0, themeColors: string[] = defaultXlsxThemeColors): CellFormat {
const record = asRecord(font);
const textColor = colorRecordToHex(childRecord(record, "color"), { ignoreThemeAndIndexed: index === 0 });
const textColor = colorRecordToHex(childRecord(record, "color"), themeColors, { ignoreThemeAndIndexed: index === 0 });
const fontSize = normalizedFontSize(numberAttribute(childRecord(record, "sz").val));
const fontFamily = normalizedFontFamily(String(childRecord(record, "name").val || ""));
return (
@@ -1087,13 +1091,13 @@ function fontXmlToCellFormat(font: unknown, index = 0): CellFormat {
);
}
function fillXmlToCellFormat(fill: unknown): CellFormat {
function fillXmlToCellFormat(fill: unknown, themeColors: string[] = defaultXlsxThemeColors): CellFormat {
const patternFill = childRecord(fill, "patternFill");
const fillColor = colorRecordToHex(childRecord(patternFill, "fgColor"));
const fillColor = colorRecordToHex(childRecord(patternFill, "fgColor"), themeColors);
return fillColor ? { fillColor } : {};
}
function borderXmlToCellFormat(border: unknown): CellFormat {
function borderXmlToCellFormat(border: unknown, themeColors: string[] = defaultXlsxThemeColors): CellFormat {
const record = asRecord(border);
const borderedSides = ["left", "right", "top", "bottom"].filter((side) => {
const style = String(childRecord(record, side).style || childRecord(record, side)["@_style"] || "").trim().toLowerCase();
@@ -1101,7 +1105,7 @@ function borderXmlToCellFormat(border: unknown): CellFormat {
});
const borderColor = borderedSides
.map((side) => colorRecordToHex(childRecord(childRecord(record, side), "color")))
.map((side) => colorRecordToHex(childRecord(childRecord(record, side), "color"), themeColors))
.find(Boolean);
return normalizedCellFormat({
@@ -1142,7 +1146,11 @@ function normalizeFillColor(value?: string) {
return /^[0-9A-F]{6}$/.test(raw) ? raw : "";
}
function colorRecordToHex(record: XmlRecord, options: { ignoreThemeAndIndexed?: boolean } = {}) {
function colorRecordToHex(
record: XmlRecord,
themeColors: string[] = defaultXlsxThemeColors,
options: { ignoreThemeAndIndexed?: boolean } = {}
) {
const rgbColor = normalizeFillColor(String(record.rgb ?? ""));
if (rgbColor) {
return rgbColor;
@@ -1153,7 +1161,7 @@ function colorRecordToHex(record: XmlRecord, options: { ignoreThemeAndIndexed?:
}
if (record.theme !== undefined) {
const themeColor = themeColorToHex(numberAttribute(record.theme), Number.parseFloat(String(record.tint ?? "")));
const themeColor = themeColorToHex(numberAttribute(record.theme), Number.parseFloat(String(record.tint ?? "")), themeColors);
if (themeColor) {
return themeColor;
}
@@ -1162,8 +1170,8 @@ function colorRecordToHex(record: XmlRecord, options: { ignoreThemeAndIndexed?:
return record.indexed !== undefined ? indexedXlsxColors[numberAttribute(record.indexed)] ?? "" : "";
}
function themeColorToHex(themeIndex: number, tint = 0) {
const baseColor = defaultXlsxThemeColors[themeIndex];
function themeColorToHex(themeIndex: number, tint = 0, themeColors: string[] = defaultXlsxThemeColors) {
const baseColor = themeColors[themeIndex] || defaultXlsxThemeColors[themeIndex];
if (!baseColor) {
return "";
}
@@ -1267,6 +1275,34 @@ async function readOptionalWorkbookStyles(zip: ZipInstance, parser: InstanceType
return stylesFile ? parser.parse(await stylesFile.async("string")) : undefined;
}
async function readOptionalWorkbookThemeColors(zip: ZipInstance, parser: InstanceType<XmlParserConstructor>) {
const themeFile = zip.file("xl/theme/theme1.xml");
if (!themeFile) {
return defaultXlsxThemeColors;
}
return xlsxThemeXmlToColors(parser.parse(await themeFile.async("string")));
}
export function xlsxThemeXmlToColors(themeXml?: unknown) {
const colorScheme = firstRecordByLocalName(themeXml, "clrScheme");
const slots = ["dk1", "lt1", "dk2", "lt2", "accent1", "accent2", "accent3", "accent4", "accent5", "accent6", "hlink", "folHlink"];
const colors = slots.map((slot, index) => themeSlotColor(directChildByLocalName(colorScheme, slot)) || defaultXlsxThemeColors[index]);
return colors.some(Boolean) ? colors : defaultXlsxThemeColors;
}
function themeSlotColor(slot: XmlRecord) {
const srgbColor = firstRecordByLocalName(slot, "srgbClr");
const systemColor = firstRecordByLocalName(slot, "sysClr");
return normalizeFillColor(String(srgbColor.val || systemColor.lastClr || ""));
}
function directChildByLocalName(record: XmlRecord, localName: string) {
const entry = Object.entries(record).find(([key]) => key.split(":").pop() === localName);
return entry ? asRecord(toArray(entry[1])[0]) : {};
}
async function readOptionalSheetRelationships(zip: ZipInstance, parser: InstanceType<XmlParserConstructor>, sheetPath: string) {
const relationshipsPath = sheetRelationshipsPath(sheetPath);
const file = zip.file(relationshipsPath);