Repository Public
1import Foundation2import SheetCore34/// A cell of a workbook model: sheet index (position in `Workbook.sheets`) and coordinate.5public struct ModelCell: Hashable, Sendable, Codable {6 public var sheet: Int7 public var cell: CellRef89 public init(sheet: Int = 0, cell: CellRef) { self.sheet = sheet; self.cell = cell }10 public init(sheet: Int = 0, row: Int, col: Int) { self.sheet = sheet; self.cell = CellRef(row, col) }11 /// From A1 notation ("B3"); nil when the text is not a cell address.12 public init?(sheet: Int = 0, a1: String) {13 guard let c = CellRef(a1: a1) else { return nil }14 self.sheet = sheet; self.cell = c15 }1617 /// "Sheet1!B3" (or "#REF!B3" when the sheet index is out of range).18 public func label(in workbook: Workbook) -> String {19 guard sheet >= 0, sheet < workbook.sheets.count else { return "#REF!" + cell.a1 }20 return FormulaPrinter.quoteSheet(workbook.sheets[sheet].name) + "!" + cell.a121 }2223 func isValid(in workbook: Workbook) -> Bool { sheet >= 0 && sheet < workbook.sheets.count && cell.isValid }24}2526/// Errors reported by the what-if tools before or while they run.27public enum WhatIfError: Error, Sendable, Hashable, CustomStringConvertible {28 case invalidCell(String)29 case invalidOptions(String)30 case invalidFormula(String)3132 public var description: String {33 switch self {34 case .invalidCell(let m): "Invalid cell: \(m)"35 case .invalidOptions(let m): "Invalid options: \(m)"36 case .invalidFormula(let m): "Invalid formula: \(m)"37 }38 }39}4041/// How a what-if tool keeps its temporary edits away from the user's document.42public enum WhatIfIsolation: String, Sendable, Codable, CaseIterable {43 /// The tool works on a private deep copy of the workbook with its own engine. The live workbook,44 /// its undo and redo stacks and its change count are never touched. Costs one copy of the cells.45 case clone46 /// The tool edits the live workbook inside one transaction and undoes it when it finishes.47 /// Values, formulas and the undo stack are restored exactly, but SheetCore offers no way to48 /// suspend undo recording, so the REDO stack is replaced by one "What-If" entry and49 /// `changeCount` grows by 2. Do not use while another transaction is open.50 case inPlace51}52