Preserve QExcell internal hyperlinks
This commit is contained in:
+69
-13
@@ -64,6 +64,13 @@ interface WorksheetCellRecord {
|
||||
cell: XmlRecord;
|
||||
}
|
||||
|
||||
interface HyperlinkEntry {
|
||||
address: string;
|
||||
target: string;
|
||||
external: boolean;
|
||||
location?: string;
|
||||
}
|
||||
|
||||
interface SharedFormulaDefinition {
|
||||
formula: string;
|
||||
baseAddress: string;
|
||||
@@ -183,7 +190,7 @@ export async function exportXlsxBlob(workbook: Workbook): Promise<Blob> {
|
||||
|
||||
sheets.forEach((sheet, index) => {
|
||||
const sheetChartParts = chartParts.filter((part) => part.sheetIndex === index);
|
||||
const drawingRelationshipId = sheetChartParts.length > 0 ? `rId${sortedHyperlinkEntries(sheet.hyperlinks).length + 1}` : "";
|
||||
const drawingRelationshipId = sheetChartParts.length > 0 ? `rId${sortedExternalHyperlinkEntries(sheet.hyperlinks).length + 1}` : "";
|
||||
zip.file(
|
||||
`xl/worksheets/sheet${index + 1}.xml`,
|
||||
buildWorksheetXml(sheet.cells, sheet.mergedCells, sheet.hyperlinks, sheet.cellFormats, styleRegistry, drawingRelationshipId)
|
||||
@@ -302,7 +309,7 @@ export function worksheetXmlToHyperlinks(worksheetXml: unknown, relationshipsXml
|
||||
const ref = normalizeCellAddress(String(record.ref || ""));
|
||||
const relationshipId = String(record["r:id"] || "");
|
||||
const location = String(record.location || "");
|
||||
const target = normalizedHyperlinkTarget(relationshipTargets[relationshipId] || (location ? `#${location}` : ""));
|
||||
const target = normalizedHyperlinkTarget(relationshipTargets[relationshipId] || "") || normalizedInternalHyperlinkTarget(location, true);
|
||||
if (ref && target) {
|
||||
links[ref] = target;
|
||||
}
|
||||
@@ -547,22 +554,30 @@ export function buildHyperlinksXml(hyperlinks: Record<string, string> = {}) {
|
||||
return "";
|
||||
}
|
||||
|
||||
let relationshipIndex = 0;
|
||||
const hyperlinkXml = refs
|
||||
.map(([address], index) => `<hyperlink ref="${escapeXmlAttribute(address)}" r:id="rId${index + 1}"/>`)
|
||||
.map((entry) => {
|
||||
if (!entry.external) {
|
||||
return `<hyperlink ref="${escapeXmlAttribute(entry.address)}" location="${escapeXmlAttribute(entry.location ?? "")}"/>`;
|
||||
}
|
||||
|
||||
relationshipIndex += 1;
|
||||
return `<hyperlink ref="${escapeXmlAttribute(entry.address)}" r:id="rId${relationshipIndex}"/>`;
|
||||
})
|
||||
.join("");
|
||||
return `<hyperlinks>${hyperlinkXml}</hyperlinks>`;
|
||||
}
|
||||
|
||||
export function buildWorksheetRelationshipsXml(hyperlinks: Record<string, string> = {}, drawingTarget = "") {
|
||||
const refs = sortedHyperlinkEntries(hyperlinks);
|
||||
const refs = sortedExternalHyperlinkEntries(hyperlinks);
|
||||
if (refs.length === 0 && !drawingTarget) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const relationships = refs
|
||||
.map(
|
||||
([, target], index) =>
|
||||
`<Relationship Id="rId${index + 1}" Type="${hyperlinkRelationshipType}" Target="${escapeXmlAttribute(target)}" TargetMode="External"/>`
|
||||
(entry, index) =>
|
||||
`<Relationship Id="rId${index + 1}" Type="${hyperlinkRelationshipType}" Target="${escapeXmlAttribute(entry.target)}" TargetMode="External"/>`
|
||||
)
|
||||
.join("");
|
||||
const drawingRelationship = drawingTarget
|
||||
@@ -1264,20 +1279,46 @@ function partDirectory(path: string) {
|
||||
|
||||
function sortedHyperlinkEntries(hyperlinks: Record<string, string> = {}) {
|
||||
return Object.entries(hyperlinks)
|
||||
.map(([address, target]) => [normalizeCellAddress(address), normalizedHyperlinkTarget(target)] as const)
|
||||
.filter((entry): entry is readonly [string, string] => Boolean(entry[0] && entry[1]))
|
||||
.filter(([address], index, entries) => entries.findIndex(([candidate]) => candidate === address) === index)
|
||||
.sort(([firstAddress], [secondAddress]) => {
|
||||
const firstRow = cellRowNumber(firstAddress);
|
||||
const secondRow = cellRowNumber(secondAddress);
|
||||
.map(([address, target]) => normalizedHyperlinkEntry(address, target))
|
||||
.filter((entry): entry is HyperlinkEntry => Boolean(entry))
|
||||
.filter((entry, index, entries) => entries.findIndex((candidate) => candidate.address === entry.address) === index)
|
||||
.sort((first, second) => {
|
||||
const firstRow = cellRowNumber(first.address);
|
||||
const secondRow = cellRowNumber(second.address);
|
||||
if (firstRow !== secondRow) {
|
||||
return firstRow - secondRow;
|
||||
}
|
||||
|
||||
return cellColumnNumber(firstAddress) - cellColumnNumber(secondAddress);
|
||||
return cellColumnNumber(first.address) - cellColumnNumber(second.address);
|
||||
});
|
||||
}
|
||||
|
||||
function sortedExternalHyperlinkEntries(hyperlinks: Record<string, string> = {}) {
|
||||
return sortedHyperlinkEntries(hyperlinks).filter((entry) => entry.external);
|
||||
}
|
||||
|
||||
function normalizedHyperlinkEntry(address: string, target: string): HyperlinkEntry | null {
|
||||
const normalizedAddress = normalizeCellAddress(address);
|
||||
const externalTarget = normalizedHyperlinkTarget(target);
|
||||
if (normalizedAddress && externalTarget) {
|
||||
return {
|
||||
address: normalizedAddress,
|
||||
target: externalTarget,
|
||||
external: true
|
||||
};
|
||||
}
|
||||
|
||||
const location = normalizedInternalHyperlinkLocation(target);
|
||||
return normalizedAddress && location
|
||||
? {
|
||||
address: normalizedAddress,
|
||||
target: `#${location}`,
|
||||
external: false,
|
||||
location
|
||||
}
|
||||
: null;
|
||||
}
|
||||
|
||||
function normalizeCellAddress(address: string) {
|
||||
const trimmed = address.trim().toUpperCase();
|
||||
return isCellAddress(trimmed) ? trimmed : "";
|
||||
@@ -1608,6 +1649,21 @@ function normalizedHyperlinkTarget(value?: string | null) {
|
||||
}
|
||||
}
|
||||
|
||||
function normalizedInternalHyperlinkTarget(value?: string | null, allowWithoutHash = false) {
|
||||
const location = normalizedInternalHyperlinkLocation(value, allowWithoutHash);
|
||||
return location ? `#${location}` : "";
|
||||
}
|
||||
|
||||
function normalizedInternalHyperlinkLocation(value?: string | null, allowWithoutHash = false) {
|
||||
const raw = value?.trim() ?? "";
|
||||
const location = raw.startsWith("#") ? raw.slice(1).trim() : allowWithoutHash ? raw : "";
|
||||
if (!location || /[\u0000-\u001F]/.test(location)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return location.slice(0, 2048);
|
||||
}
|
||||
|
||||
function sharedFormulaKey(formulaRecord: XmlRecord) {
|
||||
return formulaRecord.t === "shared" && formulaRecord.si !== undefined ? String(formulaRecord.si) : "";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user