SPB Git

Repository Public

ModelLayout.swift 3.5 KB 88 lines swift

1import Foundation2import SheetCore3import Statistics45/// A model laid out as sheet cells — labels, input numbers and LIVE formulas — ready to be written6/// into a worksheet. Addresses are absolute zero-based sheet coordinates.7public struct ModelLayout: Sendable, Hashable, Codable {8    public enum Content: Sendable, Hashable, Codable {9        case label(String)10        case number(Double)11        /// Formula text in canonical syntax, starting with "=".12        case formula(String)13    }1415    /// What the cell is for, so the app can style inputs, calculations and results differently.16    public enum Role: String, Sendable, Codable { case title, header, label, input, calculation, output }1718    public struct Cell: Sendable, Hashable, Codable {19        public var row: Int20        public var col: Int21        public var content: Content22        public var role: Role23        /// Suggested number format ("0.00%", "#,##0", …) or nil.24        public var numberFormat: String?25    }2627    public var cells: [Cell] = []28    /// Addresses of the key cells by name (e.g. "enterpriseValue", "wacc").29    public var names: [String: CellRef] = [:]3031    public init() {}3233    /// Bounding box of the layout.34    public var range: RangeRef? {35        guard let first = cells.first else { return nil }36        var r = RangeRef(CellRef(first.row, first.col))37        for c in cells { r = r.union(RangeRef(CellRef(c.row, c.col))) }38        return r39    }4041    /// Writes every cell into the sheet as one undoable transaction ("Insert Model"). Formulas are parsed42    /// with the canonical syntax; throws if one does not parse (which would be a bug in the layout).43    public func write(to engine: CalcEngine, sheet: Int) throws {44        var parsed = [(Int, Int, CellInput)]()45        parsed.reserveCapacity(cells.count)46        for c in cells {47            switch c.content {48            case .label(let s): parsed.append((c.row, c.col, .value(.text(s))))49            case .number(let d): parsed.append((c.row, c.col, .value(.number(d))))50            case .formula(let f):51                guard let e = try? FormulaParser.parse(f) else { throw StatError.invalidInput("layout formula does not parse: \(f)") }52                parsed.append((c.row, c.col, .formula(e)))53            }54        }55        engine.transaction("Insert Model") {56            for (r, c, input) in parsed { engine.set(sheet: sheet, row: r, col: c, input) }57        }58    }5960    /// The layout as a rectangular grid of values relative to its top-left corner; formulas appear as their text.61    public func grid() -> [[Value]] {62        guard let box = range else { return [] }63        var out = Array(repeating: Array(repeating: Value.blank, count: box.cols), count: box.rows)64        for c in cells {65            let v: Value66            switch c.content {67            case .label(let s): v = .text(s)68            case .number(let d): v = .number(d)69            case .formula(let f): v = .text(f)70            }71            out[c.row - box.r0][c.col - box.c0] = v72        }73        return out74    }7576    // MARK: Building7778    mutating func put(_ row: Int, _ col: Int, _ content: Content, _ role: Role, format: String? = nil, name: String? = nil) {79        cells.append(Cell(row: row, col: col, content: content, role: role, numberFormat: format))80        if let name { names[name] = CellRef(row, col) }81    }8283    /// "$B$4"84    static func abs(_ row: Int, _ col: Int) -> String { "$" + A1.columnName(col) + "$" + String(row + 1) }85    /// "B4"86    static func rel(_ row: Int, _ col: Int) -> String { A1.columnName(col) + String(row + 1) }87}88
Files main
Menu

Theme