Skip to main content

grapher.write_workbook()

Write graph to a new .xlsx at destination.

Usage

Source

grapher.write_workbook(
    graph,
    destination,
    *,
    formula_style=FormulaStyle.A1_EXCEL,
    coerce_relative_refs=False,
    overwrite=False,
    include_defined_names=True,
    shared_formulas="auto",
    series_bindings=None,
    bindings_workbook=None,
    include_bound_labels=True,
    include_cell_validation=True
)

Accepts any GraphReadView: a mutable DependencyGraph (the workbook after set_node_ast / set_node_value / move_node / in-place compress_*) or a non-mutating ProjectionResult. Formula rewriting is an intended write-back use case; compressed or projected views are written as they are. Output contains only sheets and cells from the view. Styles, charts, VBA, and cells outside the view are omitted (accepted v1 lossiness). Vacated move_node addresses are simply absent; there is no template to clear. When series_bindings is set and include_bound_labels is True (default), row_label, column_header, kind: cell, and attribute source cells that bindings read are written too, even if they sit outside the target closure. Formula labels are copied only when every static dependency is already in the view or is itself a bound label; otherwise the writer fails closed rather than extracting a second dependency closure. The bindings workbook is opened read-only for those extra cells; it is never overwritten. When include_cell_validation is True (default), constrained inputs are also written as Excel data validations. Input-series domains and relations apply when series_bindings is set; extract-time cell_type_env domains still apply to unbound value leaves (conflicts fail closed). Constants, outputs, and formula cells are omitted. Without bindings, value cells in cell_type_env are used alone.

Two write orders: move then write persists current keys on this DependencyGraph (relatives already rewritten so resolved targets match the pre-move meaning). Project then write exports the projected clone. A ProjectionResult is a snapshot from project(); a later move_node on the original graph does not update it. Re-run projection after geometry edits to include them in a projected workbook.

Formula cells are spelled from current formula_ast via render_formula (never from the opt-in raw Node.formula audit string). Defined names are expanded to A1 before parse, so written formulas are expanded A1 (for example =A1*Settings!$B$2, not =A1*TaxRate). That is expected, not a bug. When include_defined_names is True (default), workbook-global names from graph.named_ranges and graph.named_range_ranges are written so aliases still exist in Excel. Names the writer cannot express as a cell or rectangle are refused. Scope is not stored on the graph maps, so names are emitted workbook-global. The writer does not invent a name from an expanded AST.

Leaves write node.value. Formula cells do not receive evaluator-computed cached results. Cells extracted as ArrayFormula are written back as ArrayFormula(text, ref=...) using the observed spill / CSE ref. fastpyxl does not distinguish legacy CSE from dynamic-array spills, so write-back emits t="array" for both. A flagged array cell with no observed ref is refused rather than written as a scalar formula.

When graph.formula_shapes is warm, contiguous autofill runs that share an interned relative shape become one Excel shared formula (t="shared"). The overlay is opt-in and is not rewarmed here; missing or stale shapes, gaps, mixed axes, array formulas, and INDIRECT stay per-cell A1. See shared_formulas.

Parameters

graph: GraphReadView

Read view whose cells are written.

destination: Path | str

Output path. Parent directories must already exist.

formula_style: FormulaStyle = FormulaStyle.A1_EXCEL

Reference spelling. Default A1_EXCEL keeps $ on absolute axes and omits the host sheet prefix. R1C1 is not persisted for normal formula cells; shared-formula groups use interned relative shapes (the R1C1 dialect) and store the master cell in A1, which is what Excel and fastpyxl persist on disk.

coerce_relative_refs: bool = False

If True, bind relative axes to absolute indexes before spelling ($ on both axes in A1_EXCEL). Shared-formula grouping is skipped (absolute fills are not a shared relative shape).

overwrite: bool = False

If False (default), raise when destination exists. The destination is always a new file; graph’s original workbook is never saved in place. bindings_workbook is opened read-only when bound labels are included (include_bound_labels is True).

include_defined_names: bool = True

If True (default), write named_ranges and named_range_ranges as workbook-global defined names. Set False to omit the name table.

shared_formulas: SharedFormulasMode = "auto"

Group interned autofill runs into Excel shared formulas (t="shared"). auto (default) groups when graph.formula_shapes is warm and skips grouping when it is missing. off always writes per-cell A1. require fails if the overlay is missing. The writer does not auto-rewarm (GitHub #560). project() drops the overlay on the clone; rewarm projection.projected_graph.formula_shapes before writing a ProjectionResult if you want grouping. Stale shapes, non-contiguous or mixed-axis leftovers, array formulas, and INDIRECT emit per-cell rather than an invalid shared formula.

series_bindings: WorkbookSeriesBindings | None = None

Optional sidecar used to locate bound labels and input domains for cell validation. Requires bindings_workbook when include_bound_labels is True, or when input series declare relations and cell validation is enabled.

bindings_workbook: Path | str | None = None

Workbook the sidecar describes. Required when bound labels are included, or when cell validation needs relation partners from the sidecar.

include_bound_labels: bool = True

If True (default) and series_bindings is set, write row_label / column_header / kind: cell / attribute source cells that are missing from graph. Formula labels whose static deps are not already in the view or bound labels are refused. The original graph is not mutated.

include_cell_validation: bool = True
If True (default), write Excel data validations for constrained inputs. With series_bindings, rules come from input-series enum, between, real_between, from_workbook, value_map needles, and relations, unioned with cell_type_env domains on unbound value leaves (conflicts fail closed). Constants, outputs, and internals are skipped. Without bindings, value cells in cell_type_env (including a projection’s projected graph) are used. Enum members that cannot form an inline list fall back to a custom formula. Formulas longer than 255 characters, relation partners missing from the written view, and from_workbook inputs with no cached value fail closed.

Raises

FileExistsError

If destination exists and overwrite is False.

ValueError
If the view is empty, spans multiple sheets without sheet_order, a formula cell has no formula_ast, formula_style is R1C1, a relative axis has no host address, an array formula is missing its observed spill / CSE ref, a defined name cannot be expressed as a cell or rectangle, shared_formulas is not a known mode, shared_formulas='require' and formula_shapes is missing, or series_bindings is set with include_bound_labels and without bindings_workbook, a bound formula label cannot be copied as a static overlay, or a constrained input cannot be written as Excel data validation.