/** * Utilities for manipulating the external-workbook prefix in formula strings. * * Excel formula strings containing external workbook references have the * shape `[]Sheet!Ref` where `` is either * * - a 1-based numeric index — `[1]Sheet1!A1` (the canonical on-disk form * stored inside `` elements of worksheet XML), or * - a filename / relative path — `[测试.xlsx]Sheet1!A1` (what Excel * displays in the formula bar; not part of the OOXML storage contract, * but produced by hand-written formulas and some older tools). * * When writing, excelts always emits the numeric form — indices map * positionally into the workbook's `` list. When a * formula arrives with the filename form, the writer assigns (or reuses) an * ExternalLinkModel with that filename as its `target` and rewrites the * formula to the numeric form. This matches how Excel itself stores formulas * and makes them round-trippable. * * The quoted variant `'[file.xlsx]Sheet with space'!A1` is handled too — Excel * wraps the `[name]Sheet` segment in single quotes when the sheet name needs * quoting. The matching logic here recognises both the unquoted and quoted * forms, rewriting inside the quotes when needed. * * Edge cases we explicitly *do not* treat as external refs: * - `[@Column]`, `[#Headers]`, `[Column Name]` — table structured refs * (no `]Sheet!` tail). The regex requires the `]!` follow-up, * which structured refs never have. * - Array literals `{1,2;3,4}` use `{}`, not `[]`. * - String literals `"[Book]Sheet!A1"` — handled by scanning only outside * string literal regions. */ /** * A single match of an external reference inside a formula string. The * writer uses `workbook` to find/create an ExternalLinkModel and `sheet` * for reporting / sheet-name upsert. `replacement` is the substring that * should replace `match` in the final formula (with the workbook token * rewritten to a numeric index). */ export interface ExternalRefMatch { /** Full matched prefix including trailing `!`, e.g. `[测试.xlsx]Sheet1!`. */ match: string; /** The workbook token inside `[]` — either numeric or a filename/path. */ workbook: string; /** Whether the workbook token was already a numeric index. */ numeric: boolean; /** The 1-based numeric index parsed from the workbook token, if numeric. */ index: number | null; /** The sheet name (unquoted). */ sheet: string; /** Whether the match came from the quoted variant `'[..]..'!`. */ quoted: boolean; /** Start offset in the source formula. */ start: number; /** End offset (exclusive) in the source formula. */ end: number; } /** * Scan a formula string for all external-workbook references. String * literals (inside `"..."`) are skipped so that a string value like * `"[Book]Sheet!A1"` is not misidentified as a ref. * * The returned matches are in source order. If a formula contains no * external refs, the array is empty. */ export declare function findExternalRefs(formula: string): ExternalRefMatch[]; /** * Replace every external-workbook token in `formula` using the supplied * resolver. The resolver is called once per match and returns the numeric * index to substitute; returning `null` leaves the match unchanged (useful * when the caller cannot resolve a particular filename). * * Returns the rewritten formula. Offsets inside the original formula are * adjusted correctly even when multiple rewrites change the total length. */ export declare function rewriteExternalRefs(formula: string, resolve: (match: ExternalRefMatch) => number | null): string;