-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcell.cpp
More file actions
68 lines (59 loc) · 1.64 KB
/
cell.cpp
File metadata and controls
68 lines (59 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "cell.h"
Cell::Cell(std::string expression, const ISheet& sheet_) : raw_expression(expression), sheet(sheet_) {
if (raw_expression[0] == kFormulaSign) {
is_formula = true;
formula = ParseFormula(raw_expression.substr(1));
}
else {
is_formula = false;
if (raw_expression.size() > 0 && raw_expression[0] == kEscapeSign) {
value = raw_expression.substr(1);
}
else {
try {
value = std::stod(raw_expression);
}
catch (const std::invalid_argument& err) {
value = raw_expression;
}
}
}
}
void Cell::UpdateValueIfFormula() {
if (is_formula) {
auto formula_eval_res = formula.get()->Evaluate(sheet);
if (std::holds_alternative<double>(formula_eval_res)) {
value = std::get<double>(formula_eval_res);
}
else if (std::holds_alternative<FormulaError>(formula_eval_res)) {
value = std::get<FormulaError>(formula_eval_res);
}
raw_expression = "=" + formula.get()->GetExpression();
}
}
Cell::Value Cell::GetValue() const {
return value;
}
std::string Cell::GetText() const { return raw_expression; }
std::vector<Position> Cell::GetReferencedCells() const {
if (IsFormula()) {
return formula.get()->GetReferencedCells();
}
else {
return {};
}
}
bool Cell::IsFormula() const { return is_formula; }
IFormula* Cell::GetFormula() { return formula.get(); }
void Cell::SetValue(Value new_value) {
value = new_value;
}
void Cell::SetText(std::string new_text) {
raw_expression = new_text;
}
void Cell::AddDependentCell(const Position& pos) {
dependent.push_back(pos);
}
std::vector<Position>& Cell::GetDependentCells() {
return dependent;
}