#include "engine/workbook.hpp" #include "engine/dag.hpp" #include "engine/evaluator.hpp" #include "engine/format.hpp" #include #include #include #include namespace htab { namespace { constexpr size_t kMaxUndoDepth = 200; bool try_parse_plain_number(std::string_view raw, double& val_out, bool& is_pct_out) { std::string_view sv = trim_ascii(raw); if (sv.empty()) return false; is_pct_out = false; if (sv.back() == '%') { is_pct_out = true; sv.remove_suffix(1); sv = trim_ascii(sv); if (sv.empty()) return false; } size_t i = 0; if (sv[i] == '+' || sv[i] == '-') ++i; bool has_digits = false; while (i < sv.size() && std::isdigit(static_cast(sv[i]))) { has_digits = true; ++i; } if (i < sv.size() && sv[i] == '.') { ++i; while (i < sv.size() && std::isdigit(static_cast(sv[i]))) { has_digits = true; ++i; } } if (!has_digits) return false; if (i < sv.size() && (sv[i] == 'e' || sv[i] == 'E')) { ++i; if (i < sv.size() && (sv[i] == '+' || sv[i] == '-')) ++i; bool exp_digits = false; while (i < sv.size() && std::isdigit(static_cast(sv[i]))) { exp_digits = true; ++i; } if (!exp_digits) return false; } if (i != sv.size()) return false; std::string tmp(sv); char* endptr = nullptr; double val = std::strtod(tmp.c_str(), &endptr); if (endptr != tmp.c_str() + tmp.size() || !std::isfinite(val)) return false; if (is_pct_out) val /= 100.0; val_out = (val == 0.0) ? 0.0 : val; return true; } struct CellDelta { int row = 0; int col = 0; std::string old_raw; std::string old_fmt; std::string new_raw; std::string new_fmt; }; } // namespace class EditCellsCmd : public Workbook::Command { public: EditCellsCmd(int sheet_idx, std::vector deltas) : sheet_idx_(sheet_idx), deltas_(std::move(deltas)) {} void undo(Workbook& wb) override { std::vector seeds; seeds.reserve(deltas_.size()); for (const auto& d : deltas_) { wb.set_cell_raw_no_recalc(sheet_idx_, d.row, d.col, d.old_raw, d.old_fmt, seeds); } wb.dag_->recalculate_from(wb, std::move(seeds)); } void redo(Workbook& wb) override { std::vector seeds; seeds.reserve(deltas_.size()); for (const auto& d : deltas_) { wb.set_cell_raw_no_recalc(sheet_idx_, d.row, d.col, d.new_raw, d.new_fmt, seeds); } wb.dag_->recalculate_from(wb, std::move(seeds)); } private: int sheet_idx_ = 0; std::vector deltas_; }; class SnapshotCmd : public Workbook::Command { public: SnapshotCmd(std::vector before_sheets, int before_active, std::vector after_sheets, int after_active) : before_sheets_(std::move(before_sheets)), before_active_(before_active), after_sheets_(std::move(after_sheets)), after_active_(after_active) {} void undo(Workbook& wb) override { wb.sheets_ = before_sheets_; wb.active_sheet_ = std::clamp(before_active_, 0, wb.sheet_count() - 1); wb.dag_->rebuild(wb); } void redo(Workbook& wb) override { wb.sheets_ = after_sheets_; wb.active_sheet_ = std::clamp(after_active_, 0, wb.sheet_count() - 1); wb.dag_->rebuild(wb); } private: std::vector before_sheets_; int before_active_ = 0; std::vector after_sheets_; int after_active_ = 0; }; class DimEditCmd : public Workbook::Command { public: DimEditCmd(int sheet_idx, bool is_col, int index, int old_val, int new_val) : sheet_idx_(sheet_idx), is_col_(is_col), index_(index), old_val_(old_val), new_val_(new_val) {} void undo(Workbook& wb) override { apply(wb, old_val_); } void redo(Workbook& wb) override { apply(wb, new_val_); } private: void apply(Workbook& wb, int val) { if (sheet_idx_ < 0 || sheet_idx_ >= wb.sheet_count()) return; auto& map = is_col_ ? wb.sheet_mut(sheet_idx_).col_widths : wb.sheet_mut(sheet_idx_).row_heights; if (val <= 0) map.erase(index_); else map[index_] = val; } int sheet_idx_ = 0; bool is_col_ = false; int index_ = 0; int old_val_ = 0; int new_val_ = 0; }; Workbook::Workbook() : dag_(std::make_unique()) { Sheet s; s.name = "Sheet1"; sheets_.push_back(std::move(s)); } Workbook::~Workbook() = default; Workbook::Workbook(Workbook&&) noexcept = default; Workbook& Workbook::operator=(Workbook&&) noexcept = default; void Workbook::set_active_sheet(int idx) { if (idx >= 0 && idx < sheet_count()) { active_sheet_ = idx; } } int Workbook::find_sheet(std::string_view name) const { for (size_t i = 0; i < sheets_.size(); ++i) { if (iequals_ascii(sheets_[i].name, name)) { return static_cast(i); } } return -1; } void Workbook::push_command(std::unique_ptr cmd) { redo_stack_.clear(); undo_stack_.push_back(std::move(cmd)); if (undo_stack_.size() > kMaxUndoDepth) { undo_stack_.erase(undo_stack_.begin()); } } void Workbook::clear_history() { undo_stack_.clear(); redo_stack_.clear(); } bool Workbook::undo() { if (undo_stack_.empty()) return false; auto cmd = std::move(undo_stack_.back()); undo_stack_.pop_back(); cmd->undo(*this); redo_stack_.push_back(std::move(cmd)); return true; } bool Workbook::redo() { if (redo_stack_.empty()) return false; auto cmd = std::move(redo_stack_.back()); redo_stack_.pop_back(); cmd->redo(*this); undo_stack_.push_back(std::move(cmd)); return true; } int Workbook::add_sheet(std::string name) { auto before_sheets = sheets_; int before_active = active_sheet_; if (name.empty()) { int n = sheet_count() + 1; while (true) { std::string cand = "Sheet" + std::to_string(n); if (find_sheet(cand) < 0) { name = std::move(cand); break; } ++n; } } else if (find_sheet(name) >= 0) { return -1; } Sheet s; s.name = std::move(name); sheets_.push_back(std::move(s)); int idx = sheet_count() - 1; dag_->rebuild(*this); push_command(std::make_unique( std::move(before_sheets), before_active, sheets_, active_sheet_)); return idx; } bool Workbook::rename_sheet(int sheet_idx, const std::string& new_name) { if (sheet_idx < 0 || sheet_idx >= sheet_count()) return false; std::string trimmed(trim_ascii(new_name)); if (trimmed.empty()) return false; int existing = find_sheet(trimmed); if (existing >= 0 && existing != sheet_idx) return false; if (sheets_[static_cast(sheet_idx)].name == trimmed) return true; auto before_sheets = sheets_; int before_active = active_sheet_; std::string old_name = sheets_[static_cast(sheet_idx)].name; sheets_[static_cast(sheet_idx)].name = trimmed; rewrite_all_formulas(old_name, StructuralOp::RenameSheet, 0, 0, trimmed); dag_->rebuild(*this); push_command(std::make_unique( std::move(before_sheets), before_active, sheets_, active_sheet_)); return true; } bool Workbook::delete_sheet(int sheet_idx) { if (sheet_count() <= 1 || sheet_idx < 0 || sheet_idx >= sheet_count()) return false; auto before_sheets = sheets_; int before_active = active_sheet_; std::string deleted_name = sheets_[static_cast(sheet_idx)].name; sheets_.erase(sheets_.begin() + sheet_idx); if (active_sheet_ >= sheet_count()) { active_sheet_ = sheet_count() - 1; } else if (active_sheet_ > sheet_idx) { --active_sheet_; } rewrite_all_formulas(deleted_name, StructuralOp::DeleteSheet, 0, 0); dag_->rebuild(*this); push_command(std::make_unique( std::move(before_sheets), before_active, sheets_, active_sheet_)); return true; } void Workbook::parse_cell_input(Cell& cell, const std::string& raw) { cell.raw_input = raw; cell.ast.reset(); cell.parse_error = false; if (raw.empty()) { cell.value = Value::make_empty(); return; } if (raw[0] == '=') { auto res = Parser::parse_formula(raw); if (res.ok) { cell.ast = std::move(res.ast); } else { cell.parse_error = true; cell.value = Value::make_error(ErrorCode::Value); } return; } if (raw[0] == '\'') { cell.value = Value::make_string(raw.substr(1)); return; } auto trimmed = trim_ascii(raw); if (iequals_ascii(trimmed, "TRUE")) { cell.value = Value::make_bool(true); return; } if (iequals_ascii(trimmed, "FALSE")) { cell.value = Value::make_bool(false); return; } if (auto err = parse_error_code(trimmed)) { cell.value = Value::make_error(*err); return; } double num = 0.0; bool is_pct = false; if (try_parse_plain_number(raw, num, is_pct)) { cell.value = Value::make_number(num); if (is_pct && cell.number_format.empty()) { cell.number_format = "0.00%"; } return; } cell.value = Value::make_string(raw); } void Workbook::set_cell_raw_no_recalc(int sheet_idx, int row, int col, const std::string& raw, const std::optional& fmt_override, std::vector& dirty_seeds) { if (sheet_idx < 0 || sheet_idx >= sheet_count() || row < 0 || row >= kMaxRows || col < 0 || col >= kMaxCols) { return; } Sheet& s = sheets_[static_cast(sheet_idx)]; CellKey key{row, col}; GlobalAddr addr{sheet_idx, row, col}; std::string target_fmt; if (fmt_override.has_value()) { target_fmt = *fmt_override; } else { auto it = s.cells.find(key); if (it != s.cells.end()) target_fmt = it->second.number_format; } if (raw.empty() && target_fmt.empty()) { s.cells.erase(key); dag_->remove_cell(addr); dirty_seeds.push_back(addr); return; } Cell& cell = s.cells[key]; cell.number_format = std::move(target_fmt); parse_cell_input(cell, raw); if (cell.ast) { dag_->update_cell_deps(*this, addr, cell.ast.get()); } else { dag_->remove_cell(addr); } dirty_seeds.push_back(addr); } void Workbook::set_cell_input(int sheet_idx, int row, int col, const std::string& raw) { apply_cell_edits(sheet_idx, {CellEdit{row, col, raw, std::nullopt}}); } void Workbook::apply_cell_edits(int sheet_idx, const std::vector& edits) { if (sheet_idx < 0 || sheet_idx >= sheet_count() || edits.empty()) return; std::vector deltas; deltas.reserve(edits.size()); std::vector seeds; seeds.reserve(edits.size()); for (const auto& ed : edits) { if (ed.row < 0 || ed.row >= kMaxRows || ed.col < 0 || ed.col >= kMaxCols) continue; const Cell* existing = get_cell(sheet_idx, ed.row, ed.col); CellDelta delta; delta.row = ed.row; delta.col = ed.col; delta.old_raw = existing ? existing->raw_input : ""; delta.old_fmt = existing ? existing->number_format : ""; delta.new_raw = ed.raw_input; set_cell_raw_no_recalc(sheet_idx, ed.row, ed.col, ed.raw_input, ed.number_format, seeds); const Cell* after = get_cell(sheet_idx, ed.row, ed.col); delta.new_fmt = after ? after->number_format : ""; if (delta.old_raw != delta.new_raw || delta.old_fmt != delta.new_fmt) { deltas.push_back(std::move(delta)); } } if (!seeds.empty()) { dag_->recalculate_from(*this, std::move(seeds)); } if (!deltas.empty()) { push_command(std::make_unique(sheet_idx, std::move(deltas))); } } void Workbook::set_cell_format(int sheet_idx, int row, int col, const std::string& fmt) { apply_range_format(sheet_idx, CellRange{sheet_idx, row, col, row, col}, fmt); } void Workbook::apply_range_format(int sheet_idx, CellRange range, const std::string& fmt) { if (sheet_idx < 0 || sheet_idx >= sheet_count()) return; int r1 = std::max(0, std::min(range.r1, range.r2)); int r2 = std::min(kMaxRows - 1, std::max(range.r1, range.r2)); int c1 = std::max(0, std::min(range.c1, range.c2)); int c2 = std::min(kMaxCols - 1, std::max(range.c1, range.c2)); if (static_cast(r2 - r1 + 1) * (c2 - c1 + 1) > 10000) return; std::vector deltas; for (int r = r1; r <= r2; ++r) { for (int c = c1; c <= c2; ++c) { const Cell* existing = get_cell(sheet_idx, r, c); std::string raw = existing ? existing->raw_input : ""; std::string old_fmt = existing ? existing->number_format : ""; if (old_fmt == fmt) continue; CellDelta d; d.row = r; d.col = c; d.old_raw = raw; d.old_fmt = old_fmt; d.new_raw = raw; d.new_fmt = fmt; if (raw.empty() && fmt.empty()) { sheets_[static_cast(sheet_idx)].cells.erase(CellKey{r, c}); } else { Cell& cell = sheets_[static_cast(sheet_idx)].cells[CellKey{r, c}]; cell.number_format = fmt; } deltas.push_back(std::move(d)); } } if (!deltas.empty()) { push_command(std::make_unique(sheet_idx, std::move(deltas))); } } void Workbook::rewrite_all_formulas(const std::string& target_sheet, StructuralOp op, int index, int count, const std::string& new_name) { for (size_t si = 0; si < sheets_.size(); ++si) { Sheet& s = sheets_[si]; for (auto& [key, cell] : s.cells) { if (!cell.raw_input.empty() && cell.raw_input[0] == '=') { std::string updated = rewrite_formula_structural( cell.raw_input, s.name, target_sheet, op, index, count, new_name); if (updated != cell.raw_input) { parse_cell_input(cell, updated); } } } } } void Workbook::insert_rows(int sheet_idx, int row, int count) { if (sheet_idx < 0 || sheet_idx >= sheet_count() || row < 0 || count <= 0) return; auto before_sheets = sheets_; int before_active = active_sheet_; Sheet& s = sheets_[static_cast(sheet_idx)]; std::string target_name = s.name; std::unordered_map moved; moved.reserve(s.cells.size()); for (auto& [key, cell] : s.cells) { int nr = (key.row >= row) ? (key.row + count) : key.row; if (nr < kMaxRows) { moved.emplace(CellKey{nr, key.col}, std::move(cell)); } } s.cells = std::move(moved); std::unordered_map moved_h; for (const auto& [r, h] : s.row_heights) { int nr = (r >= row) ? (r + count) : r; if (nr < kMaxRows) moved_h[nr] = h; } s.row_heights = std::move(moved_h); rewrite_all_formulas(target_name, StructuralOp::InsertRow, row, count); dag_->rebuild(*this); push_command(std::make_unique( std::move(before_sheets), before_active, sheets_, active_sheet_)); } void Workbook::delete_rows(int sheet_idx, int row, int count) { if (sheet_idx < 0 || sheet_idx >= sheet_count() || row < 0 || count <= 0) return; auto before_sheets = sheets_; int before_active = active_sheet_; Sheet& s = sheets_[static_cast(sheet_idx)]; std::string target_name = s.name; std::unordered_map moved; moved.reserve(s.cells.size()); for (auto& [key, cell] : s.cells) { if (key.row >= row && key.row < row + count) continue; int nr = (key.row >= row + count) ? (key.row - count) : key.row; moved.emplace(CellKey{nr, key.col}, std::move(cell)); } s.cells = std::move(moved); std::unordered_map moved_h; for (const auto& [r, h] : s.row_heights) { if (r >= row && r < row + count) continue; int nr = (r >= row + count) ? (r - count) : r; moved_h[nr] = h; } s.row_heights = std::move(moved_h); rewrite_all_formulas(target_name, StructuralOp::DeleteRow, row, count); dag_->rebuild(*this); push_command(std::make_unique( std::move(before_sheets), before_active, sheets_, active_sheet_)); } void Workbook::insert_cols(int sheet_idx, int col, int count) { if (sheet_idx < 0 || sheet_idx >= sheet_count() || col < 0 || count <= 0) return; auto before_sheets = sheets_; int before_active = active_sheet_; Sheet& s = sheets_[static_cast(sheet_idx)]; std::string target_name = s.name; std::unordered_map moved; moved.reserve(s.cells.size()); for (auto& [key, cell] : s.cells) { int nc = (key.col >= col) ? (key.col + count) : key.col; if (nc < kMaxCols) { moved.emplace(CellKey{key.row, nc}, std::move(cell)); } } s.cells = std::move(moved); std::unordered_map moved_w; for (const auto& [c, w] : s.col_widths) { int nc = (c >= col) ? (c + count) : c; if (nc < kMaxCols) moved_w[nc] = w; } s.col_widths = std::move(moved_w); rewrite_all_formulas(target_name, StructuralOp::InsertCol, col, count); dag_->rebuild(*this); push_command(std::make_unique( std::move(before_sheets), before_active, sheets_, active_sheet_)); } void Workbook::delete_cols(int sheet_idx, int col, int count) { if (sheet_idx < 0 || sheet_idx >= sheet_count() || col < 0 || count <= 0) return; auto before_sheets = sheets_; int before_active = active_sheet_; Sheet& s = sheets_[static_cast(sheet_idx)]; std::string target_name = s.name; std::unordered_map moved; moved.reserve(s.cells.size()); for (auto& [key, cell] : s.cells) { if (key.col >= col && key.col < col + count) continue; int nc = (key.col >= col + count) ? (key.col - count) : key.col; moved.emplace(CellKey{key.row, nc}, std::move(cell)); } s.cells = std::move(moved); std::unordered_map moved_w; for (const auto& [c, w] : s.col_widths) { if (c >= col && c < col + count) continue; int nc = (c >= col + count) ? (c - count) : c; moved_w[nc] = w; } s.col_widths = std::move(moved_w); rewrite_all_formulas(target_name, StructuralOp::DeleteCol, col, count); dag_->rebuild(*this); push_command(std::make_unique( std::move(before_sheets), before_active, sheets_, active_sheet_)); } void Workbook::set_col_width(int sheet_idx, int col, int width) { if (sheet_idx < 0 || sheet_idx >= sheet_count() || col < 0 || col >= kMaxCols) return; auto& cw = sheets_[static_cast(sheet_idx)].col_widths; int old_w = cw.count(col) ? cw[col] : 0; if (old_w == width) return; if (width <= 0) cw.erase(col); else cw[col] = width; push_command(std::make_unique(sheet_idx, true, col, old_w, width)); } int Workbook::get_col_width(int sheet_idx, int col, int default_width) const { if (sheet_idx < 0 || sheet_idx >= sheet_count()) return default_width; const auto& cw = sheets_[static_cast(sheet_idx)].col_widths; auto it = cw.find(col); return (it != cw.end() && it->second > 0) ? it->second : default_width; } void Workbook::set_row_height(int sheet_idx, int row, int height) { if (sheet_idx < 0 || sheet_idx >= sheet_count() || row < 0 || row >= kMaxRows) return; auto& rh = sheets_[static_cast(sheet_idx)].row_heights; int old_h = rh.count(row) ? rh[row] : 0; if (old_h == height) return; if (height <= 0) rh.erase(row); else rh[row] = height; push_command(std::make_unique(sheet_idx, false, row, old_h, height)); } int Workbook::get_row_height(int sheet_idx, int row, int default_height) const { if (sheet_idx < 0 || sheet_idx >= sheet_count()) return default_height; const auto& rh = sheets_[static_cast(sheet_idx)].row_heights; auto it = rh.find(row); return (it != rh.end() && it->second > 0) ? it->second : default_height; } const Cell* Workbook::get_cell(int sheet_idx, int row, int col) const { if (sheet_idx < 0 || sheet_idx >= sheet_count()) return nullptr; const Sheet& s = sheets_[static_cast(sheet_idx)]; auto it = s.cells.find(CellKey{row, col}); if (it == s.cells.end()) return nullptr; return &it->second; } Value Workbook::get_scalar_value(int sheet_idx, int row, int col) const { const Cell* c = get_cell(sheet_idx, row, col); if (c && !c->raw_input.empty()) { if (c->value.is_array()) { const auto& arr = c->value.as_array(); if (arr && !arr->data.empty()) return arr->at(0, 0); return Value::make_empty(); } return c->value; } return dag_->get_spill_value(*this, sheet_idx, row, col); } std::string Workbook::get_formatted_value(int sheet_idx, int row, int col) const { const Cell* c = get_cell(sheet_idx, row, col); Value v = get_scalar_value(sheet_idx, row, col); std::string fmt = c ? c->number_format : std::string{}; return format_value(v, fmt); } bool Workbook::is_spilled_cell(int sheet_idx, int row, int col) const { return dag_->is_spilled_cell(sheet_idx, row, col); } std::optional Workbook::get_spill_range(int sheet_idx, int row, int col) const { return dag_->get_spill_range(sheet_idx, row, col); } ClipboardGrid Workbook::copy_range(int sheet_idx, CellRange range, bool is_cut) const { ClipboardGrid grid; grid.src_sheet = sheet_idx; grid.src_range = CellRange{ sheet_idx, std::min(range.r1, range.r2), std::min(range.c1, range.c2), std::max(range.r1, range.r2), std::max(range.c1, range.c2), }; grid.is_cut = is_cut; for (int r = grid.src_range.r1; r <= grid.src_range.r2; ++r) { for (int c = grid.src_range.c1; c <= grid.src_range.c2; ++c) { const Cell* cell = get_cell(sheet_idx, r, c); ClipboardCell cc; cc.dr = r - grid.src_range.r1; cc.dc = c - grid.src_range.c1; if (cell && !cell->raw_input.empty()) { cc.raw_input = cell->raw_input; cc.number_format = cell->number_format; } else { Value sv = get_scalar_value(sheet_idx, r, c); if (!sv.is_empty()) { auto s = to_string(sv); cc.raw_input = std::holds_alternative(s) ? std::get(s) : ""; } } cc.evaluated_value = get_scalar_value(sheet_idx, r, c); grid.cells.push_back(std::move(cc)); } } return grid; } void Workbook::paste_range(int sheet_idx, int dest_row, int dest_col, const ClipboardGrid& grid, bool translate_refs) { if (sheet_idx < 0 || sheet_idx >= sheet_count() || grid.cells.empty()) return; int drow = dest_row - grid.src_range.r1; int dcol = dest_col - grid.src_range.c1; std::vector edits; if (grid.is_cut && grid.src_sheet == sheet_idx) { for (int r = grid.src_range.r1; r <= grid.src_range.r2; ++r) { for (int c = grid.src_range.c1; c <= grid.src_range.c2; ++c) { edits.push_back(CellEdit{r, c, "", std::string{}}); } } } for (const auto& cc : grid.cells) { int tr = dest_row + cc.dr; int tc = dest_col + cc.dc; if (tr < 0 || tr >= kMaxRows || tc < 0 || tc >= kMaxCols) continue; std::string raw = cc.raw_input; if (translate_refs && !raw.empty() && raw[0] == '=' && !grid.is_cut) { raw = translate_formula_refs(raw, drow, dcol); } edits.push_back(CellEdit{tr, tc, std::move(raw), cc.number_format}); } apply_cell_edits(sheet_idx, edits); } void Workbook::autofill_range(int sheet_idx, CellRange src, CellRange dst) { if (sheet_idx < 0 || sheet_idx >= sheet_count()) return; CellRange s{sheet_idx, std::min(src.r1, src.r2), std::min(src.c1, src.c2), std::max(src.r1, src.r2), std::max(src.c1, src.c2)}; CellRange d{sheet_idx, std::min(dst.r1, dst.r2), std::min(dst.c1, dst.c2), std::max(dst.r1, dst.r2), std::max(dst.c1, dst.c2)}; bool vertical = (d.r1 < s.r1 || d.r2 > s.r2); std::vector edits; if (vertical) { int src_h = s.row_count(); for (int c = d.c1; c <= d.c2; ++c) { int sc = s.c1 + ((c - s.c1) % s.col_count() + s.col_count()) % s.col_count(); // Check if source column slice is pure numeric trend with >= 2 cells bool all_numeric = (src_h >= 2); std::vector nums; for (int r = s.r1; r <= s.r2 && all_numeric; ++r) { const Cell* cell = get_cell(sheet_idx, r, sc); if (!cell || (!cell->raw_input.empty() && cell->raw_input[0] == '=')) { all_numeric = false; break; } if (!cell || !cell->value.is_number()) { all_numeric = false; break; } nums.push_back(cell->value.as_number()); } double step = 0.0; if (all_numeric && nums.size() >= 2) { step = (nums.back() - nums.front()) / static_cast(nums.size() - 1); } for (int r = d.r1; r <= d.r2; ++r) { if (s.contains(sheet_idx, r, c)) continue; if (all_numeric) { double v = nums.front() + step * static_cast(r - s.r1); const Cell* template_c = get_cell(sheet_idx, s.r1, sc); auto str = to_string(Value::make_number(v)); std::string raw = std::holds_alternative(str) ? std::get(str) : ""; edits.push_back(CellEdit{r, c, std::move(raw), template_c ? template_c->number_format : std::string{}}); } else { int off = ((r - s.r1) % src_h + src_h) % src_h; int sr = s.r1 + off; const Cell* src_c = get_cell(sheet_idx, sr, sc); std::string raw = src_c ? src_c->raw_input : ""; std::string fmt = src_c ? src_c->number_format : ""; if (!raw.empty() && raw[0] == '=') { raw = translate_formula_refs(raw, r - sr, c - sc); } edits.push_back(CellEdit{r, c, std::move(raw), std::move(fmt)}); } } } } else { int src_w = s.col_count(); for (int r = d.r1; r <= d.r2; ++r) { int sr = s.r1 + ((r - s.r1) % s.row_count() + s.row_count()) % s.row_count(); bool all_numeric = (src_w >= 2); std::vector nums; for (int c = s.c1; c <= s.c2 && all_numeric; ++c) { const Cell* cell = get_cell(sheet_idx, sr, c); if (!cell || (!cell->raw_input.empty() && cell->raw_input[0] == '=') || !cell->value.is_number()) { all_numeric = false; break; } nums.push_back(cell->value.as_number()); } double step = 0.0; if (all_numeric && nums.size() >= 2) { step = (nums.back() - nums.front()) / static_cast(nums.size() - 1); } for (int c = d.c1; c <= d.c2; ++c) { if (s.contains(sheet_idx, r, c)) continue; if (all_numeric) { double v = nums.front() + step * static_cast(c - s.c1); const Cell* template_c = get_cell(sheet_idx, sr, s.c1); auto str = to_string(Value::make_number(v)); std::string raw = std::holds_alternative(str) ? std::get(str) : ""; edits.push_back(CellEdit{r, c, std::move(raw), template_c ? template_c->number_format : std::string{}}); } else { int off = ((c - s.c1) % src_w + src_w) % src_w; int sc = s.c1 + off; const Cell* src_c = get_cell(sheet_idx, sr, sc); std::string raw = src_c ? src_c->raw_input : ""; std::string fmt = src_c ? src_c->number_format : ""; if (!raw.empty() && raw[0] == '=') { raw = translate_formula_refs(raw, r - sr, c - sc); } edits.push_back(CellEdit{r, c, std::move(raw), std::move(fmt)}); } } } } apply_cell_edits(sheet_idx, edits); } void Workbook::replace_all_sheets(std::vector new_sheets, int active_sheet) { if (new_sheets.empty()) { Sheet s; s.name = "Sheet1"; new_sheets.push_back(std::move(s)); } sheets_ = std::move(new_sheets); for (auto& s : sheets_) { for (auto& [key, cell] : s.cells) { parse_cell_input(cell, cell.raw_input); } } active_sheet_ = std::clamp(active_sheet, 0, sheet_count() - 1); clear_history(); recalculate_all(); } void Workbook::replace_sheet_cells(int sheet_idx, std::unordered_map new_cells) { if (sheet_idx < 0 || sheet_idx >= sheet_count()) return; Sheet& s = sheets_[static_cast(sheet_idx)]; s.cells = std::move(new_cells); for (auto& [key, cell] : s.cells) { parse_cell_input(cell, cell.raw_input); } clear_history(); recalculate_all(); } std::optional Workbook::get_used_bounds(int sheet_idx) const { if (sheet_idx < 0 || sheet_idx >= sheet_count()) return std::nullopt; const Sheet& s = sheets_[static_cast(sheet_idx)]; bool any = false; int max_r = 0; int max_c = 0; for (const auto& [key, cell] : s.cells) { if (cell.raw_input.empty() && cell.value.is_empty()) continue; any = true; max_r = std::max(max_r, key.row); max_c = std::max(max_c, key.col); } for (const auto& [origin, sr] : dag_->spill_ranges()) { if (origin.sheet == sheet_idx) { any = true; max_r = std::max(max_r, sr.r2); max_c = std::max(max_c, sr.c2); } } if (!any) return std::nullopt; return CellRange{sheet_idx, 0, 0, max_r, max_c}; } void Workbook::recalculate_all() { dag_->rebuild(*this); } } // namespace htab