Repository Public
1import Foundation2import SheetCore3import SheetUI4import AnalysisKit56/// The financial-modelling colour convention: inputs (assumptions) in blue on a light fill, formulas in the7/// default black, outputs in bold. One definition used by "Mark as Input / Output", the DCF and loan insertions8/// and the "Assumption cells" list.9public enum ModelStyle {10 public static let inputText = RGBA(hex: 0x1F4FD8)11 public static let inputFill = RGBA(hex: 0xEAF1FF)1213 public static func applyInput(_ s: inout CellStyle) { s.textColor = inputText; s.fill = inputFill }14 public static func applyOutput(_ s: inout CellStyle) { s.bold = true }15 public static func isInput(_ s: CellStyle) -> Bool { s.textColor == inputText && s.fill == inputFill }1617 /// Applies the input or output style to ranges. Call inside a transaction.18 public static func mark(engine: CalcEngine, sheet: Int, ranges: [RangeRef], asInput: Bool) {19 for rg in ranges { engine.setStyle(sheet: sheet, range: rg) { s in if asInput { applyInput(&s) } else { applyOutput(&s) } } }20 }2122 @MainActor public static func markSelection(_ session: WorkbookSession, asInput: Bool) {23 let ranges = session.selection.ranges, si = session.sheetIndex24 session.perform(asInput ? L("Mark as Input") : L("Mark as Output")) { mark(engine: $0, sheet: si, ranges: ranges, asInput: asInput) }25 session.statusMessage = asInput ? L("Selection marked as input (blue text, light fill)") : L("Selection marked as output (bold)")26 }2728 public struct AssumptionCell: Sendable, Hashable, Identifiable {29 public var id: CellRef { cell }30 public var cell: CellRef31 /// Text of the nearest label to the left on the same row ("" when none).32 public var label: String33 public var value: Value34 public var numberFormat: String35 public var isFormula: Bool36 }3738 /// Cells of a sheet carrying the input style and holding content, in reading order.39 public static func assumptionCells(workbook: Workbook, sheet si: Int, limit: Int = 2000) -> [AssumptionCell] {40 guard si >= 0, si < workbook.sheets.count else { return [] }41 let sh = workbook.sheets[si]42 var flags = [Int32: Bool]()43 var found = [CellRef]()44 sh.cells.forEachAll { r, c, cell in45 guard cell.hasContent else { return }46 let id = cell.style != 0 ? cell.style : sh.styleID(r, c)47 let isIn = flags[id] ?? { let v = isInput(workbook.styles[id]); flags[id] = v; return v }()48 if isIn { found.append(CellRef(r, c)) }49 }50 return found.sorted().prefix(limit).map { ref in51 var label = ""52 var c = ref.col - 153 while c >= 0, ref.col - c <= 12 {54 if case .text(let s) = sh.value(ref.row, c), !s.trimmingCharacters(in: .whitespaces).isEmpty { label = s; break }55 c -= 156 }57 let cell = sh.cell(ref.row, ref.col)58 return AssumptionCell(cell: ref, label: label, value: sh.value(ref.row, ref.col),59 numberFormat: workbook.styles[sh.styleID(ref.row, ref.col)].numberFormat, isFormula: cell?.formula != nil)60 }61 }62}6364/// Writer of labelled blocks on a sheet (title, headers, label/value rows) used by the model insertions.65struct BlockWriter {66 let engine: CalcEngine67 let sheet: Int68 var row: Int69 let col: Int70 static let accent = RGBA(hex: 0x0891B2)71 static let muted = RGBA(hex: 0x7A8098)7273 init(engine: CalcEngine, sheet: Int, origin: CellRef = CellRef(0, 0)) { self.engine = engine; self.sheet = sheet; row = origin.row; col = origin.col }7475 func style(_ r: Int, _ c: Int, _ f: (inout CellStyle) -> Void) { engine.setStyle(sheet: sheet, range: RangeRef(CellRef(r, c)), f) }76 func text(_ r: Int, _ c: Int, _ s: String) { engine.set(sheet: sheet, row: r, col: c, .value(.text(s))) }77 func number(_ r: Int, _ c: Int, _ x: Double, format: String) {78 engine.set(sheet: sheet, row: r, col: c, .value(.number(x)))79 style(r, c) { $0.numberFormat = format }80 }81 /// Writes a formula; returns false when it does not parse.82 @discardableResult func formula(_ r: Int, _ c: Int, _ f: String, format: String) -> Bool {83 guard let e = try? FormulaParser.parse(f) else { return false }84 engine.set(sheet: sheet, row: r, col: c, .formula(e))85 style(r, c) { $0.numberFormat = format }86 return true87 }8889 mutating func title(_ s: String) { text(row, col, s); style(row, col) { $0.bold = true; $0.fontSize = 15 }; row += 1 }90 mutating func heading(_ s: String) { text(row, col, s); style(row, col) { $0.bold = true; $0.textColor = BlockWriter.accent }; row += 1 }91 mutating func note(_ s: String) { text(row, col, s); style(row, col) { $0.italic = true; $0.textColor = BlockWriter.muted }; row += 1 }92 mutating func skip() { row += 1 }9394 /// "label | input value" row; returns the absolute address of the value cell ("$B$4").95 mutating func input(_ label: String, _ x: Double, format: String) -> String {96 text(row, col, label)97 number(row, col + 1, x, format: format)98 style(row, col + 1) { ModelStyle.applyInput(&$0) }99 defer { row += 1 }100 return "$" + A1.columnName(col + 1) + "$" + String(row + 1)101 }102 mutating func textRow(_ label: String, _ value: String) {103 text(row, col, label); text(row, col + 1, value); style(row, col + 1) { $0.hAlign = .right }104 row += 1105 }106 mutating func valueRow(_ label: String, _ x: Double, format: String, bold: Bool = false) {107 text(row, col, label); number(row, col + 1, x, format: format)108 if bold { style(row, col + 1) { ModelStyle.applyOutput(&$0) } }109 row += 1110 }111 mutating func formulaRow(_ label: String, _ f: String, format: String, bold: Bool = false) -> String {112 text(row, col, label); formula(row, col + 1, f, format: format)113 if bold { style(row, col + 1) { ModelStyle.applyOutput(&$0) } }114 defer { row += 1 }115 return "$" + A1.columnName(col + 1) + "$" + String(row + 1)116 }117 /// Bold header row with a bottom rule.118 mutating func header(_ labels: [String]) {119 for (i, l) in labels.enumerated() { text(row, col + i, l) }120 engine.setStyle(sheet: sheet, range: RangeRef(r0: row, c0: col, r1: row, c1: col + labels.count - 1)) { s in121 s.bold = true; s.borders.bottom = BorderEdge(style: .thin, color: BlockWriter.accent)122 }123 if labels.count > 1 { engine.setStyle(sheet: sheet, range: RangeRef(r0: row, c0: col + 1, r1: row, c1: col + labels.count - 1)) { $0.hAlign = .right } }124 row += 1125 }126}127