Compare commits
17 Commits
feature/ni
...
feature/da
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4b0dbc598f | ||
|
|
8844b80c51 | ||
|
|
0f8f345aae | ||
|
|
3191e12b93 | ||
|
|
61cecb7af5 | ||
|
|
fdeae2c771 | ||
|
|
3e9c69f122 | ||
|
|
c1c43143cc | ||
|
|
763b43a590 | ||
|
|
8dbfe07392 | ||
|
|
e39bfe912a | ||
|
|
9bbf94e033 | ||
|
|
0194912845 | ||
|
|
1d4d84bb57 | ||
|
|
e841c17aca | ||
|
|
f2c43f2070 | ||
|
|
32b1f8ef4e |
7
Makefile
7
Makefile
@@ -31,12 +31,7 @@ clean: remove-artifacts
|
||||
rm -r -f base/target
|
||||
rm -r -f xlsx/target
|
||||
rm -r -f bindings/python/target
|
||||
rm -r -f bindings/wasm/target
|
||||
rm -r -f bindings/wasm/pkg
|
||||
rm -r -f webapp/IronCalc/node_modules
|
||||
rm -r -f webapp/IronCalc/dist
|
||||
rm -r -f webapp/app.ironcalc.com/frontend/node_modules
|
||||
rm -r -f webapp/app.ironcalc.com/frontend/dist
|
||||
rm -r -f bindings/wasm/targets
|
||||
rm -f cargo-test-*
|
||||
rm -f base/cargo-test-*
|
||||
rm -f xlsx/cargo-test-*
|
||||
|
||||
61
base/CALC.md
61
base/CALC.md
@@ -1,61 +0,0 @@
|
||||
# Evaluation Strategy
|
||||
|
||||
|
||||
We have a list of the spill cells:
|
||||
|
||||
```
|
||||
// Checks if the array starting at cell will cover cells whose values
|
||||
// has been requested
|
||||
def CheckSpill(cell, array):
|
||||
for c in cell+array:
|
||||
support CellHasBeenRequested(c):
|
||||
if support is not empty:
|
||||
return support
|
||||
return []
|
||||
|
||||
// Fills cells with the result (an array)
|
||||
def FillCells(cell, result):
|
||||
|
||||
|
||||
def EvaluateNodeInContext(node, context):
|
||||
match node:
|
||||
case OP(left, right, op):
|
||||
l = EvaluateNodeInContext(left, context)?
|
||||
r = EvaluateNodeInContext(left, context)?
|
||||
return op(l, r)
|
||||
case FUNCTION(args, fn):
|
||||
...
|
||||
case CELL(cell):
|
||||
EvaluateCell(cell)
|
||||
case RANGE(start, end):
|
||||
...
|
||||
|
||||
|
||||
|
||||
def EvaluateCell(cell):
|
||||
if IsCellEvaluating(cell):
|
||||
return CIRC
|
||||
MarkEvaluating(cell)
|
||||
result = EvaluateNodeInContext(cell.formula, cell)
|
||||
if isSpill(result):
|
||||
CheckSpill(cell, array)?
|
||||
FillCells(result)
|
||||
|
||||
|
||||
def EvaluateWorkbook():
|
||||
spill_cells = [cell_1, ...., cell_n];
|
||||
|
||||
|
||||
for cell in spill_cells:
|
||||
result = evaluate(cell)
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# When updating a cell value
|
||||
|
||||
If it was a spill cell we nee
|
||||
@@ -22,7 +22,7 @@ impl Model {
|
||||
.cell(row, column)
|
||||
.and_then(|c| c.get_formula())
|
||||
{
|
||||
let node = &self.parsed_formulas[sheet as usize][f as usize].0.clone();
|
||||
let node = &self.parsed_formulas[sheet as usize][f as usize].clone();
|
||||
let cell_reference = CellReferenceRC {
|
||||
sheet: self.workbook.worksheets[sheet as usize].get_name(),
|
||||
row,
|
||||
|
||||
@@ -77,6 +77,8 @@ impl Model {
|
||||
match to_f64(&node) {
|
||||
Ok(f2) => match op(f1, f2) {
|
||||
Ok(x) => data_row.push(ArrayNode::Number(x)),
|
||||
Err(Error::DIV) => data_row.push(ArrayNode::Error(Error::DIV)),
|
||||
Err(Error::VALUE) => data_row.push(ArrayNode::Error(Error::VALUE)),
|
||||
Err(e) => data_row.push(ArrayNode::Error(e)),
|
||||
},
|
||||
Err(err) => data_row.push(ArrayNode::Error(err)),
|
||||
@@ -98,6 +100,8 @@ impl Model {
|
||||
match to_f64(&node) {
|
||||
Ok(f1) => match op(f1, f2) {
|
||||
Ok(x) => data_row.push(ArrayNode::Number(x)),
|
||||
Err(Error::DIV) => data_row.push(ArrayNode::Error(Error::DIV)),
|
||||
Err(Error::VALUE) => data_row.push(ArrayNode::Error(Error::VALUE)),
|
||||
Err(e) => data_row.push(ArrayNode::Error(e)),
|
||||
},
|
||||
Err(err) => data_row.push(ArrayNode::Error(err)),
|
||||
@@ -133,6 +137,10 @@ impl Model {
|
||||
(Some(v1), Some(v2)) => match (to_f64(v1), to_f64(v2)) {
|
||||
(Ok(f1), Ok(f2)) => match op(f1, f2) {
|
||||
Ok(x) => data_row.push(ArrayNode::Number(x)),
|
||||
Err(Error::DIV) => data_row.push(ArrayNode::Error(Error::DIV)),
|
||||
Err(Error::VALUE) => {
|
||||
data_row.push(ArrayNode::Error(Error::VALUE))
|
||||
}
|
||||
Err(e) => data_row.push(ArrayNode::Error(e)),
|
||||
},
|
||||
(Err(e), _) | (_, Err(e)) => data_row.push(ArrayNode::Error(e)),
|
||||
|
||||
100
base/src/cell.rs
100
base/src/cell.rs
@@ -64,50 +64,12 @@ impl Cell {
|
||||
/// Returns the formula of a cell if any.
|
||||
pub fn get_formula(&self) -> Option<i32> {
|
||||
match self {
|
||||
Cell::CellFormula { f, .. }
|
||||
| Cell::CellFormulaBoolean { f, .. }
|
||||
| Cell::CellFormulaNumber { f, .. }
|
||||
| Cell::CellFormulaString { f, .. }
|
||||
| Cell::CellFormulaError { f, .. }
|
||||
| Cell::DynamicCellFormula { f, .. }
|
||||
| Cell::DynamicCellFormulaBoolean { f, .. }
|
||||
| Cell::DynamicCellFormulaNumber { f, .. }
|
||||
| Cell::DynamicCellFormulaString { f, .. }
|
||||
| Cell::DynamicCellFormulaError { f, .. } => Some(*f),
|
||||
Cell::EmptyCell { .. }
|
||||
| Cell::BooleanCell { .. }
|
||||
| Cell::NumberCell { .. }
|
||||
| Cell::ErrorCell { .. }
|
||||
| Cell::SharedString { .. }
|
||||
| Cell::SpillNumberCell { .. }
|
||||
| Cell::SpillBooleanCell { .. }
|
||||
| Cell::SpillErrorCell { .. }
|
||||
| Cell::SpillStringCell { .. } => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the dynamic range of a cell if any.
|
||||
pub fn get_dynamic_range(&self) -> Option<(i32, i32)> {
|
||||
match self {
|
||||
Cell::DynamicCellFormula { r, .. } => Some(*r),
|
||||
Cell::DynamicCellFormulaBoolean { r, .. } => Some(*r),
|
||||
Cell::DynamicCellFormulaNumber { r, .. } => Some(*r),
|
||||
Cell::DynamicCellFormulaString { r, .. } => Some(*r),
|
||||
Cell::DynamicCellFormulaError { r, .. } => Some(*r),
|
||||
Cell::EmptyCell { .. }
|
||||
| Cell::BooleanCell { .. }
|
||||
| Cell::NumberCell { .. }
|
||||
| Cell::ErrorCell { .. }
|
||||
| Cell::SharedString { .. }
|
||||
| Cell::CellFormula { .. }
|
||||
| Cell::CellFormulaBoolean { .. }
|
||||
| Cell::CellFormulaNumber { .. }
|
||||
| Cell::CellFormulaString { .. }
|
||||
| Cell::CellFormulaError { .. }
|
||||
| Cell::SpillNumberCell { .. }
|
||||
| Cell::SpillBooleanCell { .. }
|
||||
| Cell::SpillErrorCell { .. }
|
||||
| Cell::SpillStringCell { .. } => None,
|
||||
Cell::CellFormula { f, .. } => Some(*f),
|
||||
Cell::CellFormulaBoolean { f, .. } => Some(*f),
|
||||
Cell::CellFormulaNumber { f, .. } => Some(*f),
|
||||
Cell::CellFormulaString { f, .. } => Some(*f),
|
||||
Cell::CellFormulaError { f, .. } => Some(*f),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,15 +89,6 @@ impl Cell {
|
||||
Cell::CellFormulaNumber { s, .. } => *s = style,
|
||||
Cell::CellFormulaString { s, .. } => *s = style,
|
||||
Cell::CellFormulaError { s, .. } => *s = style,
|
||||
Cell::SpillBooleanCell { s, .. } => *s = style,
|
||||
Cell::SpillNumberCell { s, .. } => *s = style,
|
||||
Cell::SpillStringCell { s, .. } => *s = style,
|
||||
Cell::SpillErrorCell { s, .. } => *s = style,
|
||||
Cell::DynamicCellFormula { s, .. } => *s = style,
|
||||
Cell::DynamicCellFormulaBoolean { s, .. } => *s = style,
|
||||
Cell::DynamicCellFormulaNumber { s, .. } => *s = style,
|
||||
Cell::DynamicCellFormulaString { s, .. } => *s = style,
|
||||
Cell::DynamicCellFormulaError { s, .. } => *s = style,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -151,15 +104,6 @@ impl Cell {
|
||||
Cell::CellFormulaNumber { s, .. } => *s,
|
||||
Cell::CellFormulaString { s, .. } => *s,
|
||||
Cell::CellFormulaError { s, .. } => *s,
|
||||
Cell::SpillBooleanCell { s, .. } => *s,
|
||||
Cell::SpillNumberCell { s, .. } => *s,
|
||||
Cell::SpillStringCell { s, .. } => *s,
|
||||
Cell::SpillErrorCell { s, .. } => *s,
|
||||
Cell::DynamicCellFormula { s, .. } => *s,
|
||||
Cell::DynamicCellFormulaBoolean { s, .. } => *s,
|
||||
Cell::DynamicCellFormulaNumber { s, .. } => *s,
|
||||
Cell::DynamicCellFormulaString { s, .. } => *s,
|
||||
Cell::DynamicCellFormulaError { s, .. } => *s,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -175,15 +119,6 @@ impl Cell {
|
||||
Cell::CellFormulaNumber { .. } => CellType::Number,
|
||||
Cell::CellFormulaString { .. } => CellType::Text,
|
||||
Cell::CellFormulaError { .. } => CellType::ErrorValue,
|
||||
Cell::SpillBooleanCell { .. } => CellType::LogicalValue,
|
||||
Cell::SpillNumberCell { .. } => CellType::Number,
|
||||
Cell::SpillStringCell { .. } => CellType::Text,
|
||||
Cell::SpillErrorCell { .. } => CellType::ErrorValue,
|
||||
Cell::DynamicCellFormula { .. } => CellType::Number,
|
||||
Cell::DynamicCellFormulaBoolean { .. } => CellType::LogicalValue,
|
||||
Cell::DynamicCellFormulaNumber { .. } => CellType::Number,
|
||||
Cell::DynamicCellFormulaString { .. } => CellType::Text,
|
||||
Cell::DynamicCellFormulaError { .. } => CellType::ErrorValue,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -201,7 +136,7 @@ impl Cell {
|
||||
Cell::EmptyCell { .. } => CellValue::None,
|
||||
Cell::BooleanCell { v, s: _ } => CellValue::Boolean(*v),
|
||||
Cell::NumberCell { v, s: _ } => CellValue::Number(*v),
|
||||
Cell::ErrorCell { ei, .. } | Cell::SpillErrorCell { ei, .. } => {
|
||||
Cell::ErrorCell { ei, .. } => {
|
||||
let v = ei.to_localized_error_string(language);
|
||||
CellValue::String(v)
|
||||
}
|
||||
@@ -213,25 +148,14 @@ impl Cell {
|
||||
};
|
||||
CellValue::String(v)
|
||||
}
|
||||
Cell::DynamicCellFormula { .. } | Cell::CellFormula { .. } => {
|
||||
CellValue::String("#ERROR!".to_string())
|
||||
}
|
||||
Cell::DynamicCellFormulaBoolean { v, .. } | Cell::CellFormulaBoolean { v, .. } => {
|
||||
CellValue::Boolean(*v)
|
||||
}
|
||||
Cell::DynamicCellFormulaNumber { v, .. } | Cell::CellFormulaNumber { v, .. } => {
|
||||
CellValue::Number(*v)
|
||||
}
|
||||
Cell::DynamicCellFormulaString { v, .. } | Cell::CellFormulaString { v, .. } => {
|
||||
CellValue::String(v.clone())
|
||||
}
|
||||
Cell::DynamicCellFormulaError { ei, .. } | Cell::CellFormulaError { ei, .. } => {
|
||||
Cell::CellFormula { .. } => CellValue::String("#ERROR!".to_string()),
|
||||
Cell::CellFormulaBoolean { v, .. } => CellValue::Boolean(*v),
|
||||
Cell::CellFormulaNumber { v, .. } => CellValue::Number(*v),
|
||||
Cell::CellFormulaString { v, .. } => CellValue::String(v.clone()),
|
||||
Cell::CellFormulaError { ei, .. } => {
|
||||
let v = ei.to_localized_error_string(language);
|
||||
CellValue::String(v)
|
||||
}
|
||||
Cell::SpillBooleanCell { v, .. } => CellValue::Boolean(*v),
|
||||
Cell::SpillNumberCell { v, .. } => CellValue::Number(*v),
|
||||
Cell::SpillStringCell { v, .. } => CellValue::String(v.clone()),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -717,7 +717,7 @@ impl Parser {
|
||||
return Node::ParseErrorKind {
|
||||
formula: self.lexer.get_formula(),
|
||||
position: 0,
|
||||
message: "sheet not found".to_string(),
|
||||
message: format!("sheet not found: {}", context.sheet),
|
||||
};
|
||||
}
|
||||
};
|
||||
@@ -850,7 +850,7 @@ impl Parser {
|
||||
return Node::ParseErrorKind {
|
||||
formula: self.lexer.get_formula(),
|
||||
position: 0,
|
||||
message: "sheet not found".to_string(),
|
||||
message: format!("sheet not found: {}", context.sheet),
|
||||
};
|
||||
}
|
||||
};
|
||||
@@ -878,7 +878,7 @@ impl Parser {
|
||||
return Node::ParseErrorKind {
|
||||
formula: self.lexer.get_formula(),
|
||||
position: 0,
|
||||
message: "sheet not found".to_string(),
|
||||
message: format!("table sheet not found: {}", table.sheet_name),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
@@ -186,8 +186,7 @@ pub fn add_implicit_intersection(node: &mut Node, add: bool) {
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum StaticResult {
|
||||
pub(crate) enum StaticResult {
|
||||
Scalar,
|
||||
Array(i32, i32),
|
||||
Range(i32, i32),
|
||||
@@ -223,7 +222,7 @@ fn static_analysis_op_nodes(left: &Node, right: &Node) -> StaticResult {
|
||||
// * Array(a, b) if we know it will be an a x b array.
|
||||
// * Range(a, b) if we know it will be a a x b range.
|
||||
// * Unknown if we cannot guaranty either
|
||||
pub(crate) fn run_static_analysis_on_node(node: &Node) -> StaticResult {
|
||||
fn run_static_analysis_on_node(node: &Node) -> StaticResult {
|
||||
match node {
|
||||
Node::BooleanKind(_)
|
||||
| Node::NumberKind(_)
|
||||
|
||||
@@ -246,7 +246,7 @@ impl Model {
|
||||
}
|
||||
// None of the cases matched so we return the default
|
||||
// If there is an even number of args is the last one otherwise is #N/A
|
||||
if args_count % 2 == 0 {
|
||||
if args_count.is_multiple_of(2) {
|
||||
return self.evaluate_node_in_context(&args[args_count - 1], cell);
|
||||
}
|
||||
CalcResult::Error {
|
||||
@@ -262,7 +262,7 @@ impl Model {
|
||||
if args_count < 2 {
|
||||
return CalcResult::new_args_number_error(cell);
|
||||
}
|
||||
if args_count % 2 != 0 {
|
||||
if !args_count.is_multiple_of(2) {
|
||||
// Missing value for last condition
|
||||
return CalcResult::new_args_number_error(cell);
|
||||
}
|
||||
|
||||
@@ -350,7 +350,7 @@ impl Model {
|
||||
// FIXME: This function shares a lot of code with apply_ifs. Can we merge them?
|
||||
pub(crate) fn fn_countifs(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
|
||||
let args_count = args.len();
|
||||
if args_count < 2 || args_count % 2 == 1 {
|
||||
if args_count < 2 || !args_count.is_multiple_of(2) {
|
||||
return CalcResult::new_args_number_error(cell);
|
||||
}
|
||||
|
||||
@@ -476,7 +476,7 @@ impl Model {
|
||||
F: FnMut(f64),
|
||||
{
|
||||
let args_count = args.len();
|
||||
if args_count < 3 || args_count % 2 == 0 {
|
||||
if args_count < 3 || args_count.is_multiple_of(2) {
|
||||
return Err(CalcResult::new_args_number_error(cell));
|
||||
}
|
||||
let arg_0 = self.evaluate_node_in_context(&args[0], cell);
|
||||
|
||||
@@ -96,7 +96,7 @@ impl Model {
|
||||
|
||||
match cell.get_formula() {
|
||||
Some(f) => {
|
||||
let node = &self.parsed_formulas[sheet_index as usize][f as usize].0;
|
||||
let node = &self.parsed_formulas[sheet_index as usize][f as usize];
|
||||
matches!(
|
||||
node,
|
||||
Node::FunctionKind {
|
||||
|
||||
@@ -11,9 +11,8 @@ use crate::{
|
||||
lexer::LexerMode,
|
||||
parser::{
|
||||
move_formula::{move_formula, MoveContext},
|
||||
static_analysis::{run_static_analysis_on_node, StaticResult},
|
||||
stringify::{rename_defined_name_in_node, to_rc_format, to_string},
|
||||
ArrayNode, Node, Parser,
|
||||
Node, Parser,
|
||||
},
|
||||
token::{get_error_by_name, Error, OpCompare, OpProduct, OpSum, OpUnary},
|
||||
types::*,
|
||||
@@ -84,24 +83,6 @@ pub(crate) enum ParsedDefinedName {
|
||||
InvalidDefinedNameFormula,
|
||||
}
|
||||
|
||||
/// A support node is either a cell or a range of cells
|
||||
pub(crate) enum SupportNode {
|
||||
/// (sheet, row, column)
|
||||
Cell((u32, i32, i32)),
|
||||
/// (sheet, row, column, height, width)
|
||||
Range((u32, i32, i32, u32, u32))
|
||||
}
|
||||
|
||||
/// The state of the computation
|
||||
pub(crate) enum EvaluationState {
|
||||
/// the model is ready for a new evaluation
|
||||
Ready,
|
||||
/// the model is evaluating cells that might spill
|
||||
EvaluatingSpills,
|
||||
/// the model is evaluating cells normally
|
||||
Evaluating
|
||||
}
|
||||
|
||||
/// A dynamical IronCalc model.
|
||||
///
|
||||
/// Its is composed of a `Workbook`. Everything else are dynamical quantities:
|
||||
@@ -118,13 +99,15 @@ pub struct Model {
|
||||
/// A Rust internal representation of an Excel workbook
|
||||
pub workbook: Workbook,
|
||||
/// A list of parsed formulas
|
||||
pub parsed_formulas: Vec<Vec<(Node, StaticResult)>>,
|
||||
pub parsed_formulas: Vec<Vec<Node>>,
|
||||
/// A list of parsed defined names
|
||||
pub(crate) parsed_defined_names: HashMap<(Option<u32>, String), ParsedDefinedName>,
|
||||
/// An optimization to lookup strings faster
|
||||
pub(crate) shared_strings: HashMap<String, usize>,
|
||||
/// An instance of the parser
|
||||
pub(crate) parser: Parser,
|
||||
/// The list of cells with formulas that are evaluated or being evaluated
|
||||
pub(crate) cells: HashMap<(u32, i32, i32), CellState>,
|
||||
/// The locale of the model
|
||||
pub(crate) locale: Locale,
|
||||
/// The language used
|
||||
@@ -133,16 +116,6 @@ pub struct Model {
|
||||
pub(crate) tz: Tz,
|
||||
/// The view id. A view consists of a selected sheet and ranges.
|
||||
pub(crate) view_id: u32,
|
||||
/// ** Runtime ***
|
||||
/// The list of cells with formulas that are evaluated or being evaluated
|
||||
pub(crate) cells: HashMap<(u32, i32, i32), CellState>,
|
||||
/// The support graph. For a given cell (sheet, row, column) the list of cells and ranges that were requested
|
||||
pub(crate) support_graph: HashMap<(u32, i32, i32), Vec<SupportNode>>,
|
||||
/// If the model is in a switch state then spill cells in the indices should be switched and recalculation redone
|
||||
pub(crate) switch_cells: Option<(i32, i32)>,
|
||||
/// Stack of cells being evaluated
|
||||
pub(crate) stack: Vec<(u32, i32, i32)>,
|
||||
pub(crate) state: EvaluationState,
|
||||
}
|
||||
|
||||
// FIXME: Maybe this should be the same as CellReference
|
||||
@@ -549,203 +522,14 @@ impl Model {
|
||||
}
|
||||
Ok(format!("{}!{}{}", sheet.name, column, cell_reference.row))
|
||||
}
|
||||
|
||||
/// Sets sheet, target_row, target_column, (width, height), &v
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn set_spill_cell_with_formula_value(
|
||||
&mut self,
|
||||
sheet: u32,
|
||||
row: i32,
|
||||
column: i32,
|
||||
r: (i32, i32),
|
||||
v: &CalcResult,
|
||||
s: i32,
|
||||
f: i32,
|
||||
) -> Result<(), String> {
|
||||
let new_cell = match v {
|
||||
CalcResult::EmptyCell => Cell::DynamicCellFormulaNumber {
|
||||
f,
|
||||
v: 0.0,
|
||||
s,
|
||||
r,
|
||||
a: false,
|
||||
},
|
||||
CalcResult::String(v) => Cell::DynamicCellFormulaString {
|
||||
f,
|
||||
v: v.clone(),
|
||||
s,
|
||||
r,
|
||||
a: false,
|
||||
},
|
||||
CalcResult::Number(v) => Cell::DynamicCellFormulaNumber {
|
||||
v: *v,
|
||||
s,
|
||||
r,
|
||||
f,
|
||||
a: false,
|
||||
},
|
||||
CalcResult::Boolean(b) => Cell::DynamicCellFormulaBoolean {
|
||||
v: *b,
|
||||
s,
|
||||
r,
|
||||
f,
|
||||
a: false,
|
||||
},
|
||||
CalcResult::Error { error, .. } => Cell::DynamicCellFormulaError {
|
||||
ei: error.clone(),
|
||||
s,
|
||||
r,
|
||||
f,
|
||||
a: false,
|
||||
o: "".to_string(),
|
||||
m: "".to_string(),
|
||||
},
|
||||
|
||||
// These cannot happen
|
||||
// FIXME: Maybe the type of get_cell_value should be different
|
||||
CalcResult::Range { .. } | CalcResult::EmptyArg | CalcResult::Array(_) => {
|
||||
Cell::DynamicCellFormulaError {
|
||||
ei: Error::ERROR,
|
||||
s,
|
||||
r,
|
||||
f,
|
||||
a: false,
|
||||
o: "".to_string(),
|
||||
m: "".to_string(),
|
||||
}
|
||||
}
|
||||
};
|
||||
let sheet_data = &mut self.workbook.worksheet_mut(sheet)?.sheet_data;
|
||||
|
||||
match sheet_data.get_mut(&row) {
|
||||
Some(column_data) => match column_data.get(&column) {
|
||||
Some(_cell) => {
|
||||
column_data.insert(column, new_cell);
|
||||
}
|
||||
None => {
|
||||
column_data.insert(column, new_cell);
|
||||
}
|
||||
},
|
||||
None => {
|
||||
let mut column_data = HashMap::new();
|
||||
column_data.insert(column, new_cell);
|
||||
sheet_data.insert(row, column_data);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Sets a cell with a "spill" value
|
||||
fn set_spill_cell_with_value(
|
||||
&mut self,
|
||||
sheet: u32,
|
||||
row: i32,
|
||||
column: i32,
|
||||
m: (i32, i32),
|
||||
v: &CalcResult,
|
||||
) -> Result<(), String> {
|
||||
let style_index = self.get_cell_style_index(sheet, row, column)?;
|
||||
let new_style_index = if self.workbook.styles.style_is_quote_prefix(style_index) {
|
||||
self.workbook
|
||||
.styles
|
||||
.get_style_without_quote_prefix(style_index)?
|
||||
} else {
|
||||
style_index
|
||||
};
|
||||
let new_cell = match v {
|
||||
CalcResult::EmptyCell => Cell::SpillNumberCell {
|
||||
v: 0.0,
|
||||
s: style_index,
|
||||
m,
|
||||
},
|
||||
CalcResult::String(s) => Cell::SpillStringCell {
|
||||
v: s.clone(),
|
||||
s: new_style_index,
|
||||
m,
|
||||
},
|
||||
CalcResult::Number(f) => Cell::SpillNumberCell {
|
||||
v: *f,
|
||||
s: new_style_index,
|
||||
m,
|
||||
},
|
||||
CalcResult::Boolean(b) => Cell::SpillBooleanCell {
|
||||
v: *b,
|
||||
s: new_style_index,
|
||||
m,
|
||||
},
|
||||
CalcResult::Error { error, .. } => Cell::SpillErrorCell {
|
||||
ei: error.clone(),
|
||||
s: style_index,
|
||||
m,
|
||||
},
|
||||
|
||||
// These cannot happen
|
||||
// FIXME: Maybe the type of get_cell_value should be different
|
||||
CalcResult::Range { .. } | CalcResult::EmptyArg | CalcResult::Array(_) => {
|
||||
Cell::SpillErrorCell {
|
||||
ei: Error::ERROR,
|
||||
s: style_index,
|
||||
m,
|
||||
}
|
||||
}
|
||||
};
|
||||
let sheet_data = &mut self.workbook.worksheet_mut(sheet)?.sheet_data;
|
||||
|
||||
match sheet_data.get_mut(&row) {
|
||||
Some(column_data) => match column_data.get(&column) {
|
||||
Some(_cell) => {
|
||||
column_data.insert(column, new_cell);
|
||||
}
|
||||
None => {
|
||||
column_data.insert(column, new_cell);
|
||||
}
|
||||
},
|
||||
None => {
|
||||
let mut column_data = HashMap::new();
|
||||
column_data.insert(column, new_cell);
|
||||
sheet_data.insert(row, column_data);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns `None` if no cell has called this cell, otherwise returns the dependent cell
|
||||
fn get_support_cell(&self, sheet: u32, row: i32, column: i32) -> Result<Option<&Cell>, String> {
|
||||
self.workbook.supporting_cells.get(&(sheet, row, column)).map(|c| Some(c)).ok_or_else(|| "Cell not found".into())
|
||||
}
|
||||
|
||||
/// Sets `result` in the cell given by `sheet` sheet index, row and column
|
||||
/// Note that will panic if the cell does not exist
|
||||
/// It will do nothing if the cell does not have a formula
|
||||
/// If the cell is an array or a range it will check if it is possible to spill to other cells
|
||||
/// if it is not it will return an error.
|
||||
/// Then it will check if any of the cells has been requested before.
|
||||
#[allow(clippy::expect_used)]
|
||||
fn set_cell_value(
|
||||
&mut self,
|
||||
cell_reference: CellReferenceIndex,
|
||||
result: &CalcResult,
|
||||
) -> Result<(), String> {
|
||||
fn set_cell_value(&mut self, cell_reference: CellReferenceIndex, result: &CalcResult) {
|
||||
let CellReferenceIndex { sheet, column, row } = cell_reference;
|
||||
let cell = self
|
||||
.workbook
|
||||
.worksheet(sheet)?
|
||||
.cell(row, column)
|
||||
.cloned()
|
||||
.unwrap_or_default();
|
||||
let cell = &self.workbook.worksheets[sheet as usize].sheet_data[&row][&column];
|
||||
let s = cell.get_style();
|
||||
// If the cell is a dynamic cell we need to delete all the cells in the range
|
||||
if let Some((width, height)) = cell.get_dynamic_range() {
|
||||
for r in row..row + height {
|
||||
for c in column..column + width {
|
||||
// skip the "mother" cell
|
||||
if r == row && c == column {
|
||||
continue;
|
||||
}
|
||||
self.cell_clear_contents(sheet, r, c)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(f) = cell.get_formula() {
|
||||
match result {
|
||||
CalcResult::Number(value) => {
|
||||
@@ -810,145 +594,19 @@ impl Model {
|
||||
ei: error.clone(),
|
||||
};
|
||||
}
|
||||
CalcResult::EmptyCell | CalcResult::EmptyArg => {
|
||||
*self.workbook.worksheets[sheet as usize]
|
||||
.sheet_data
|
||||
.get_mut(&row)
|
||||
.expect("expected a row")
|
||||
.get_mut(&column)
|
||||
.expect("expected a column") = Cell::CellFormulaNumber { f, s, v: 0.0 };
|
||||
}
|
||||
CalcResult::Range { left, right } => {
|
||||
if left.sheet == right.sheet
|
||||
&& left.row == right.row
|
||||
&& left.column == right.column
|
||||
{
|
||||
// There is only one cell
|
||||
let single_cell = CellReferenceIndex {
|
||||
let intersection_cell = CellReferenceIndex {
|
||||
sheet: left.sheet,
|
||||
column: left.column,
|
||||
row: left.row,
|
||||
};
|
||||
let v = self.evaluate_cell(single_cell);
|
||||
self.set_cell_value(cell_reference, &v)?;
|
||||
let v = self.evaluate_cell(intersection_cell);
|
||||
self.set_cell_value(cell_reference, &v);
|
||||
} else {
|
||||
// We need to check if all the cells are empty, otherwise we mark the cell as #SPILL!
|
||||
let mut all_empty = true;
|
||||
for r in row..=row + right.row - left.row {
|
||||
for c in column..=column + right.column - left.column {
|
||||
if r == row && c == column {
|
||||
// skip the "mother" cell
|
||||
continue;
|
||||
}
|
||||
if !self.is_empty_cell(sheet, r, c).unwrap_or(false) {
|
||||
all_empty = false;
|
||||
break;
|
||||
}
|
||||
if let Some(support) = self.get_support_cell(sheet, r, c) {
|
||||
all_empty = false;
|
||||
}
|
||||
}
|
||||
if !all_empty {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if !all_empty {
|
||||
let o = match self.cell_reference_to_string(&cell_reference) {
|
||||
Ok(s) => s,
|
||||
Err(_) => "".to_string(),
|
||||
};
|
||||
*self.workbook.worksheets[sheet as usize]
|
||||
.sheet_data
|
||||
.get_mut(&row)
|
||||
.expect("expected a row")
|
||||
.get_mut(&column)
|
||||
.expect("expected a column") = Cell::DynamicCellFormulaError {
|
||||
f,
|
||||
s,
|
||||
o,
|
||||
m: "Result would spill to non empty cells".to_string(),
|
||||
ei: Error::SPILL,
|
||||
r: (1, 1),
|
||||
a: false,
|
||||
};
|
||||
return Ok(());
|
||||
}
|
||||
// evaluate all the cells in that range
|
||||
for r in left.row..=right.row {
|
||||
for c in left.column..=right.column {
|
||||
let cell_reference = CellReferenceIndex {
|
||||
sheet: left.sheet,
|
||||
row: r,
|
||||
column: c,
|
||||
};
|
||||
// FIXME: We ned to return an error
|
||||
self.evaluate_cell(cell_reference);
|
||||
}
|
||||
}
|
||||
// now write the result in the target
|
||||
for r in left.row..=right.row {
|
||||
let row_delta = r - left.row;
|
||||
for c in left.column..=right.column {
|
||||
let column_delta = c - left.column;
|
||||
// We need to put whatever is in (left.sheet, r, c) in
|
||||
// (sheet, row + row_delta, column + column_delta)
|
||||
// But we need to preserve the style
|
||||
let target_row = row + row_delta;
|
||||
let target_column = column + column_delta;
|
||||
let cell = self
|
||||
.workbook
|
||||
.worksheet(left.sheet)?
|
||||
.cell(r, c)
|
||||
.cloned()
|
||||
.unwrap_or_default();
|
||||
let cell_reference = CellReferenceIndex {
|
||||
sheet: left.sheet,
|
||||
row: r,
|
||||
column: c,
|
||||
};
|
||||
let v = self.get_cell_value(&cell, cell_reference);
|
||||
if row == target_row && column == target_column {
|
||||
// let cell_reference = CellReferenceIndex { sheet, row, column };
|
||||
// self.set_cell_value(cell_reference, &v);
|
||||
self.set_spill_cell_with_formula_value(
|
||||
sheet,
|
||||
target_row,
|
||||
target_column,
|
||||
(right.column - left.column + 1, right.row - left.row + 1),
|
||||
&v,
|
||||
s,
|
||||
f,
|
||||
)?;
|
||||
continue;
|
||||
}
|
||||
self.set_spill_cell_with_value(
|
||||
sheet,
|
||||
target_row,
|
||||
target_column,
|
||||
(row, column),
|
||||
&v,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
CalcResult::Array(array) => {
|
||||
let width = array[0].len() as i32;
|
||||
let height = array.len() as i32;
|
||||
// First we check that we don't spill:
|
||||
let mut all_empty = true;
|
||||
for r in row..row + height {
|
||||
for c in column..column + width {
|
||||
if r == row && c == column {
|
||||
continue;
|
||||
}
|
||||
if !self.is_empty_cell(sheet, r, c).unwrap_or(false) {
|
||||
all_empty = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if !all_empty {
|
||||
let o = match self.cell_reference_to_string(&cell_reference) {
|
||||
Ok(s) => s,
|
||||
Err(_) => "".to_string(),
|
||||
@@ -962,65 +620,57 @@ impl Model {
|
||||
f,
|
||||
s,
|
||||
o,
|
||||
m: "Result would spill to non empty cells".to_string(),
|
||||
ei: Error::SPILL,
|
||||
m: "Implicit Intersection not implemented".to_string(),
|
||||
ei: Error::NIMPL,
|
||||
};
|
||||
return Ok(());
|
||||
}
|
||||
let mut target_row = row;
|
||||
for data_row in array {
|
||||
let mut target_column = column;
|
||||
for value in data_row {
|
||||
if row == target_row && column == target_column {
|
||||
// This is the root cell of the dynamic array
|
||||
let cell_reference = CellReferenceIndex { sheet, row, column };
|
||||
let v = match value {
|
||||
ArrayNode::Boolean(b) => CalcResult::Boolean(*b),
|
||||
ArrayNode::Number(f) => CalcResult::Number(*f),
|
||||
ArrayNode::String(s) => CalcResult::String(s.clone()),
|
||||
ArrayNode::Error(error) => CalcResult::new_error(
|
||||
error.clone(),
|
||||
cell_reference,
|
||||
error.to_localized_error_string(&self.language),
|
||||
),
|
||||
};
|
||||
self.set_spill_cell_with_formula_value(
|
||||
sheet,
|
||||
target_row,
|
||||
target_column,
|
||||
(width, height),
|
||||
&v,
|
||||
s,
|
||||
f,
|
||||
)?;
|
||||
target_column += 1;
|
||||
continue;
|
||||
}
|
||||
let v = match value {
|
||||
ArrayNode::Boolean(b) => CalcResult::Boolean(*b),
|
||||
ArrayNode::Number(f) => CalcResult::Number(*f),
|
||||
ArrayNode::String(s) => CalcResult::String(s.clone()),
|
||||
ArrayNode::Error(error) => CalcResult::new_error(
|
||||
error.clone(),
|
||||
cell_reference,
|
||||
error.to_localized_error_string(&self.language),
|
||||
),
|
||||
};
|
||||
self.set_spill_cell_with_value(
|
||||
sheet,
|
||||
target_row,
|
||||
target_column,
|
||||
(row, column),
|
||||
&v,
|
||||
)?;
|
||||
target_column += 1;
|
||||
}
|
||||
target_row += 1;
|
||||
}
|
||||
// if let Some(intersection_cell) = implicit_intersection(&cell_reference, &range)
|
||||
// {
|
||||
// let v = self.evaluate_cell(intersection_cell);
|
||||
// self.set_cell_value(cell_reference, &v);
|
||||
// } else {
|
||||
// let o = match self.cell_reference_to_string(&cell_reference) {
|
||||
// Ok(s) => s,
|
||||
// Err(_) => "".to_string(),
|
||||
// };
|
||||
// *self.workbook.worksheets[sheet as usize]
|
||||
// .sheet_data
|
||||
// .get_mut(&row)
|
||||
// .expect("expected a row")
|
||||
// .get_mut(&column)
|
||||
// .expect("expected a column") = Cell::CellFormulaError {
|
||||
// f,
|
||||
// s,
|
||||
// o,
|
||||
// m: "Invalid reference".to_string(),
|
||||
// ei: Error::VALUE,
|
||||
// };
|
||||
// }
|
||||
}
|
||||
CalcResult::EmptyCell | CalcResult::EmptyArg => {
|
||||
*self.workbook.worksheets[sheet as usize]
|
||||
.sheet_data
|
||||
.get_mut(&row)
|
||||
.expect("expected a row")
|
||||
.get_mut(&column)
|
||||
.expect("expected a column") = Cell::CellFormulaNumber { f, s, v: 0.0 };
|
||||
}
|
||||
CalcResult::Array(_) => {
|
||||
*self.workbook.worksheets[sheet as usize]
|
||||
.sheet_data
|
||||
.get_mut(&row)
|
||||
.expect("expected a row")
|
||||
.get_mut(&column)
|
||||
.expect("expected a column") = Cell::CellFormulaError {
|
||||
f,
|
||||
s,
|
||||
o: "".to_string(),
|
||||
m: "Arrays not supported yet".to_string(),
|
||||
ei: Error::NIMPL,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Sets the color of the sheet tab.
|
||||
@@ -1064,18 +714,16 @@ impl Model {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// EmptyCell, Boolean, Number, String, Error
|
||||
fn get_cell_value(&self, cell: &Cell, cell_reference: CellReferenceIndex) -> CalcResult {
|
||||
use Cell::*;
|
||||
match cell {
|
||||
EmptyCell { .. } => CalcResult::EmptyCell,
|
||||
BooleanCell { v, .. } | SpillBooleanCell { v, .. } => CalcResult::Boolean(*v),
|
||||
NumberCell { v, .. } | SpillNumberCell { v, .. } => CalcResult::Number(*v),
|
||||
ErrorCell { ei, .. } | SpillErrorCell { ei, .. } => {
|
||||
BooleanCell { v, .. } => CalcResult::Boolean(*v),
|
||||
NumberCell { v, .. } => CalcResult::Number(*v),
|
||||
ErrorCell { ei, .. } => {
|
||||
let message = ei.to_localized_error_string(&self.language);
|
||||
CalcResult::new_error(ei.clone(), cell_reference, message)
|
||||
}
|
||||
SpillStringCell { v, .. } => CalcResult::String(v.clone()),
|
||||
SharedString { si, .. } => {
|
||||
if let Some(s) = self.workbook.shared_strings.get(*si as usize) {
|
||||
CalcResult::String(s.clone())
|
||||
@@ -1084,21 +732,15 @@ impl Model {
|
||||
CalcResult::new_error(Error::ERROR, cell_reference, message)
|
||||
}
|
||||
}
|
||||
DynamicCellFormula { .. } | CellFormula { .. } => CalcResult::Error {
|
||||
CellFormula { .. } => CalcResult::Error {
|
||||
error: Error::ERROR,
|
||||
origin: cell_reference,
|
||||
message: "Unevaluated formula".to_string(),
|
||||
},
|
||||
DynamicCellFormulaBoolean { v, .. } | CellFormulaBoolean { v, .. } => {
|
||||
CalcResult::Boolean(*v)
|
||||
}
|
||||
DynamicCellFormulaNumber { v, .. } | CellFormulaNumber { v, .. } => {
|
||||
CalcResult::Number(*v)
|
||||
}
|
||||
DynamicCellFormulaString { v, .. } | CellFormulaString { v, .. } => {
|
||||
CalcResult::String(v.clone())
|
||||
}
|
||||
DynamicCellFormulaError { ei, o, m, .. } | CellFormulaError { ei, o, m, .. } => {
|
||||
CellFormulaBoolean { v, .. } => CalcResult::Boolean(*v),
|
||||
CellFormulaNumber { v, .. } => CalcResult::Number(*v),
|
||||
CellFormulaString { v, .. } => CalcResult::String(v.clone()),
|
||||
CellFormulaError { ei, o, m, .. } => {
|
||||
if let Some(cell_reference) = self.parse_reference(o) {
|
||||
CalcResult::new_error(ei.clone(), cell_reference, m.clone())
|
||||
} else {
|
||||
@@ -1130,8 +772,6 @@ impl Model {
|
||||
self.workbook.worksheet(sheet)?.is_empty_cell(row, column)
|
||||
}
|
||||
|
||||
/// Evaluates the cell. After the evaluation is done puts the value in the cell and other cells if it spills.
|
||||
/// If when writing a spill cell encounter a cell whose value has been requested marks the model as "dirty"
|
||||
pub(crate) fn evaluate_cell(&mut self, cell_reference: CellReferenceIndex) -> CalcResult {
|
||||
let row_data = match self.workbook.worksheets[cell_reference.sheet as usize]
|
||||
.sheet_data
|
||||
@@ -1170,10 +810,9 @@ impl Model {
|
||||
self.cells.insert(key, CellState::Evaluating);
|
||||
}
|
||||
}
|
||||
let (node, _static_result) =
|
||||
&self.parsed_formulas[cell_reference.sheet as usize][f as usize];
|
||||
let result = self.evaluate_node_in_context(&node.clone(), cell_reference);
|
||||
let _ = self.set_cell_value(cell_reference, &result);
|
||||
let node = &self.parsed_formulas[cell_reference.sheet as usize][f as usize].clone();
|
||||
let result = self.evaluate_node_in_context(node, cell_reference);
|
||||
self.set_cell_value(cell_reference, &result);
|
||||
// mark cell as evaluated
|
||||
self.cells.insert(key, CellState::Evaluated);
|
||||
result
|
||||
@@ -1283,10 +922,6 @@ impl Model {
|
||||
locale,
|
||||
tz,
|
||||
view_id: 0,
|
||||
support_graph: HashMap::new(),
|
||||
switch_cells: None,
|
||||
stack: Vec::new(),
|
||||
state: EvaluationState::Ready,
|
||||
};
|
||||
|
||||
model.parse_formulas();
|
||||
@@ -1465,8 +1100,7 @@ impl Model {
|
||||
Some(cell) => match cell.get_formula() {
|
||||
None => cell.get_text(&self.workbook.shared_strings, &self.language),
|
||||
Some(i) => {
|
||||
let (formula, static_result) =
|
||||
&self.parsed_formulas[sheet as usize][i as usize];
|
||||
let formula = &self.parsed_formulas[sheet as usize][i as usize];
|
||||
let cell_ref = CellReferenceRC {
|
||||
sheet: self.workbook.worksheets[sheet as usize].get_name(),
|
||||
row: target_row,
|
||||
@@ -1569,8 +1203,7 @@ impl Model {
|
||||
.get(sheet as usize)
|
||||
.ok_or("missing sheet")?
|
||||
.get(formula_index as usize)
|
||||
.ok_or("missing formula")?
|
||||
.0;
|
||||
.ok_or("missing formula")?;
|
||||
let cell_ref = CellReferenceRC {
|
||||
sheet: worksheet.get_name(),
|
||||
row,
|
||||
@@ -1804,25 +1437,6 @@ impl Model {
|
||||
column: i32,
|
||||
value: String,
|
||||
) -> Result<(), String> {
|
||||
// We need to check if the cell is part of a dynamic array
|
||||
let cell = self
|
||||
.workbook
|
||||
.worksheet(sheet)?
|
||||
.cell(row, column)
|
||||
.cloned()
|
||||
.unwrap_or_default();
|
||||
// If the cell is a dynamic cell we need to delete all the cells in the range
|
||||
if let Some((width, height)) = cell.get_dynamic_range() {
|
||||
for r in row..row + height {
|
||||
for c in column..column + width {
|
||||
// skip the "mother" cell
|
||||
if r == row && c == column {
|
||||
continue;
|
||||
}
|
||||
self.cell_clear_contents(sheet, r, c)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
// If value starts with "'" then we force the style to be quote_prefix
|
||||
let style_index = self.get_cell_style_index(sheet, row, column)?;
|
||||
if let Some(new_value) = value.strip_prefix('\'') {
|
||||
@@ -1848,9 +1462,8 @@ impl Model {
|
||||
self.set_cell_with_formula(sheet, row, column, formula, new_style_index)?;
|
||||
// Update the style if needed
|
||||
let cell = CellReferenceIndex { sheet, row, column };
|
||||
let (parsed_formula, static_result) =
|
||||
self.parsed_formulas[sheet as usize][formula_index as usize].clone();
|
||||
if let Some(units) = self.compute_node_units(&parsed_formula, &cell) {
|
||||
let parsed_formula = &self.parsed_formulas[sheet as usize][formula_index as usize];
|
||||
if let Some(units) = self.compute_node_units(parsed_formula, &cell) {
|
||||
let new_style_index = self
|
||||
.workbook
|
||||
.styles
|
||||
@@ -1858,14 +1471,6 @@ impl Model {
|
||||
let style = self.workbook.styles.get_style(new_style_index)?;
|
||||
self.set_cell_style(sheet, row, column, &style)?
|
||||
}
|
||||
match static_result {
|
||||
StaticResult::Scalar => {}
|
||||
StaticResult::Array(_, _)
|
||||
| StaticResult::Range(_, _)
|
||||
| StaticResult::Unknown => {
|
||||
self.workbook.spill_cells.push((sheet, row, column));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// The list of currencies is '$', '€' and the local currency
|
||||
let mut currencies = vec!["$", "€"];
|
||||
@@ -1939,7 +1544,6 @@ impl Model {
|
||||
_ => parsed_formula = new_parsed_formula,
|
||||
}
|
||||
}
|
||||
let static_result = run_static_analysis_on_node(&parsed_formula);
|
||||
|
||||
let s = to_rc_format(&parsed_formula);
|
||||
let mut formula_index: i32 = -1;
|
||||
@@ -1948,7 +1552,7 @@ impl Model {
|
||||
}
|
||||
if formula_index == -1 {
|
||||
shared_formulas.push(s);
|
||||
self.parsed_formulas[sheet as usize].push((parsed_formula, static_result));
|
||||
self.parsed_formulas[sheet as usize].push(parsed_formula);
|
||||
formula_index = (shared_formulas.len() as i32) - 1;
|
||||
}
|
||||
worksheet.set_cell_with_formula(row, column, formula_index, style)?;
|
||||
@@ -2143,7 +1747,7 @@ impl Model {
|
||||
};
|
||||
match cell.get_formula() {
|
||||
Some(formula_index) => {
|
||||
let formula = &self.parsed_formulas[sheet as usize][formula_index as usize].0;
|
||||
let formula = &self.parsed_formulas[sheet as usize][formula_index as usize];
|
||||
let cell_ref = CellReferenceRC {
|
||||
sheet: worksheet.get_name(),
|
||||
row,
|
||||
@@ -2179,34 +1783,9 @@ impl Model {
|
||||
|
||||
/// Evaluates the model with a top-down recursive algorithm
|
||||
pub fn evaluate(&mut self) {
|
||||
// We first evaluate all the cells that might spill to other cells
|
||||
let mut spills_computed = false;
|
||||
self.state = EvaluationState::EvaluatingSpills;
|
||||
while !spills_computed {
|
||||
spills_computed = true;
|
||||
// clear all computation artifacts
|
||||
self.cells.clear();
|
||||
// Evaluate all the cells that might spill
|
||||
let spill_cells = self.workbook.spill_cells.clone();
|
||||
for (sheet, row, column) in spill_cells {
|
||||
self.evaluate_cell(CellReferenceIndex { sheet, row, column });
|
||||
if self.switch_cells.is_some() {
|
||||
spills_computed = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if let Some((index1, index2)) = self.switch_cells {
|
||||
spills_computed = false;
|
||||
// switch the cells indices in the spill_cells
|
||||
let cell1 = self.workbook.spill_cells[index1 as usize];
|
||||
let cell2 = self.workbook.spill_cells[index2 as usize];
|
||||
self.workbook.spill_cells[index1 as usize] = cell2;
|
||||
self.workbook.spill_cells[index2 as usize] = cell1;
|
||||
}
|
||||
}
|
||||
self.state = EvaluationState::Evaluating;
|
||||
// clear all computation artifacts
|
||||
self.cells.clear();
|
||||
|
||||
// Now we compute all the rest
|
||||
let cells = self.get_all_cells();
|
||||
|
||||
for cell in cells {
|
||||
@@ -2216,7 +1795,6 @@ impl Model {
|
||||
column: cell.column,
|
||||
});
|
||||
}
|
||||
self.state = EvaluationState::Ready;
|
||||
}
|
||||
|
||||
/// Removes the content of the cell but leaves the style.
|
||||
@@ -2240,22 +1818,9 @@ impl Model {
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn cell_clear_contents(&mut self, sheet: u32, row: i32, column: i32) -> Result<(), String> {
|
||||
// If it has a spill formula we need to delete the contents of all the spilled cells
|
||||
let worksheet = self.workbook.worksheet_mut(sheet)?;
|
||||
if let Some(cell) = worksheet.cell(row, column) {
|
||||
if let Some((width, height)) = cell.get_dynamic_range() {
|
||||
for r in row..row + height {
|
||||
for c in column..column + width {
|
||||
if row == r && column == c {
|
||||
// we skip the root cell
|
||||
continue;
|
||||
}
|
||||
worksheet.cell_clear_contents(r, c)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
worksheet.cell_clear_contents(row, column)?;
|
||||
self.workbook
|
||||
.worksheet_mut(sheet)?
|
||||
.cell_clear_contents(row, column)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -2280,18 +1845,6 @@ impl Model {
|
||||
/// # }
|
||||
pub fn cell_clear_all(&mut self, sheet: u32, row: i32, column: i32) -> Result<(), String> {
|
||||
let worksheet = self.workbook.worksheet_mut(sheet)?;
|
||||
// Delete the contents of spilled cells if any
|
||||
if let Some(cell) = worksheet.cell(row, column) {
|
||||
if let Some((width, height)) = cell.get_dynamic_range() {
|
||||
for r in row..row + height {
|
||||
for c in column..column + width {
|
||||
if row == r && c == column {
|
||||
worksheet.cell_clear_contents(r, c)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let sheet_data = &mut worksheet.sheet_data;
|
||||
if let Some(row_data) = sheet_data.get_mut(&row) {
|
||||
|
||||
@@ -8,7 +8,6 @@ use crate::{
|
||||
expressions::{
|
||||
lexer::LexerMode,
|
||||
parser::{
|
||||
static_analysis::run_static_analysis_on_node,
|
||||
stringify::{rename_sheet_in_node, to_rc_format, to_string},
|
||||
Parser,
|
||||
},
|
||||
@@ -16,7 +15,7 @@ use crate::{
|
||||
},
|
||||
language::get_language,
|
||||
locale::get_locale,
|
||||
model::{get_milliseconds_since_epoch, EvaluationState, Model, ParsedDefinedName},
|
||||
model::{get_milliseconds_since_epoch, Model, ParsedDefinedName},
|
||||
types::{
|
||||
DefinedName, Metadata, SheetState, Workbook, WorkbookSettings, WorkbookView, Worksheet,
|
||||
WorksheetView,
|
||||
@@ -95,8 +94,7 @@ impl Model {
|
||||
let mut parse_formula = Vec::new();
|
||||
for formula in shared_formulas {
|
||||
let t = self.parser.parse(formula, &cell_reference);
|
||||
let static_result = run_static_analysis_on_node(&t);
|
||||
parse_formula.push((t, static_result));
|
||||
parse_formula.push(t);
|
||||
}
|
||||
self.parsed_formulas.push(parse_formula);
|
||||
}
|
||||
@@ -407,7 +405,6 @@ impl Model {
|
||||
},
|
||||
tables: HashMap::new(),
|
||||
views,
|
||||
spill_cells: Vec::new(),
|
||||
};
|
||||
let parsed_formulas = Vec::new();
|
||||
let worksheets = &workbook.worksheets;
|
||||
@@ -430,10 +427,6 @@ impl Model {
|
||||
language,
|
||||
tz,
|
||||
view_id: 0,
|
||||
support_graph: HashMap::new(),
|
||||
switch_cells: None,
|
||||
stack: Vec::new(),
|
||||
state: EvaluationState::Ready,
|
||||
};
|
||||
model.parse_formulas();
|
||||
Ok(model)
|
||||
|
||||
@@ -52,7 +52,6 @@ mod test_fn_offset;
|
||||
mod test_number_format;
|
||||
|
||||
mod test_arrays;
|
||||
mod test_dynamic_arrays;
|
||||
mod test_escape_quotes;
|
||||
mod test_extend;
|
||||
mod test_fn_fv;
|
||||
|
||||
@@ -1,50 +0,0 @@
|
||||
#![allow(clippy::unwrap_used)]
|
||||
|
||||
use crate::test::util::new_empty_model;
|
||||
|
||||
#[test]
|
||||
fn they_spill() {
|
||||
let mut model = new_empty_model();
|
||||
model._set("A1", "42");
|
||||
model._set("A2", "5");
|
||||
model._set("A3", "7");
|
||||
|
||||
model._set("B1", "=A1:A3");
|
||||
|
||||
model.evaluate();
|
||||
|
||||
assert_eq!(model._get_text("B1"), *"42");
|
||||
assert_eq!(model._get_text("B2"), *"5");
|
||||
assert_eq!(model._get_text("B3"), *"7");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spill_error() {
|
||||
let mut model = new_empty_model();
|
||||
model._set("A1", "42");
|
||||
model._set("A2", "5");
|
||||
model._set("A3", "7");
|
||||
|
||||
model._set("B1", "=A1:A3");
|
||||
model._set("B2", "4");
|
||||
|
||||
model.evaluate();
|
||||
|
||||
assert_eq!(model._get_text("B1"), *"#SPILL!");
|
||||
assert_eq!(model._get_text("B2"), *"4");
|
||||
assert_eq!(model._get_text("B3"), *"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn second_evaluation() {
|
||||
let mut model = new_empty_model();
|
||||
model._set("C3", "={1,2,3}");
|
||||
model.evaluate();
|
||||
|
||||
assert_eq!(model._get_text("D3"), "2");
|
||||
|
||||
model._set("D8", "23");
|
||||
model.evaluate();
|
||||
|
||||
assert_eq!(model._get_text("D3"), "2");
|
||||
}
|
||||
@@ -91,12 +91,12 @@ fn fn_or_xor() {
|
||||
model._set("A10", &format!("={func}(X99:Z99"));
|
||||
|
||||
// Reference to cell with reference to empty range
|
||||
model._set("B11", "=@X99:Z99");
|
||||
model._set("B11", "=X99:Z99");
|
||||
model._set("A11", &format!("={func}(B11)"));
|
||||
|
||||
// Reference to cell with non-empty range
|
||||
model._set("X12", "1");
|
||||
model._set("B12", "=@X12:Z12");
|
||||
model._set("B12", "=X12:Z12");
|
||||
model._set("A12", &format!("={func}(B12)"));
|
||||
|
||||
// Reference to text cell
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
use crate::test::util::new_empty_model;
|
||||
|
||||
#[test]
|
||||
fn simple_column() {
|
||||
fn simple_colum() {
|
||||
let mut model = new_empty_model();
|
||||
// We populate cells A1 to A3
|
||||
model._set("A1", "1");
|
||||
@@ -30,7 +30,7 @@ fn return_of_array_is_n_impl() {
|
||||
|
||||
model.evaluate();
|
||||
|
||||
assert_eq!(model._get_text("C2"), "1".to_string());
|
||||
assert_eq!(model._get_text("C2"), "#N/IMPL!".to_string());
|
||||
assert_eq!(model._get_text("D2"), "1.89188842".to_string());
|
||||
}
|
||||
|
||||
|
||||
@@ -6,10 +6,8 @@ mod test_border;
|
||||
mod test_clear_cells;
|
||||
mod test_column_style;
|
||||
mod test_defined_names;
|
||||
mod test_delete_evaluates;
|
||||
mod test_delete_row_column_formatting;
|
||||
mod test_diff_queue;
|
||||
mod test_dynamic_array;
|
||||
mod test_evaluation;
|
||||
mod test_general;
|
||||
mod test_grid_lines;
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
#![allow(clippy::unwrap_used)]
|
||||
|
||||
use crate::{expressions::types::Area, UserModel};
|
||||
|
||||
#[test]
|
||||
fn clear_cell_contents_evaluates() {
|
||||
let mut model = UserModel::new_empty("model", "en", "UTC").unwrap();
|
||||
model.set_user_input(0, 1, 1, "42").unwrap();
|
||||
model.set_user_input(0, 1, 2, "=A1").unwrap();
|
||||
assert_eq!(
|
||||
model.get_formatted_cell_value(0, 1, 2),
|
||||
Ok("42".to_string())
|
||||
);
|
||||
model
|
||||
.range_clear_contents(&Area {
|
||||
sheet: 0,
|
||||
row: 1,
|
||||
column: 1,
|
||||
width: 1,
|
||||
height: 1,
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(model.get_formatted_cell_value(0, 1, 2), Ok("0".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clear_cell_all_evaluates() {
|
||||
let mut model = UserModel::new_empty("model", "en", "UTC").unwrap();
|
||||
model.set_user_input(0, 1, 1, "42").unwrap();
|
||||
model.set_user_input(0, 1, 2, "=A1").unwrap();
|
||||
assert_eq!(
|
||||
model.get_formatted_cell_value(0, 1, 2),
|
||||
Ok("42".to_string())
|
||||
);
|
||||
model
|
||||
.range_clear_all(&Area {
|
||||
sheet: 0,
|
||||
row: 1,
|
||||
column: 1,
|
||||
width: 1,
|
||||
height: 1,
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(model.get_formatted_cell_value(0, 1, 2), Ok("0".to_string()));
|
||||
}
|
||||
@@ -1,130 +0,0 @@
|
||||
#![allow(clippy::unwrap_used)]
|
||||
|
||||
use crate::{expressions::types::Area, UserModel};
|
||||
|
||||
// Tests basic behavour.
|
||||
#[test]
|
||||
fn basic() {
|
||||
let mut model = UserModel::new_empty("model", "en", "UTC").unwrap();
|
||||
// We put a value by the dynamic array to check the border conditions
|
||||
model.set_user_input(0, 2, 1, "22").unwrap();
|
||||
model.set_user_input(0, 1, 1, "={34,35,3}").unwrap();
|
||||
|
||||
assert_eq!(
|
||||
model.get_formatted_cell_value(0, 1, 1),
|
||||
Ok("34".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
// Test that overwriting a dynamic array with a single value dissolves the array
|
||||
#[test]
|
||||
fn sett_user_input_mother() {
|
||||
let mut model = UserModel::new_empty("model", "en", "UTC").unwrap();
|
||||
model.set_user_input(0, 1, 1, "={34,35,3}").unwrap();
|
||||
assert_eq!(
|
||||
model.get_formatted_cell_value(0, 1, 2),
|
||||
Ok("35".to_string())
|
||||
);
|
||||
model.set_user_input(0, 1, 1, "123").unwrap();
|
||||
assert_eq!(model.get_formatted_cell_value(0, 1, 2), Ok("".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_user_input_sibling() {
|
||||
let mut model = UserModel::new_empty("model", "en", "UTC").unwrap();
|
||||
model.set_user_input(0, 1, 1, "={43,55,34}").unwrap();
|
||||
assert_eq!(
|
||||
model.get_formatted_cell_value(0, 1, 2),
|
||||
Ok("55".to_string())
|
||||
);
|
||||
// This does nothing
|
||||
model.set_user_input(0, 1, 2, "123").unwrap();
|
||||
assert_eq!(
|
||||
model.get_formatted_cell_value(0, 1, 2),
|
||||
Ok("55".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn basic_undo_redo() {
|
||||
let mut model = UserModel::new_empty("model", "en", "UTC").unwrap();
|
||||
model.set_user_input(0, 1, 1, "={34,35,3}").unwrap();
|
||||
assert_eq!(
|
||||
model.get_formatted_cell_value(0, 1, 2),
|
||||
Ok("35".to_string())
|
||||
);
|
||||
model.undo().unwrap();
|
||||
assert_eq!(model.get_formatted_cell_value(0, 1, 2), Ok("".to_string()));
|
||||
model.redo().unwrap();
|
||||
assert_eq!(
|
||||
model.get_formatted_cell_value(0, 1, 2),
|
||||
Ok("35".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mixed_spills() {
|
||||
let mut model = UserModel::new_empty("model", "en", "UTC").unwrap();
|
||||
// D9 => ={1,2,3}
|
||||
model.set_user_input(0, 9, 4, "={34,35,3}").unwrap();
|
||||
// F6 => ={1;2;3;4}
|
||||
model.set_user_input(0, 6, 6, "={1;2;3;4}").unwrap();
|
||||
|
||||
// F6 should be #SPILL!
|
||||
assert_eq!(
|
||||
model.get_formatted_cell_value(0, 6, 6),
|
||||
Ok("#SPILL!".to_string())
|
||||
);
|
||||
|
||||
// We delete D9
|
||||
model
|
||||
.range_clear_contents(&Area {
|
||||
sheet: 0,
|
||||
row: 9,
|
||||
column: 4,
|
||||
width: 1,
|
||||
height: 1,
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
// F6 should be 1
|
||||
assert_eq!(model.get_formatted_cell_value(0, 6, 6), Ok("1".to_string()));
|
||||
|
||||
// Now we undo that
|
||||
model.undo().unwrap();
|
||||
// F6 should be #SPILL!
|
||||
assert_eq!(
|
||||
model.get_formatted_cell_value(0, 6, 6),
|
||||
Ok("#SPILL!".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spill_order_d9_f6() {
|
||||
let mut model = UserModel::new_empty("model", "en", "UTC").unwrap();
|
||||
// D9 => ={1,2,3}
|
||||
model.set_user_input(0, 9, 4, "={34,35,3}").unwrap();
|
||||
// F6 => ={1;2;3;4}
|
||||
model.set_user_input(0, 6, 6, "={1;2;3;4}").unwrap();
|
||||
|
||||
// F6 should be #SPILL!
|
||||
assert_eq!(
|
||||
model.get_formatted_cell_value(0, 6, 6),
|
||||
Ok("#SPILL!".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spill_order_f6_d9() {
|
||||
let mut model = UserModel::new_empty("model", "en", "UTC").unwrap();
|
||||
// F6 => ={1;2;3;4}
|
||||
model.set_user_input(0, 6, 6, "={1;2;3;4}").unwrap();
|
||||
// D9 => ={1,2,3}
|
||||
model.set_user_input(0, 9, 4, "={34,35,3}").unwrap();
|
||||
|
||||
// D9 should be #SPILL!
|
||||
assert_eq!(
|
||||
model.get_formatted_cell_value(0, 9, 4),
|
||||
Ok("#SPILL!".to_string())
|
||||
);
|
||||
}
|
||||
@@ -51,8 +51,6 @@ pub struct Workbook {
|
||||
pub metadata: Metadata,
|
||||
pub tables: HashMap<String, Table>,
|
||||
pub views: HashMap<u32, WorkbookView>,
|
||||
/// The list of cells that spill in the order of evaluation
|
||||
pub spill_cells: Vec<(u32, i32, i32)>,
|
||||
}
|
||||
|
||||
/// A defined name. The `sheet_id` is the sheet index in case the name is local
|
||||
@@ -161,17 +159,17 @@ pub enum CellType {
|
||||
CompoundData = 128,
|
||||
}
|
||||
|
||||
/// Cell types
|
||||
/// s is always the style index of the cell
|
||||
#[derive(Encode, Decode, Debug, Clone, PartialEq)]
|
||||
pub enum Cell {
|
||||
EmptyCell {
|
||||
s: i32,
|
||||
},
|
||||
|
||||
BooleanCell {
|
||||
v: bool,
|
||||
s: i32,
|
||||
},
|
||||
|
||||
NumberCell {
|
||||
v: f64,
|
||||
s: i32,
|
||||
@@ -183,7 +181,6 @@ pub enum Cell {
|
||||
},
|
||||
// Always a shared string
|
||||
SharedString {
|
||||
// string index
|
||||
si: i32,
|
||||
s: i32,
|
||||
},
|
||||
@@ -192,11 +189,13 @@ pub enum Cell {
|
||||
f: i32,
|
||||
s: i32,
|
||||
},
|
||||
|
||||
CellFormulaBoolean {
|
||||
f: i32,
|
||||
v: bool,
|
||||
s: i32,
|
||||
},
|
||||
|
||||
CellFormulaNumber {
|
||||
f: i32,
|
||||
v: f64,
|
||||
@@ -208,9 +207,9 @@ pub enum Cell {
|
||||
v: String,
|
||||
s: i32,
|
||||
},
|
||||
|
||||
CellFormulaError {
|
||||
f: i32,
|
||||
// error index
|
||||
ei: Error,
|
||||
s: i32,
|
||||
// Origin: Sheet3!C4
|
||||
@@ -218,81 +217,7 @@ pub enum Cell {
|
||||
// Error Message: "Not implemented function"
|
||||
m: String,
|
||||
},
|
||||
// All Spill/dynamic cells have a boolean, a for array, if true it is an array formula
|
||||
// Spill cells point to a mother cell (row, column)
|
||||
SpillNumberCell {
|
||||
v: f64,
|
||||
s: i32,
|
||||
// mother cell (row, column)
|
||||
m: (i32, i32),
|
||||
},
|
||||
SpillBooleanCell {
|
||||
v: bool,
|
||||
s: i32,
|
||||
// mother cell (row, column)
|
||||
m: (i32, i32),
|
||||
},
|
||||
SpillErrorCell {
|
||||
ei: Error,
|
||||
s: i32,
|
||||
// mother cell (row, column)
|
||||
m: (i32, i32),
|
||||
},
|
||||
SpillStringCell {
|
||||
v: String,
|
||||
s: i32,
|
||||
// mother cell (row, column)
|
||||
m: (i32, i32),
|
||||
},
|
||||
// Dynamic cell formulas have a range (width, height)
|
||||
DynamicCellFormula {
|
||||
f: i32,
|
||||
s: i32,
|
||||
// range of the formula (width, height)
|
||||
r: (i32, i32),
|
||||
// true if the formula is a CSE formula
|
||||
a: bool,
|
||||
},
|
||||
DynamicCellFormulaBoolean {
|
||||
f: i32,
|
||||
v: bool,
|
||||
s: i32,
|
||||
// range of the formula (width, height)
|
||||
r: (i32, i32),
|
||||
// true if the formula is a CSE formula
|
||||
a: bool,
|
||||
},
|
||||
DynamicCellFormulaNumber {
|
||||
f: i32,
|
||||
v: f64,
|
||||
s: i32,
|
||||
// range of the formula (width, height)
|
||||
r: (i32, i32),
|
||||
// true if the formula is a CSE formula
|
||||
a: bool,
|
||||
},
|
||||
DynamicCellFormulaString {
|
||||
f: i32,
|
||||
v: String,
|
||||
s: i32,
|
||||
// range of the formula (width, height)
|
||||
r: (i32, i32),
|
||||
// true if the formula is a CSE formula
|
||||
a: bool,
|
||||
},
|
||||
DynamicCellFormulaError {
|
||||
f: i32,
|
||||
ei: Error,
|
||||
s: i32,
|
||||
// Cell origin of the error
|
||||
o: String,
|
||||
// Error message in text
|
||||
m: String,
|
||||
// range of the formula (width, height)
|
||||
r: (i32, i32),
|
||||
// true if the formula is a CSE formula
|
||||
a: bool,
|
||||
},
|
||||
// TODO: Array formulas
|
||||
}
|
||||
|
||||
impl Default for Cell {
|
||||
|
||||
@@ -24,18 +24,6 @@ use crate::user_model::history::{
|
||||
};
|
||||
|
||||
use super::border_utils::is_max_border;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub enum CellArrayStructure {
|
||||
// It s just a single cell
|
||||
SingleCell,
|
||||
// It is part o a dynamic array
|
||||
// (mother_row, mother_column, width, height)
|
||||
DynamicChild(i32, i32, i32, i32),
|
||||
// Mother of a dynamic array (width, height)
|
||||
DynamicMother(i32, i32),
|
||||
}
|
||||
|
||||
/// Data for the clipboard
|
||||
pub type ClipboardData = HashMap<i32, HashMap<i32, ClipboardCell>>;
|
||||
|
||||
@@ -639,7 +627,6 @@ impl UserModel {
|
||||
}
|
||||
}
|
||||
self.push_diff_list(diff_list);
|
||||
self.evaluate_if_not_paused();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -669,7 +656,6 @@ impl UserModel {
|
||||
}
|
||||
}
|
||||
self.push_diff_list(diff_list);
|
||||
self.evaluate_if_not_paused();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1755,65 +1741,6 @@ impl UserModel {
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
/// Returns the geometric structure of a cell
|
||||
pub fn get_cell_array_structure(
|
||||
&self,
|
||||
sheet: u32,
|
||||
row: i32,
|
||||
column: i32,
|
||||
) -> Result<CellArrayStructure, String> {
|
||||
let cell = self
|
||||
.model
|
||||
.workbook
|
||||
.worksheet(sheet)?
|
||||
.cell(row, column)
|
||||
.cloned()
|
||||
.unwrap_or_default();
|
||||
match cell {
|
||||
Cell::EmptyCell { .. }
|
||||
| Cell::BooleanCell { .. }
|
||||
| Cell::NumberCell { .. }
|
||||
| Cell::ErrorCell { .. }
|
||||
| Cell::SharedString { .. }
|
||||
| Cell::CellFormula { .. }
|
||||
| Cell::CellFormulaBoolean { .. }
|
||||
| Cell::CellFormulaNumber { .. }
|
||||
| Cell::CellFormulaString { .. }
|
||||
| Cell::CellFormulaError { .. } => Ok(CellArrayStructure::SingleCell),
|
||||
Cell::SpillNumberCell { m, .. }
|
||||
| Cell::SpillBooleanCell { m, .. }
|
||||
| Cell::SpillErrorCell { m, .. }
|
||||
| Cell::SpillStringCell { m, .. } => {
|
||||
let (m_row, m_column) = m;
|
||||
let m_cell = self
|
||||
.model
|
||||
.workbook
|
||||
.worksheet(sheet)?
|
||||
.cell(m_row, m_column)
|
||||
.cloned()
|
||||
.unwrap_or_default();
|
||||
let (width, height) = match m_cell {
|
||||
Cell::DynamicCellFormula { r, .. }
|
||||
| Cell::DynamicCellFormulaBoolean { r, .. }
|
||||
| Cell::DynamicCellFormulaNumber { r, .. }
|
||||
| Cell::DynamicCellFormulaString { r, .. }
|
||||
| Cell::DynamicCellFormulaError { r, .. } => (r.0, r.1),
|
||||
_ => return Err("Invalid structure".to_string()),
|
||||
};
|
||||
Ok(CellArrayStructure::DynamicChild(
|
||||
m_row, m_column, width, height,
|
||||
))
|
||||
}
|
||||
Cell::DynamicCellFormula { r, .. }
|
||||
| Cell::DynamicCellFormulaBoolean { r, .. }
|
||||
| Cell::DynamicCellFormulaNumber { r, .. }
|
||||
| Cell::DynamicCellFormulaString { r, .. }
|
||||
| Cell::DynamicCellFormulaError { r, .. } => {
|
||||
Ok(CellArrayStructure::DynamicMother(r.0, r.1))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a copy of the selected area
|
||||
pub fn copy_to_clipboard(&self) -> Result<Clipboard, String> {
|
||||
let selected_area = self.get_selected_view();
|
||||
@@ -2116,24 +2043,6 @@ impl UserModel {
|
||||
old_value,
|
||||
} => {
|
||||
needs_evaluation = true;
|
||||
let cell = self
|
||||
.model
|
||||
.workbook
|
||||
.worksheet(*sheet)?
|
||||
.cell(*row, *column)
|
||||
.cloned()
|
||||
.unwrap_or_default();
|
||||
if let Some((width, height)) = cell.get_dynamic_range() {
|
||||
for r in *row..*row + height {
|
||||
for c in *column..*column + width {
|
||||
// skip the "mother" cell
|
||||
if r == *row && c == *column {
|
||||
continue;
|
||||
}
|
||||
self.model.cell_clear_contents(*sheet, r, c)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
match *old_value.clone() {
|
||||
Some(value) => {
|
||||
self.model
|
||||
|
||||
@@ -5,21 +5,18 @@ use bitcode::{Decode, Encode};
|
||||
use crate::types::{Cell, Col, Row, SheetState, Style, Worksheet};
|
||||
|
||||
#[derive(Clone, Encode, Decode)]
|
||||
#[cfg_attr(debug_assertions, derive(Debug))]
|
||||
pub(crate) struct RowData {
|
||||
pub(crate) row: Option<Row>,
|
||||
pub(crate) data: HashMap<i32, Cell>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Encode, Decode)]
|
||||
#[cfg_attr(debug_assertions, derive(Debug))]
|
||||
pub(crate) struct ColumnData {
|
||||
pub(crate) column: Option<Col>,
|
||||
pub(crate) data: HashMap<i32, Cell>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Encode, Decode)]
|
||||
#[cfg_attr(debug_assertions, derive(Debug))]
|
||||
pub(crate) enum Diff {
|
||||
// Cell diffs
|
||||
SetCellValue {
|
||||
|
||||
@@ -4,15 +4,20 @@
|
||||
Example usage:
|
||||
|
||||
```javascript
|
||||
import { Model } from '@ironcalc/wasm';
|
||||
import { Model } from '@ironcalc/nodejs';
|
||||
|
||||
const model = new Model("Workbook1", "en", "UTC");
|
||||
|
||||
model.setUserInput(0, 1, 1, "=1+1");
|
||||
const result1 = model.getFormattedCellValue(0, 1, 1);
|
||||
|
||||
console.log('Cell value', result1);
|
||||
const result1 = model.getFormattedCellValue(0, 1, 1);
|
||||
console.log('Cell value', result1); // "#ERROR"
|
||||
|
||||
model.evaluate();
|
||||
|
||||
const resultAfterEvaluate = model.getFormattedCellValue(0, 1, 1);
|
||||
console.log('Cell value', resultAfterEvaluate); // 2
|
||||
|
||||
let result2 = model.getCellStyle(0, 1, 1);
|
||||
console.log('Cell style', result2);
|
||||
```
|
||||
```
|
||||
|
||||
@@ -766,21 +766,4 @@ impl Model {
|
||||
.get_first_non_empty_in_row_after_column(sheet, row, column)
|
||||
.map_err(to_js_error)
|
||||
}
|
||||
|
||||
#[wasm_bindgen(
|
||||
js_name = "getCellArrayStructure",
|
||||
unchecked_return_type = "CellArrayStructure"
|
||||
)]
|
||||
pub fn get_cell_array_structure(
|
||||
&self,
|
||||
sheet: u32,
|
||||
row: i32,
|
||||
column: i32,
|
||||
) -> Result<JsValue, JsError> {
|
||||
let cell_structure = self
|
||||
.model
|
||||
.get_cell_array_structure(sheet, row, column)
|
||||
.map_err(|e| to_js_error(e.to_string()))?;
|
||||
serde_wasm_bindgen::to_value(&cell_structure).map_err(JsError::from)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,11 +109,6 @@ export interface MarkedToken {
|
||||
end: number;
|
||||
}
|
||||
|
||||
export type CellArrayStructure =
|
||||
| "SingleCell"
|
||||
| { DynamicChild: [number, number, number, number] }
|
||||
| { DynamicMother: [number, number] };
|
||||
|
||||
export interface WorksheetProperties {
|
||||
name: string;
|
||||
color: string;
|
||||
@@ -221,7 +216,7 @@ export interface SelectedView {
|
||||
// };
|
||||
|
||||
// type ClipboardData = Record<string, Record <string, ClipboardCell>>;
|
||||
type ClipboardData = Map<number, Map<number, ClipboardCell>>;
|
||||
type ClipboardData = Map<number, Map <number, ClipboardCell>>;
|
||||
|
||||
export interface ClipboardCell {
|
||||
text: string;
|
||||
@@ -238,4 +233,4 @@ export interface DefinedName {
|
||||
name: string;
|
||||
scope?: number;
|
||||
formula: string;
|
||||
}
|
||||
}
|
||||
@@ -66,4 +66,8 @@ Using IronCalc, a complex number is a string of the form "1+j3".
|
||||
|
||||
"#N/A" => #N/A
|
||||
|
||||
## Arrays
|
||||
## Arrays
|
||||
|
||||
## References
|
||||
|
||||
A reference is a pointer to a single cell or a range of cells. The reference can either be entered manually, for example "A4", or as the result of a calculation, such as the OFFSET Function or the INDIRECT Function. A reference can also be built, for example with the Colon (\:) Operator.
|
||||
BIN
docs/src/functions/images/hyperbolicarccosine-curve.png
Normal file
BIN
docs/src/functions/images/hyperbolicarccosine-curve.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 38 KiB |
BIN
docs/src/functions/images/hyperbolicarcsine-curve.png
Normal file
BIN
docs/src/functions/images/hyperbolicarcsine-curve.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 79 KiB |
BIN
docs/src/functions/images/hyperbolicarctangent-curve.png
Normal file
BIN
docs/src/functions/images/hyperbolicarctangent-curve.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 45 KiB |
@@ -16,7 +16,7 @@ You can track the progress in this [GitHub issue](https://github.com/ironcalc/Ir
|
||||
| CHOOSE | <Badge type="tip" text="Available" /> | – |
|
||||
| CHOOSECOLS | <Badge type="info" text="Not implemented yet" /> | – |
|
||||
| CHOOSEROWS | <Badge type="info" text="Not implemented yet" /> | – |
|
||||
| COLUMN | <Badge type="tip" text="Available" /> | – |
|
||||
| COLUMN | <Badge type="tip" text="Available" /> | [COLUMN](lookup_and_reference/column) |
|
||||
| COLUMNS | <Badge type="tip" text="Available" /> | – |
|
||||
| DROP | <Badge type="info" text="Not implemented yet" /> | – |
|
||||
| EXPAND | <Badge type="info" text="Not implemented yet" /> | – |
|
||||
|
||||
@@ -4,8 +4,28 @@ outline: deep
|
||||
lang: en-US
|
||||
---
|
||||
|
||||
# COLUMN
|
||||
|
||||
::: warning
|
||||
🚧 This function is implemented but currently lacks detailed documentation. For guidance, you may refer to the equivalent functionality in [Microsoft Excel documentation](https://support.microsoft.com/en-us/office/excel-functions-by-category-5f91f4e9-7b42-46d2-9bd1-63f26a86c0eb).
|
||||
:::
|
||||
# COLUMN function
|
||||
## Overview
|
||||
The COLUMN Function in IronCalc is a lookup & reference formula that is used to query and return the column number of a referenced Column or Cell.
|
||||
## Usage
|
||||
### Syntax
|
||||
**COLUMN(<span title="Reference" style="color:#1E88E5">reference</span>) => <span title="Number" style="color:#1E88E5">column</span>**
|
||||
### Argument descriptions
|
||||
* *reference* ([cell](/features/value-types#references), [optional](/features/optional-arguments.md)). The number of the cell you wish to reference the column number of.
|
||||
### Additional guidance
|
||||
* When referencing a range of cells, only the column number of the left most cell will be returned.
|
||||
* You are also able to reference complete columns instead of individual cells.
|
||||
### Returned value
|
||||
COLUMN returns the [number](/features/value-types#numbers) of the specific cell or column which is being referenced.
|
||||
### Error conditions
|
||||
* IronCalc currently does not support the referencing of cells with names.
|
||||
## Details
|
||||
The COLUMN Function can only be used to display the correlating number of a single column within a Sheet. If you wish to show the number of columns used within a specific range, you can use the COLUMNS Function.
|
||||
## Examples
|
||||
### No Cell Reference
|
||||
When no cell reference is made, the formula uses **=COLUMN()**. This will then output the column number of the cell where the formula is placed.<br><br>For example, if the formula is placed in cell A1, then "1" will be displayed.
|
||||
### With Cell Reference
|
||||
When a cell reference is made, the formula uses **=COLUMN([Referenced Cell])**. This will then output the column number of the referenced cell, regardless of where the formula is placed in the sheet.<br><br>For example, if the cell B1 is the referenced cell, "2" will be the output of the formula no matter where it is placed in the sheet.<br><br>**Note:** references do not always have to be specific cells, you can also reference complete columns. For example, **=COLUMN(B:B)** would also result in an output of "2".
|
||||
### Range References
|
||||
The COLUMN function can also be used to reference a range of Cells or Columns. In this case only the most left-hand column will be the resulting output.<br><br>For example, **=COLUMN(A1:J1)** will result in the ouput of "1".
|
||||
## Links
|
||||
@@ -13,16 +13,16 @@ You can track the progress in this [GitHub issue](https://github.com/ironcalc/Ir
|
||||
| --------------- | ---------------------------------------------- | ------------- |
|
||||
| ABS | <Badge type="tip" text="Available" /> | – |
|
||||
| ACOS | <Badge type="tip" text="Available" /> | [ACOS](math_and_trigonometry/acos) |
|
||||
| ACOSH | <Badge type="tip" text="Available" /> | – |
|
||||
| ACOSH | <Badge type="tip" text="Available" /> | [ACOSH](math_and_trigonometry/acosh) |
|
||||
| ACOT | <Badge type="info" text="Not implemented yet" /> | – |
|
||||
| ACOTH | <Badge type="info" text="Not implemented yet" /> | – |
|
||||
| AGGREGATE | <Badge type="info" text="Not implemented yet" /> | – |
|
||||
| ARABIC | <Badge type="info" text="Not implemented yet" /> | – |
|
||||
| ASIN | <Badge type="tip" text="Available" /> | [ASIN](math_and_trigonometry/asin) |
|
||||
| ASINH | <Badge type="tip" text="Available" /> | – |
|
||||
| ASINH | <Badge type="tip" text="Available" /> | [ASINH](math_and_trigonometry/asinh) |
|
||||
| ATAN | <Badge type="tip" text="Available" /> | [ATAN](math_and_trigonometry/atan) |
|
||||
| ATAN2 | <Badge type="tip" text="Available" /> | – |
|
||||
| ATANH | <Badge type="tip" text="Available" /> | – |
|
||||
| ATAN2 | <Badge type="tip" text="Available" /> | [ATAN2](math_and_trigonometry/atan2) |
|
||||
| ATANH | <Badge type="tip" text="Available" /> | [ATANH](math_and_trigonometry/atanh) |
|
||||
| BASE | <Badge type="info" text="Not implemented yet" /> | – |
|
||||
| CEILING | <Badge type="info" text="Not implemented yet" /> | – |
|
||||
| CEILING.MATH | <Badge type="info" text="Not implemented yet" /> | – |
|
||||
@@ -49,9 +49,9 @@ You can track the progress in this [GitHub issue](https://github.com/ironcalc/Ir
|
||||
| ISO.CEILING | <Badge type="info" text="Not implemented yet" /> | – |
|
||||
| LCM | <Badge type="info" text="Not implemented yet" /> | – |
|
||||
| LET | <Badge type="info" text="Not implemented yet" /> | – |
|
||||
| LN | <Badge type="info" text="Not implemented yet" /> | – |
|
||||
| LOG | <Badge type="info" text="Not implemented yet" /> | – |
|
||||
| LOG10 | <Badge type="info" text="Not implemented yet" /> | – |
|
||||
| LN | <Badge type="info" text="Available" /> | – |
|
||||
| LOG | <Badge type="info" text="Available" /> | – |
|
||||
| LOG10 | <Badge type="info" text="Available" /> | – |
|
||||
| MDETERM | <Badge type="info" text="Not implemented yet" /> | – |
|
||||
| MINVERSE | <Badge type="info" text="Not implemented yet" /> | – |
|
||||
| MMULT | <Badge type="info" text="Not implemented yet" /> | – |
|
||||
@@ -80,11 +80,11 @@ You can track the progress in this [GitHub issue](https://github.com/ironcalc/Ir
|
||||
| SIN | <Badge type="tip" text="Available" /> | [SIN](math_and_trigonometry/sin) |
|
||||
| SINH | <Badge type="tip" text="Available" /> | [SINH](math_and_trigonometry/sinh) |
|
||||
| SQRT | <Badge type="tip" text="Available" /> | – |
|
||||
| SQRTPI | <Badge type="info" text="Not implemented yet" /> | – |
|
||||
| SQRTPI | <Badge type="info" text="Available" /> | – |
|
||||
| SUBTOTAL | <Badge type="info" text="Not implemented yet" /> | – |
|
||||
| SUM | <Badge type="tip" text="Available" /> | – |
|
||||
| SUMIF | <Badge type="tip" text="Available" /> | – |
|
||||
| SUMIFS | <Badge type="info" text="Not implemented yet" /> | – |
|
||||
| SUMIFS | <Badge type="info" text="Available" /> | – |
|
||||
| SUMPRODUCT | <Badge type="info" text="Not implemented yet" /> | – |
|
||||
| SUMSQ | <Badge type="info" text="Not implemented yet" /> | – |
|
||||
| SUMX2MY2 | <Badge type="info" text="Not implemented yet" /> | – |
|
||||
|
||||
@@ -3,9 +3,40 @@ layout: doc
|
||||
outline: deep
|
||||
lang: en-US
|
||||
---
|
||||
# ACOSH function
|
||||
## Overview
|
||||
ACOSH is a function of the Math and Trigonometry category that calculates the inverse hyperbolic cosine (hyperbolic arccosine) of a number, returning a non-negative value in the range [0, +∞).
|
||||
## Usage
|
||||
### Syntax
|
||||
**ACOSH(<span title="Number" style="color:#1E88E5">number</span>) => <span title="Number" style="color:#1E88E5">acosh</span>**
|
||||
### Argument descriptions
|
||||
* *number* ([number](/features/value-types#numbers), required). The value whose hyperbolic arccosine is to be calculated. The value must be greater than or equal to 1.
|
||||
|
||||
# ACOSH
|
||||
### Additional guidance
|
||||
The hyperbolic arccosine function is defined as:
|
||||
|
||||
::: warning
|
||||
🚧 This function is implemented but currently lacks detailed documentation. For guidance, you may refer to the equivalent functionality in [Microsoft Excel documentation](https://support.microsoft.com/en-us/office/excel-functions-by-category-5f91f4e9-7b42-46d2-9bd1-63f26a86c0eb).
|
||||
:::
|
||||
$$
|
||||
\operatorname{acosh}(x) = \ln(x + \sqrt{x^2 - 1})
|
||||
$$
|
||||
|
||||
### Returned value
|
||||
ACOSH returns a [number](/features/value-types#numbers) in the range [0, +∞) that is the hyperbolic arccosine of the specified value, expressed in radians.
|
||||
### Error conditions
|
||||
* In common with many other IronCalc functions, ACOSH propagates errors that are found in its argument.
|
||||
* If no argument, or more than one argument, is supplied, then ACOSH returns the [`#ERROR!`](/features/error-types.md#error) error.
|
||||
* If the value of the *number* argument is not (or cannot be converted to) a [number](/features/value-types#numbers), then ACOSH returns the [`#VALUE!`](/features/error-types.md#value) error.
|
||||
* If the value of the *number* argument is less than 1, then ACOSH returns the [`#NUM!`](/features/error-types.md#num) error.
|
||||
<!--@include: ../markdown-snippets/error-type-details.txt-->
|
||||
## Details
|
||||
* The ACOSH function utilizes the *acosh()* method provided by the [Rust Standard Library](https://doc.rust-lang.org/std/).
|
||||
* The figure below illustrates the output of the ACOSH function for values $x \geq 1$ in the range [0, +∞).
|
||||
<center><img src="/functions/images/hyperbolicarccosine-curve.png" width="350" alt="Graph showing acosh(x) for x ≥ 1."></center>
|
||||
|
||||
## Examples
|
||||
[See some examples in IronCalc](https://app.ironcalc.com/?example=acosh).
|
||||
|
||||
## Links
|
||||
* For more information about inverse hyperbolic functions, visit Wikipedia's [Inverse hyperbolic functions](https://en.wikipedia.org/wiki/Inverse_hyperbolic_functions) page.
|
||||
* See also IronCalc's [COSH](/functions/math_and_trigonometry/cosh), [ASINH](/functions/math_and_trigonometry/asinh) and [ATANH](/functions/math_and_trigonometry/atanh) functions.
|
||||
* Visit Microsoft Excel's [ACOSH function](https://support.microsoft.com/en-us/office/acosh-function-e3992cc1-103f-4e72-9f04-624b9ef5ebfe) page.
|
||||
* Both [Google Sheets](https://support.google.com/docs/answer/3093391) and [LibreOffice Calc](https://wiki.documentfoundation.org/Documentation/Calc_Functions/ACOSH) provide versions of the ACOSH function.
|
||||
@@ -4,8 +4,36 @@ outline: deep
|
||||
lang: en-US
|
||||
---
|
||||
|
||||
# ASINH
|
||||
# ASINH function
|
||||
## Overview
|
||||
ASINH is a function of the Math and Trigonometry category that calculates the inverse hyperbolic sine (hyperbolic arcsine) of a number, returning the hyperbolic angle expressed in radians.
|
||||
## Usage
|
||||
### Syntax
|
||||
**ASINH(<span title="Number" style="color:#1E88E5">number</span>) => <span title="Number" style="color:#1E88E5">asinh</span>**
|
||||
### Argument descriptions
|
||||
* *number* ([number](/features/value-types#numbers), required). The value whose inverse hyperbolic sine is to be calculated.
|
||||
### Additional guidance
|
||||
The hyperbolic arcsine function is defined as:
|
||||
$$
|
||||
\operatorname{asinh}(x) = \ln\!\left(x + \sqrt{x^2 + 1}\,\right)
|
||||
$$
|
||||
### Returned value
|
||||
ASINH returns a real [number](/features/value-types#numbers) in the range (-∞, +∞) that is the hyperbolic arcsine of the specified value, expressed in radians.
|
||||
### Error conditions
|
||||
* In common with many other IronCalc functions, ASINH propagates errors that are found in its argument.
|
||||
* If no argument, or more than one argument, is supplied, then ASINH returns the [`#ERROR!`](/features/error-types.md#error) error.
|
||||
* If the value of the *number* argument is not (or cannot be converted to) a [number](/features/value-types#numbers), then ASINH returns the [`#VALUE!`](/features/error-types.md#value) error.
|
||||
<!--@include: ../markdown-snippets/error-type-details.txt-->
|
||||
## Details
|
||||
* The ASINH function utilizes the *asinh()* method provided by the [Rust Standard Library](https://doc.rust-lang.org/std/).
|
||||
* The figure below illustrates the output of the ASINH function.
|
||||
<center><img src="/functions/images/hyperbolicarcsine-curve.png" width="350" alt="Graph showing asinh(x)."></center>
|
||||
|
||||
::: warning
|
||||
🚧 This function is implemented but currently lacks detailed documentation. For guidance, you may refer to the equivalent functionality in [Microsoft Excel documentation](https://support.microsoft.com/en-us/office/excel-functions-by-category-5f91f4e9-7b42-46d2-9bd1-63f26a86c0eb).
|
||||
:::
|
||||
## Examples
|
||||
[See some examples in IronCalc](https://app.ironcalc.com/?example=asinh).
|
||||
|
||||
## Links
|
||||
* For more information about inverse hyperbolic functions, visit Wikipedia's [Inverse hyperbolic functions](https://en.wikipedia.org/wiki/Inverse_hyperbolic_functions) page.
|
||||
* See also IronCalc's [SINH](/functions/math_and_trigonometry/sinh), [ACOSH](/functions/math_and_trigonometry/acosh) and [ATANH](/functions/math_and_trigonometry/atanh) functions.
|
||||
* Visit Microsoft Excel's [ASINH function](https://support.microsoft.com/de-de/office/asinh-function-62b4f5b6-d9cc-4c17-9d04-aa5371806c74) page.
|
||||
* Both [Google Sheets](https://support.google.com/docs/answer/3093393) and [LibreOffice Calc](https://wiki.documentfoundation.org/Documentation/Calc_Functions/ASINH) provide versions of the ASINH function.
|
||||
@@ -4,8 +4,34 @@ outline: deep
|
||||
lang: en-US
|
||||
---
|
||||
|
||||
# ATAN2
|
||||
# ATAN2 function
|
||||
## Overview
|
||||
ATAN2 is a function of the Math and Trigonometry category that calculates the inverse tangent (arctangent) for the specified *x* and *y* coordinates. The arctangent returns the angle defined by the x-axis and a line defined by the origin and a point with coordinates (x,y). The returned angle is expressed in radians, in the range (-$\pi$, +$\pi$].
|
||||
## Usage
|
||||
### Syntax
|
||||
**ATAN2(<span title="Number" style="color:#1E88E5">x,y</span>) => <span title="Number" style="color:#1E88E5">atan2</span>**
|
||||
### Argument descriptions
|
||||
* *x* ([number](/features/value-types#numbers), required). Value of the x coordinate.
|
||||
* *y* ([number](/features/value-types#numbers), required). Value of the y coordinate.
|
||||
### Additional guidance
|
||||
If the returned value is positive, it represents a counterclockwise angle from the x-axis, while a negative value represents a clockwise angle.
|
||||
ATAN2(x,y) is equivalent to ATAN(y/x), with the difference that the x argument in ATAN2 can be 0.
|
||||
### Returned value
|
||||
ATAN2 returns a number in radians in the range (-$\pi$, +$\pi$] that is the inverse tangent for the specified x and y coordinates.
|
||||
### Error conditions
|
||||
* In common with many other IronCalc functions, ATAN2 propagates errors that are found in its argument.
|
||||
* If no argument, or arguments other than 2, are supplied, then ATAN2 returns the [`#ERROR!`](/features/error-types.md#error) error.
|
||||
* If the value of either the *x* or *y* argument is not (or cannot be converted to) a [number](/features/value-types#numbers), then ATAN2 returns the [`#VALUE!`](/features/error-types.md#value) error.
|
||||
* If both *x* and *y* are equal to 0, ATAN2 returns a [`#DIV/0!`](/features/error-types.md#div-0) error.
|
||||
<!--@include: ../markdown-snippets/error-type-details.txt-->
|
||||
## Details
|
||||
* The ATAN2 function utilizes the *atan2()* method provided by the [Rust Standard Library](https://doc.rust-lang.org/std/).
|
||||
## Examples
|
||||
[See some examples in IronCalc](https://app.ironcalc.com/?example=atan2).
|
||||
|
||||
## Links
|
||||
* For more information about inverse trigonometric functions, visit Wikipedia's [Inverse trigonometric functions](https://en.wikipedia.org/wiki/Inverse_trigonometric_functions) page.
|
||||
* See also IronCalc's [ATAN](/functions/math_and_trigonometry/atan), [TAN](/functions/math_and_trigonometry/tan) and [ASIN](/functions/math_and_trigonometry/asin) functions.
|
||||
* Visit Microsoft Excel's [ATAN2 function](https://support.microsoft.com/en-us/office/atan2-function-51123ced-348c-416a-b2e2-833f7868569f) page.
|
||||
* Both [Google Sheets](https://support.google.com/docs/answer/3093468) and [LibreOffice Calc](https://wiki.documentfoundation.org/Documentation/Calc_Functions/ATAN2) provide versions of the ATAN2 function.
|
||||
|
||||
::: warning
|
||||
🚧 This function is implemented but currently lacks detailed documentation. For guidance, you may refer to the equivalent functionality in [Microsoft Excel documentation](https://support.microsoft.com/en-us/office/excel-functions-by-category-5f91f4e9-7b42-46d2-9bd1-63f26a86c0eb).
|
||||
:::
|
||||
@@ -4,8 +4,37 @@ outline: deep
|
||||
lang: en-US
|
||||
---
|
||||
|
||||
# ATANH
|
||||
# ATANH function
|
||||
## Overview
|
||||
ATANH is a function of the Math and Trigonometry category that calculates the inverse hyperbolic tangent (hyperbolic arctangent) of a number in the range (-1, +1), returning the hyperbolic angle expressed in radians.
|
||||
## Usage
|
||||
### Syntax
|
||||
**ATANH(<span title="Number" style="color:#1E88E5">number</span>) => <span title="Number" style="color:#1E88E5">atanh</span>**
|
||||
### Argument descriptions
|
||||
* *number* ([number](/features/value-types#numbers), required). The value whose inverse hyperbolic tangent is to be calculated, in the range (-1,+1).
|
||||
### Additional guidance
|
||||
The hyperbolic arctangent function is defined as:
|
||||
$$
|
||||
\operatorname{atanh}(x) = \tfrac{1}{2}\,\ln\!\left(\dfrac{1+x}{1-x}\right),\quad |x| < 1
|
||||
$$
|
||||
### Returned value
|
||||
ATANH returns a real [number](/features/value-types#numbers) in the range (-∞, +∞) that is the hyperbolic arctangent of the specified value, expressed in radians.
|
||||
### Error conditions
|
||||
* In common with many other IronCalc functions, ATANH propagates errors that are found in its argument.
|
||||
* If no argument, or more than one argument, is supplied, then ATANH returns the [`#ERROR!`](/features/error-types.md#error) error.
|
||||
* If the value of the *number* argument is not (or cannot be converted to) a [number](/features/value-types#numbers), then ATANH returns the [`#VALUE!`](/features/error-types.md#value) error.
|
||||
* If the value of the *number* argument lies outside the domain (-1, +1), then ATANH returns the [`#NUM!`](/features/error-types.md#num) error.
|
||||
<!--@include: ../markdown-snippets/error-type-details.txt-->
|
||||
## Details
|
||||
* The ATANH function utilizes the *atanh()* method provided by the [Rust Standard Library](https://doc.rust-lang.org/std/).
|
||||
* The figure below illustrates the output of the ATANH function.
|
||||
<center><img src="/functions/images/hyperbolicarctangent-curve.png" width="350" alt="Graph showing atanh(x)."></center>
|
||||
|
||||
::: warning
|
||||
🚧 This function is implemented but currently lacks detailed documentation. For guidance, you may refer to the equivalent functionality in [Microsoft Excel documentation](https://support.microsoft.com/en-us/office/excel-functions-by-category-5f91f4e9-7b42-46d2-9bd1-63f26a86c0eb).
|
||||
:::
|
||||
## Examples
|
||||
[See some examples in IronCalc](https://app.ironcalc.com/?example=atanh).
|
||||
|
||||
## Links
|
||||
* For more information about inverse hyperbolic functions, visit Wikipedia's [Inverse hyperbolic functions](https://en.wikipedia.org/wiki/Inverse_hyperbolic_functions) page.
|
||||
* See also IronCalc's [ASINH](/functions/math_and_trigonometry/asinh), [ACOSH](/functions/math_and_trigonometry/acosh) and [TANH](/functions/math_and_trigonometry/tanh) functions.
|
||||
* Visit Microsoft Excel's [ATANH function](https://support.microsoft.com/de-de/office/atanh-function-453534d1-76a5-4f17-8c04-c3f2feee0dd5) page.
|
||||
* Both [Google Sheets](https://support.google.com/docs/answer/3093397) and [LibreOffice Calc](https://wiki.documentfoundation.org/Documentation/Calc_Functions/ATANH) provide versions of the ATANH function.
|
||||
@@ -7,6 +7,5 @@ lang: en-US
|
||||
# LN
|
||||
|
||||
::: warning
|
||||
🚧 This function is not yet available in IronCalc.
|
||||
[Follow development here](https://github.com/ironcalc/IronCalc/labels/Functions)
|
||||
🚧 This function is implemented but currently lacks detailed documentation. For guidance, you may refer to the equivalent functionality in [Microsoft Excel documentation](https://support.microsoft.com/en-us/office/excel-functions-by-category-5f91f4e9-7b42-46d2-9bd1-63f26a86c0eb).
|
||||
:::
|
||||
@@ -7,6 +7,5 @@ lang: en-US
|
||||
# LOG
|
||||
|
||||
::: warning
|
||||
🚧 This function is not yet available in IronCalc.
|
||||
[Follow development here](https://github.com/ironcalc/IronCalc/labels/Functions)
|
||||
🚧 This function is implemented but currently lacks detailed documentation. For guidance, you may refer to the equivalent functionality in [Microsoft Excel documentation](https://support.microsoft.com/en-us/office/excel-functions-by-category-5f91f4e9-7b42-46d2-9bd1-63f26a86c0eb).
|
||||
:::
|
||||
@@ -7,6 +7,5 @@ lang: en-US
|
||||
# LOG10
|
||||
|
||||
::: warning
|
||||
🚧 This function is not yet available in IronCalc.
|
||||
[Follow development here](https://github.com/ironcalc/IronCalc/labels/Functions)
|
||||
🚧 This function is implemented but currently lacks detailed documentation. For guidance, you may refer to the equivalent functionality in [Microsoft Excel documentation](https://support.microsoft.com/en-us/office/excel-functions-by-category-5f91f4e9-7b42-46d2-9bd1-63f26a86c0eb).
|
||||
:::
|
||||
@@ -13,7 +13,6 @@ import type { WorkbookState } from "../workbookState";
|
||||
type FormulaBarProps = {
|
||||
cellAddress: string;
|
||||
formulaValue: string;
|
||||
isPartOfArray: boolean;
|
||||
model: Model;
|
||||
workbookState: WorkbookState;
|
||||
onChange: () => void;
|
||||
@@ -24,7 +23,6 @@ function FormulaBar(properties: FormulaBarProps) {
|
||||
const {
|
||||
cellAddress,
|
||||
formulaValue,
|
||||
isPartOfArray,
|
||||
model,
|
||||
onChange,
|
||||
onTextUpdated,
|
||||
@@ -64,9 +62,6 @@ function FormulaBar(properties: FormulaBarProps) {
|
||||
event.stopPropagation();
|
||||
event.preventDefault();
|
||||
}}
|
||||
sx={{
|
||||
color: isPartOfArray ? "grey" : "black",
|
||||
}}
|
||||
>
|
||||
<Editor
|
||||
originalText={formulaValue}
|
||||
|
||||
@@ -362,19 +362,6 @@ const Workbook = (props: { model: Model; workbookState: WorkbookState }) => {
|
||||
return model.getCellContent(sheet, row, column);
|
||||
};
|
||||
|
||||
// returns true if it is either single cell or the root cell of an array
|
||||
const isRootCellOfArray = () => {
|
||||
const { sheet, row, column } = model.getSelectedView();
|
||||
const r = model.getCellArrayStructure(sheet, row, column);
|
||||
if (r === "SingleCell") {
|
||||
return false;
|
||||
}
|
||||
if ("DynamicMother" in r) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
const getCellStyle = useCallback(() => {
|
||||
const { sheet, row, column } = model.getSelectedView();
|
||||
return model.getCellStyle(sheet, row, column);
|
||||
@@ -718,7 +705,6 @@ const Workbook = (props: { model: Model; workbookState: WorkbookState }) => {
|
||||
}}
|
||||
model={model}
|
||||
workbookState={workbookState}
|
||||
isPartOfArray={isRootCellOfArray()}
|
||||
/>
|
||||
<Worksheet
|
||||
model={model}
|
||||
|
||||
@@ -14,7 +14,6 @@ import {
|
||||
LAST_COLUMN,
|
||||
LAST_ROW,
|
||||
ROW_HEIGH_SCALE,
|
||||
cellArrayStructureColor,
|
||||
outlineBackgroundColor,
|
||||
outlineColor,
|
||||
} from "../WorksheetCanvas/constants";
|
||||
@@ -60,7 +59,6 @@ const Worksheet = forwardRef(
|
||||
const spacerElement = useRef<HTMLDivElement>(null);
|
||||
const cellOutline = useRef<HTMLDivElement>(null);
|
||||
const areaOutline = useRef<HTMLDivElement>(null);
|
||||
const cellArrayStructure = useRef<HTMLDivElement>(null);
|
||||
const extendToOutline = useRef<HTMLDivElement>(null);
|
||||
const columnResizeGuide = useRef<HTMLDivElement>(null);
|
||||
const rowResizeGuide = useRef<HTMLDivElement>(null);
|
||||
@@ -87,7 +85,6 @@ const Worksheet = forwardRef(
|
||||
|
||||
const outline = cellOutline.current;
|
||||
const area = areaOutline.current;
|
||||
const arrayStructure = cellArrayStructure.current;
|
||||
const extendTo = extendToOutline.current;
|
||||
const editor = editorElement.current;
|
||||
|
||||
@@ -101,8 +98,7 @@ const Worksheet = forwardRef(
|
||||
!area ||
|
||||
!extendTo ||
|
||||
!scrollElement.current ||
|
||||
!editor ||
|
||||
!arrayStructure
|
||||
!editor
|
||||
)
|
||||
return;
|
||||
// FIXME: This two need to be computed.
|
||||
@@ -119,7 +115,6 @@ const Worksheet = forwardRef(
|
||||
rowGuide: rowGuideRef,
|
||||
columnHeaders: columnHeadersRef,
|
||||
cellOutline: outline,
|
||||
cellArrayStructure: arrayStructure,
|
||||
areaOutline: area,
|
||||
extendToOutline: extendTo,
|
||||
editor: editor,
|
||||
@@ -334,7 +329,6 @@ const Worksheet = forwardRef(
|
||||
/>
|
||||
</EditorWrapper>
|
||||
<AreaOutline ref={areaOutline} />
|
||||
<CellArrayStructure ref={cellArrayStructure} />
|
||||
<ExtendToOutline ref={extendToOutline} />
|
||||
<ColumnResizeGuide ref={columnResizeGuide} />
|
||||
<RowResizeGuide ref={rowResizeGuide} />
|
||||
@@ -520,12 +514,6 @@ const AreaOutline = styled("div")`
|
||||
background-color: ${outlineBackgroundColor};
|
||||
`;
|
||||
|
||||
const CellArrayStructure = styled("div")`
|
||||
position: absolute;
|
||||
border: 1px solid ${cellArrayStructureColor};
|
||||
border-radius: 3px;
|
||||
`;
|
||||
|
||||
const CellOutline = styled("div")`
|
||||
position: absolute;
|
||||
border: 2px solid ${outlineColor};
|
||||
|
||||
@@ -13,7 +13,6 @@ export const defaultTextColor = "#2E414D";
|
||||
|
||||
export const outlineColor = "#F2994A";
|
||||
export const outlineBackgroundColor = "#F2994A1A";
|
||||
export const cellArrayStructureColor = "#64BDFDA1";
|
||||
|
||||
export const LAST_COLUMN = 16_384;
|
||||
export const LAST_ROW = 1_048_576;
|
||||
|
||||
@@ -31,7 +31,6 @@ export interface CanvasSettings {
|
||||
canvas: HTMLCanvasElement;
|
||||
cellOutline: HTMLDivElement;
|
||||
areaOutline: HTMLDivElement;
|
||||
cellArrayStructure: HTMLDivElement;
|
||||
extendToOutline: HTMLDivElement;
|
||||
columnGuide: HTMLDivElement;
|
||||
rowGuide: HTMLDivElement;
|
||||
@@ -91,8 +90,6 @@ export default class WorksheetCanvas {
|
||||
|
||||
cellOutlineHandle: HTMLDivElement;
|
||||
|
||||
cellArrayStructure: HTMLDivElement;
|
||||
|
||||
extendToOutline: HTMLDivElement;
|
||||
|
||||
workbookState: WorkbookState;
|
||||
@@ -127,7 +124,6 @@ export default class WorksheetCanvas {
|
||||
this.refresh = options.refresh;
|
||||
|
||||
this.cellOutline = options.elements.cellOutline;
|
||||
this.cellArrayStructure = options.elements.cellArrayStructure;
|
||||
this.areaOutline = options.elements.areaOutline;
|
||||
this.extendToOutline = options.elements.extendToOutline;
|
||||
this.rowGuide = options.elements.rowGuide;
|
||||
@@ -1519,20 +1515,16 @@ export default class WorksheetCanvas {
|
||||
}
|
||||
|
||||
private drawCellOutline(): void {
|
||||
const { cellArrayStructure, cellOutline, areaOutline, cellOutlineHandle } =
|
||||
this;
|
||||
const { cellOutline, areaOutline, cellOutlineHandle } = this;
|
||||
if (this.workbookState.getEditingCell()) {
|
||||
cellOutline.style.visibility = "hidden";
|
||||
cellOutlineHandle.style.visibility = "hidden";
|
||||
areaOutline.style.visibility = "hidden";
|
||||
cellArrayStructure.style.visibility = "hidden";
|
||||
return;
|
||||
}
|
||||
cellOutline.style.visibility = "visible";
|
||||
cellOutlineHandle.style.visibility = "visible";
|
||||
areaOutline.style.visibility = "visible";
|
||||
// This one is hidden by default
|
||||
cellArrayStructure.style.visibility = "hidden";
|
||||
|
||||
const [selectedSheet, selectedRow, selectedColumn] =
|
||||
this.model.getSelectedCell();
|
||||
@@ -1588,34 +1580,6 @@ export default class WorksheetCanvas {
|
||||
[handleX, handleY] = this.getCoordinatesByCell(rowStart, columnStart);
|
||||
handleX += this.getColumnWidth(selectedSheet, columnStart);
|
||||
handleY += this.getRowHeight(selectedSheet, rowStart);
|
||||
// we draw the array structure if needed only in this case
|
||||
const arrayStructure = this.model.getCellArrayStructure(
|
||||
selectedSheet,
|
||||
selectedRow,
|
||||
selectedColumn,
|
||||
);
|
||||
let array = null;
|
||||
if (arrayStructure === "SingleCell") {
|
||||
// nothing to see here
|
||||
} else if ("DynamicMother" in arrayStructure) {
|
||||
cellArrayStructure.style.visibility = "visible";
|
||||
const [arrayWidth, arrayHeight] = arrayStructure.DynamicMother;
|
||||
array = [selectedRow, selectedColumn, arrayWidth, arrayHeight];
|
||||
} else {
|
||||
cellArrayStructure.style.visibility = "visible";
|
||||
array = arrayStructure.DynamicChild;
|
||||
}
|
||||
if (array !== null) {
|
||||
const [arrayX, arrayY] = this.getCoordinatesByCell(array[0], array[1]);
|
||||
const [arrayX1, arrayY1] = this.getCoordinatesByCell(
|
||||
array[0] + array[3],
|
||||
array[1] + array[2],
|
||||
);
|
||||
cellArrayStructure.style.left = `${arrayX}px`;
|
||||
cellArrayStructure.style.top = `${arrayY}px`;
|
||||
cellArrayStructure.style.width = `${arrayX1 - arrayX}px`;
|
||||
cellArrayStructure.style.height = `${arrayY1 - arrayY}px`;
|
||||
}
|
||||
} else {
|
||||
areaOutline.style.visibility = "visible";
|
||||
cellOutlineHandle.style.visibility = "visible";
|
||||
|
||||
@@ -2,6 +2,7 @@ import "./App.css";
|
||||
import styled from "@emotion/styled";
|
||||
import { useEffect, useState } from "react";
|
||||
import { FileBar } from "./components/FileBar";
|
||||
import LeftDrawer from "./components/LeftDrawer/LeftDrawer";
|
||||
import {
|
||||
get_documentation_model,
|
||||
get_model,
|
||||
@@ -9,7 +10,10 @@ import {
|
||||
} from "./components/rpc";
|
||||
import {
|
||||
createNewModel,
|
||||
deleteModelByUuid,
|
||||
deleteSelectedModel,
|
||||
// getModelsMetadata,
|
||||
// getSelectedUuid,
|
||||
loadModelFromStorageOrCreate,
|
||||
saveModelToStorage,
|
||||
saveSelectedModelInStorage,
|
||||
@@ -21,6 +25,7 @@ import { IronCalc, IronCalcIcon, Model, init } from "@ironcalc/workbook";
|
||||
|
||||
function App() {
|
||||
const [model, setModel] = useState<Model | null>(null);
|
||||
const [isDrawerOpen, setIsDrawerOpen] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
async function start() {
|
||||
@@ -79,48 +84,80 @@ function App() {
|
||||
// We could use context for model, but the problem is that it should initialized to null.
|
||||
// Passing the property down makes sure it is always defined.
|
||||
|
||||
// Handlers for model changes that also update our models state
|
||||
const handleNewModel = () => {
|
||||
const newModel = createNewModel();
|
||||
setModel(newModel);
|
||||
};
|
||||
|
||||
const handleSetModel = (uuid: string) => {
|
||||
const newModel = selectModelFromStorage(uuid);
|
||||
if (newModel) {
|
||||
setModel(newModel);
|
||||
}
|
||||
};
|
||||
|
||||
const handleDeleteModel = () => {
|
||||
const newModel = deleteSelectedModel();
|
||||
if (newModel) {
|
||||
setModel(newModel);
|
||||
}
|
||||
};
|
||||
|
||||
const handleDeleteModelByUuid = (uuid: string) => {
|
||||
const newModel = deleteModelByUuid(uuid);
|
||||
if (newModel) {
|
||||
setModel(newModel);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Wrapper>
|
||||
<FileBar
|
||||
model={model}
|
||||
onModelUpload={async (arrayBuffer: ArrayBuffer, fileName: string) => {
|
||||
const blob = await uploadFile(arrayBuffer, fileName);
|
||||
|
||||
const bytes = new Uint8Array(await blob.arrayBuffer());
|
||||
const newModel = Model.from_bytes(bytes);
|
||||
saveModelToStorage(newModel);
|
||||
|
||||
setModel(newModel);
|
||||
}}
|
||||
newModel={() => {
|
||||
setModel(createNewModel());
|
||||
}}
|
||||
setModel={(uuid: string) => {
|
||||
const newModel = selectModelFromStorage(uuid);
|
||||
if (newModel) {
|
||||
setModel(newModel);
|
||||
}
|
||||
}}
|
||||
onDelete={() => {
|
||||
const newModel = deleteSelectedModel();
|
||||
if (newModel) {
|
||||
setModel(newModel);
|
||||
}
|
||||
}}
|
||||
<AppContainer>
|
||||
<LeftDrawer
|
||||
open={isDrawerOpen}
|
||||
onClose={() => setIsDrawerOpen(false)}
|
||||
newModel={handleNewModel}
|
||||
setModel={handleSetModel}
|
||||
onDelete={handleDeleteModelByUuid}
|
||||
/>
|
||||
<IronCalc model={model} />
|
||||
</Wrapper>
|
||||
|
||||
<MainContent isDrawerOpen={isDrawerOpen}>
|
||||
<FileBar
|
||||
model={model}
|
||||
onModelUpload={async (arrayBuffer: ArrayBuffer, fileName: string) => {
|
||||
const blob = await uploadFile(arrayBuffer, fileName);
|
||||
const bytes = new Uint8Array(await blob.arrayBuffer());
|
||||
const newModel = Model.from_bytes(bytes);
|
||||
saveModelToStorage(newModel);
|
||||
setModel(newModel);
|
||||
}}
|
||||
newModel={handleNewModel}
|
||||
setModel={handleSetModel}
|
||||
onDelete={handleDeleteModel}
|
||||
isDrawerOpen={isDrawerOpen}
|
||||
setIsDrawerOpen={setIsDrawerOpen}
|
||||
/>
|
||||
<IronCalc model={model} />
|
||||
</MainContent>
|
||||
</AppContainer>
|
||||
);
|
||||
}
|
||||
|
||||
const Wrapper = styled("div")`
|
||||
margin: 0px;
|
||||
padding: 0px;
|
||||
const AppContainer = styled("div")`
|
||||
display: flex;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
`;
|
||||
|
||||
const MainContent = styled("div")<{ isDrawerOpen: boolean }>`
|
||||
margin-left: ${({ isDrawerOpen }) => (isDrawerOpen ? "0px" : "-264px")};
|
||||
transition: margin-left 0.3s ease;
|
||||
width: ${({ isDrawerOpen }) =>
|
||||
isDrawerOpen ? "calc(100% - 264px)" : "100%"};
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
position: absolute;
|
||||
`;
|
||||
|
||||
const Loading = styled("div")`
|
||||
|
||||
@@ -12,19 +12,9 @@ function DeleteWorkbookDialog(properties: DeleteWorkbookDialogProperties) {
|
||||
const deleteButtonRef = useRef<HTMLButtonElement>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const root = document.getElementById("root");
|
||||
if (root) {
|
||||
root.style.filter = "blur(2px)";
|
||||
}
|
||||
if (deleteButtonRef.current) {
|
||||
deleteButtonRef.current.focus();
|
||||
}
|
||||
return () => {
|
||||
const root = document.getElementById("root");
|
||||
if (root) {
|
||||
root.style.filter = "none";
|
||||
}
|
||||
};
|
||||
}, []);
|
||||
|
||||
return (
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import styled from "@emotion/styled";
|
||||
import type { Model } from "@ironcalc/workbook";
|
||||
import { IronCalcIcon, IronCalcLogo } from "@ironcalc/workbook";
|
||||
import { Button, IconButton } from "@mui/material";
|
||||
import { PanelLeftClose, PanelLeftOpen } from "lucide-react";
|
||||
import { useLayoutEffect, useRef, useState } from "react";
|
||||
import { FileMenu } from "./FileMenu";
|
||||
import { HelpMenu } from "./HelpMenu";
|
||||
import { DesktopMenu, MobileMenu } from "./FileMenu";
|
||||
import { ShareButton } from "./ShareButton";
|
||||
import ShareWorkbookDialog from "./ShareWorkbookDialog";
|
||||
import { WorkbookTitle } from "./WorkbookTitle";
|
||||
@@ -30,6 +30,8 @@ export function FileBar(properties: {
|
||||
setModel: (key: string) => void;
|
||||
onModelUpload: (blob: ArrayBuffer, fileName: string) => Promise<void>;
|
||||
onDelete: () => void;
|
||||
isDrawerOpen: boolean;
|
||||
setIsDrawerOpen: (open: boolean) => void;
|
||||
}) {
|
||||
const [isDialogOpen, setIsDialogOpen] = useState(false);
|
||||
const spacerRef = useRef<HTMLDivElement>(null);
|
||||
@@ -45,24 +47,49 @@ export function FileBar(properties: {
|
||||
}
|
||||
}, [width]);
|
||||
|
||||
// Common handler functions for both menu types
|
||||
const handleDownload = async () => {
|
||||
const model = properties.model;
|
||||
const bytes = model.toBytes();
|
||||
const fileName = model.getName();
|
||||
await downloadModel(bytes, fileName);
|
||||
};
|
||||
|
||||
return (
|
||||
<FileBarWrapper>
|
||||
<StyledDesktopLogo />
|
||||
<StyledIronCalcIcon />
|
||||
<Divider />
|
||||
<FileMenu
|
||||
newModel={properties.newModel}
|
||||
setModel={properties.setModel}
|
||||
onModelUpload={properties.onModelUpload}
|
||||
onDownload={async () => {
|
||||
const model = properties.model;
|
||||
const bytes = model.toBytes();
|
||||
const fileName = model.getName();
|
||||
await downloadModel(bytes, fileName);
|
||||
}}
|
||||
onDelete={properties.onDelete}
|
||||
/>
|
||||
<HelpMenu />
|
||||
<DrawerButton
|
||||
$isDrawerOpen={properties.isDrawerOpen}
|
||||
onClick={() => properties.setIsDrawerOpen(!properties.isDrawerOpen)}
|
||||
disableRipple
|
||||
title="Toggle sidebar"
|
||||
>
|
||||
{properties.isDrawerOpen ? <PanelLeftClose /> : <PanelLeftOpen />}
|
||||
</DrawerButton>
|
||||
<DesktopButtonsWrapper>
|
||||
<DesktopMenu
|
||||
newModel={properties.newModel}
|
||||
setModel={properties.setModel}
|
||||
onModelUpload={properties.onModelUpload}
|
||||
onDownload={handleDownload}
|
||||
onDelete={properties.onDelete}
|
||||
/>
|
||||
<FileBarButton
|
||||
disableRipple
|
||||
onClick={() => window.open("https://docs.ironcalc.com", "_blank")}
|
||||
>
|
||||
Help
|
||||
</FileBarButton>
|
||||
</DesktopButtonsWrapper>
|
||||
<MobileButtonsWrapper>
|
||||
<MobileMenu
|
||||
newModel={properties.newModel}
|
||||
setModel={properties.setModel}
|
||||
onModelUpload={properties.onModelUpload}
|
||||
onDownload={handleDownload}
|
||||
onDelete={properties.onDelete}
|
||||
/>
|
||||
</MobileButtonsWrapper>
|
||||
<Spacer ref={spacerRef} />
|
||||
<WorkbookTitleWrapper>
|
||||
<WorkbookTitle
|
||||
name={properties.model.getName()}
|
||||
@@ -88,12 +115,8 @@ export function FileBar(properties: {
|
||||
);
|
||||
}
|
||||
|
||||
// We want the workbook title to be exactly an the center of the page,
|
||||
// so we need an absolute position
|
||||
const WorkbookTitleWrapper = styled("div")`
|
||||
position: absolute;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
position: relative;
|
||||
`;
|
||||
|
||||
// The "Spacer" component occupies as much space as possible between the menu and the share button
|
||||
@@ -101,38 +124,79 @@ const Spacer = styled("div")`
|
||||
flex-grow: 1;
|
||||
`;
|
||||
|
||||
const StyledDesktopLogo = styled(IronCalcLogo)`
|
||||
width: 120px;
|
||||
margin-left: 12px;
|
||||
@media (max-width: 769px) {
|
||||
display: none;
|
||||
const DrawerButton = styled(IconButton)<{ $isDrawerOpen: boolean }>`
|
||||
margin-left: 8px;
|
||||
height: 32px;
|
||||
width: 32px;
|
||||
padding: 8px;
|
||||
border-radius: 4px;
|
||||
cursor: ${(props) => (props.$isDrawerOpen ? "w-resize" : "e-resize")};
|
||||
svg {
|
||||
stroke-width: 2px;
|
||||
stroke: #757575;
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
}
|
||||
`;
|
||||
|
||||
const StyledIronCalcIcon = styled(IronCalcIcon)`
|
||||
width: 36px;
|
||||
margin-left: 10px;
|
||||
@media (min-width: 769px) {
|
||||
display: none;
|
||||
&:hover {
|
||||
background-color: #f2f2f2;
|
||||
}
|
||||
&:active {
|
||||
background-color: #e0e0e0;
|
||||
}
|
||||
`;
|
||||
|
||||
const Divider = styled("div")`
|
||||
margin: 0px 8px 0px 16px;
|
||||
height: 12px;
|
||||
border-left: 1px solid #e0e0e0;
|
||||
`;
|
||||
|
||||
// The container must be relative positioned so we can position the title absolutely
|
||||
const FileBarWrapper = styled("div")`
|
||||
position: relative;
|
||||
height: 60px;
|
||||
min-height: 60px;
|
||||
width: 100%;
|
||||
background: #fff;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
border-bottom: 1px solid #e0e0e0;
|
||||
justify-content: space-between;
|
||||
box-sizing: border-box;
|
||||
`;
|
||||
|
||||
const DesktopButtonsWrapper = styled("div")`
|
||||
display: flex;
|
||||
gap: 4px;
|
||||
margin-left: 8px;
|
||||
@media (max-width: 600px) {
|
||||
display: none;
|
||||
}
|
||||
`;
|
||||
|
||||
const MobileButtonsWrapper = styled("div")`
|
||||
display: flex;
|
||||
gap: 4px;
|
||||
@media (min-width: 601px) {
|
||||
display: none;
|
||||
}
|
||||
@media (max-width: 600px) {
|
||||
display: flex;
|
||||
}
|
||||
`;
|
||||
|
||||
const FileBarButton = styled(Button)`
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
font-size: 12px;
|
||||
height: 32px;
|
||||
width: auto;
|
||||
padding: 4px 8px;
|
||||
font-weight: 400;
|
||||
min-width: 0px;
|
||||
text-transform: capitalize;
|
||||
color: #333333;
|
||||
&:hover {
|
||||
background-color: #f2f2f2;
|
||||
}
|
||||
&:active {
|
||||
background-color: #e0e0e0;
|
||||
}
|
||||
`;
|
||||
|
||||
const DialogContainer = styled("div")`
|
||||
|
||||
@@ -1,93 +1,165 @@
|
||||
import styled from "@emotion/styled";
|
||||
import { Menu, MenuItem, Modal } from "@mui/material";
|
||||
import { Check, FileDown, FileUp, Plus, Trash2 } from "lucide-react";
|
||||
import { Button, IconButton, Menu, MenuItem, Modal } from "@mui/material";
|
||||
import {
|
||||
ChevronRight,
|
||||
EllipsisVertical,
|
||||
FileDown,
|
||||
FileUp,
|
||||
Plus,
|
||||
Trash2,
|
||||
} from "lucide-react";
|
||||
import { useRef, useState } from "react";
|
||||
import DeleteWorkbookDialog from "./DeleteWorkbookDialog";
|
||||
import UploadFileDialog from "./UploadFileDialog";
|
||||
import { getModelsMetadata, getSelectedUuid } from "./storage";
|
||||
|
||||
export function DesktopMenu(props: {
|
||||
newModel: () => void;
|
||||
setModel: (key: string) => void;
|
||||
onDownload: () => void;
|
||||
onModelUpload: (blob: ArrayBuffer, fileName: string) => Promise<void>;
|
||||
onDelete: () => void;
|
||||
}) {
|
||||
const [isFileMenuOpen, setFileMenuOpen] = useState(false);
|
||||
const anchorElement = useRef<HTMLButtonElement>(
|
||||
null as unknown as HTMLButtonElement,
|
||||
);
|
||||
|
||||
return (
|
||||
<>
|
||||
<FileBarButton
|
||||
onClick={(): void => setFileMenuOpen(!isFileMenuOpen)}
|
||||
ref={anchorElement}
|
||||
disableRipple
|
||||
isOpen={isFileMenuOpen}
|
||||
>
|
||||
File
|
||||
</FileBarButton>
|
||||
<FileMenu
|
||||
newModel={props.newModel}
|
||||
setModel={props.setModel}
|
||||
onDownload={props.onDownload}
|
||||
onModelUpload={props.onModelUpload}
|
||||
onDelete={props.onDelete}
|
||||
isFileMenuOpen={isFileMenuOpen}
|
||||
setFileMenuOpen={setFileMenuOpen}
|
||||
setMobileMenuOpen={() => {}}
|
||||
anchorElement={anchorElement}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export function MobileMenu(props: {
|
||||
newModel: () => void;
|
||||
setModel: (key: string) => void;
|
||||
onDownload: () => void;
|
||||
onModelUpload: (blob: ArrayBuffer, fileName: string) => Promise<void>;
|
||||
onDelete: () => void;
|
||||
}) {
|
||||
const [isMobileMenuOpen, setMobileMenuOpen] = useState(false);
|
||||
const [isFileMenuOpen, setFileMenuOpen] = useState(false);
|
||||
const anchorElement = useRef<HTMLButtonElement>(
|
||||
null as unknown as HTMLButtonElement,
|
||||
);
|
||||
const [fileMenuAnchorEl, setFileMenuAnchorEl] = useState<HTMLElement | null>(
|
||||
null,
|
||||
);
|
||||
|
||||
return (
|
||||
<>
|
||||
<MenuButton
|
||||
onClick={(): void => setMobileMenuOpen(true)}
|
||||
ref={anchorElement}
|
||||
disableRipple
|
||||
>
|
||||
<EllipsisVertical />
|
||||
</MenuButton>
|
||||
<StyledMenu
|
||||
open={isMobileMenuOpen}
|
||||
onClose={(): void => setMobileMenuOpen(false)}
|
||||
anchorEl={anchorElement.current}
|
||||
>
|
||||
<MenuItemWrapper
|
||||
onClick={(event) => {
|
||||
setFileMenuOpen(true);
|
||||
setFileMenuAnchorEl(event.currentTarget);
|
||||
}}
|
||||
disableRipple
|
||||
>
|
||||
<MenuItemText>File</MenuItemText>
|
||||
<ChevronRight />
|
||||
</MenuItemWrapper>
|
||||
<MenuDivider />
|
||||
<MenuItemWrapper
|
||||
onClick={() => {
|
||||
window.open("https://docs.ironcalc.com", "_blank");
|
||||
setMobileMenuOpen(false);
|
||||
}}
|
||||
disableRipple
|
||||
>
|
||||
<MenuItemText>Help</MenuItemText>
|
||||
</MenuItemWrapper>
|
||||
</StyledMenu>
|
||||
<FileMenu
|
||||
newModel={props.newModel}
|
||||
setModel={props.setModel}
|
||||
onDownload={props.onDownload}
|
||||
onModelUpload={props.onModelUpload}
|
||||
onDelete={props.onDelete}
|
||||
isFileMenuOpen={isFileMenuOpen}
|
||||
setFileMenuOpen={setFileMenuOpen}
|
||||
setMobileMenuOpen={setMobileMenuOpen}
|
||||
anchorElement={anchorElement}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export function FileMenu(props: {
|
||||
newModel: () => void;
|
||||
setModel: (key: string) => void;
|
||||
onDownload: () => void;
|
||||
onModelUpload: (blob: ArrayBuffer, fileName: string) => Promise<void>;
|
||||
onDelete: () => void;
|
||||
isFileMenuOpen: boolean;
|
||||
setFileMenuOpen: (open: boolean) => void;
|
||||
setMobileMenuOpen: (open: boolean) => void;
|
||||
anchorElement: React.RefObject<HTMLButtonElement>;
|
||||
}) {
|
||||
const [isMenuOpen, setMenuOpen] = useState(false);
|
||||
const [isImportMenuOpen, setImportMenuOpen] = useState(false);
|
||||
const anchorElement = useRef<HTMLButtonElement>(null);
|
||||
const models = getModelsMetadata();
|
||||
const uuids = Object.keys(models);
|
||||
const selectedUuid = getSelectedUuid();
|
||||
const [isDeleteDialogOpen, setDeleteDialogOpen] = useState(false);
|
||||
|
||||
const elements = [];
|
||||
for (const uuid of uuids) {
|
||||
elements.push(
|
||||
<MenuItemWrapper
|
||||
key={uuid}
|
||||
onClick={() => {
|
||||
props.setModel(uuid);
|
||||
setMenuOpen(false);
|
||||
}}
|
||||
>
|
||||
<CheckIndicator>
|
||||
{uuid === selectedUuid ? (
|
||||
<StyledIcon>
|
||||
<Check />
|
||||
</StyledIcon>
|
||||
) : (
|
||||
""
|
||||
)}
|
||||
</CheckIndicator>
|
||||
<MenuItemText
|
||||
style={{
|
||||
maxWidth: "240px",
|
||||
overflow: "hidden",
|
||||
textOverflow: "ellipsis",
|
||||
}}
|
||||
>
|
||||
{models[uuid]}
|
||||
</MenuItemText>
|
||||
</MenuItemWrapper>,
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<FileMenuWrapper
|
||||
type="button"
|
||||
id="file-menu-button"
|
||||
onClick={(): void => setMenuOpen(true)}
|
||||
ref={anchorElement}
|
||||
$isActive={isMenuOpen}
|
||||
aria-haspopup="true"
|
||||
>
|
||||
File
|
||||
</FileMenuWrapper>
|
||||
<Menu
|
||||
open={isMenuOpen}
|
||||
onClose={(): void => setMenuOpen(false)}
|
||||
anchorEl={anchorElement.current}
|
||||
autoFocus={false}
|
||||
disableRestoreFocus={true}
|
||||
sx={{
|
||||
"& .MuiPaper-root": { borderRadius: "8px", padding: "4px 0px" },
|
||||
"& .MuiList-root": { padding: "0" },
|
||||
transform: "translate(-4px, 4px)",
|
||||
<StyledMenu
|
||||
open={props.isFileMenuOpen}
|
||||
onClose={(): void => props.setFileMenuOpen(false)}
|
||||
anchorEl={props.anchorElement.current}
|
||||
anchorOrigin={{
|
||||
vertical: "bottom",
|
||||
horizontal: "left",
|
||||
}}
|
||||
slotProps={{
|
||||
list: {
|
||||
"aria-labelledby": "file-menu-button",
|
||||
tabIndex: -1,
|
||||
},
|
||||
transformOrigin={{
|
||||
vertical: "top",
|
||||
horizontal: "left",
|
||||
}}
|
||||
// To prevent closing parent menu when interacting with submenu
|
||||
onMouseLeave={() => {
|
||||
if (!isImportMenuOpen && !isDeleteDialogOpen) {
|
||||
props.setFileMenuOpen(false);
|
||||
}
|
||||
}}
|
||||
>
|
||||
<MenuItemWrapper
|
||||
onClick={() => {
|
||||
props.newModel();
|
||||
setMenuOpen(false);
|
||||
props.setFileMenuOpen(false);
|
||||
props.setMobileMenuOpen(false);
|
||||
}}
|
||||
disableRipple
|
||||
>
|
||||
<StyledIcon>
|
||||
<Plus />
|
||||
@@ -97,34 +169,41 @@ export function FileMenu(props: {
|
||||
<MenuItemWrapper
|
||||
onClick={() => {
|
||||
setImportMenuOpen(true);
|
||||
setMenuOpen(false);
|
||||
props.setFileMenuOpen(false);
|
||||
props.setMobileMenuOpen(false);
|
||||
}}
|
||||
disableRipple
|
||||
>
|
||||
<StyledIcon>
|
||||
<FileUp />
|
||||
</StyledIcon>
|
||||
<MenuItemText>Import</MenuItemText>
|
||||
</MenuItemWrapper>
|
||||
<MenuItemWrapper onClick={props.onDownload}>
|
||||
<StyledIcon>
|
||||
<FileDown />
|
||||
</StyledIcon>
|
||||
<MenuItemWrapper
|
||||
onClick={() => {
|
||||
props.onDownload();
|
||||
props.setMobileMenuOpen(false);
|
||||
}}
|
||||
disableRipple
|
||||
>
|
||||
<StyledFileDown />
|
||||
<MenuItemText>Download (.xlsx)</MenuItemText>
|
||||
</MenuItemWrapper>
|
||||
<MenuDivider />
|
||||
<MenuItemWrapper
|
||||
onClick={() => {
|
||||
setDeleteDialogOpen(true);
|
||||
setMenuOpen(false);
|
||||
props.setFileMenuOpen(false);
|
||||
props.setMobileMenuOpen(false);
|
||||
}}
|
||||
disableRipple
|
||||
>
|
||||
<StyledIcon>
|
||||
<Trash2 />
|
||||
</StyledIcon>
|
||||
<MenuItemText>Delete workbook</MenuItemText>
|
||||
</MenuItemWrapper>
|
||||
<MenuDivider />
|
||||
{elements}
|
||||
</Menu>
|
||||
</StyledMenu>
|
||||
<Modal
|
||||
open={isImportMenuOpen}
|
||||
onClose={() => {
|
||||
@@ -149,7 +228,7 @@ export function FileMenu(props: {
|
||||
<DeleteWorkbookDialog
|
||||
onClose={() => setDeleteDialogOpen(false)}
|
||||
onConfirm={props.onDelete}
|
||||
workbookName={selectedUuid ? models[selectedUuid] : ""}
|
||||
workbookName={selectedUuid ? models[selectedUuid]?.name || "" : ""}
|
||||
/>
|
||||
</Modal>
|
||||
</>
|
||||
@@ -167,7 +246,55 @@ const StyledIcon = styled.div`
|
||||
}
|
||||
`;
|
||||
|
||||
const MenuDivider = styled.div`
|
||||
const MenuButton = styled(IconButton)`
|
||||
height: 32px;
|
||||
width: 32px;
|
||||
padding: 8px;
|
||||
border-radius: 4px;
|
||||
svg {
|
||||
stroke-width: 2px;
|
||||
stroke: #757575;
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
}
|
||||
&:hover {
|
||||
background-color: #f2f2f2;
|
||||
}
|
||||
&:active {
|
||||
background-color: #e0e0e0;
|
||||
}
|
||||
`;
|
||||
|
||||
const FileBarButton = styled(Button)<{ isOpen: boolean }>`
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
font-size: 12px;
|
||||
height: 32px;
|
||||
width: auto;
|
||||
padding: 4px 8px;
|
||||
font-weight: 400;
|
||||
min-width: 0px;
|
||||
text-transform: capitalize;
|
||||
color: #333333;
|
||||
background-color: ${({ isOpen }) => (isOpen ? "#f2f2f2" : "none")};
|
||||
&:hover {
|
||||
background-color: #f2f2f2;
|
||||
}
|
||||
&:active {
|
||||
background-color: #e0e0e0;
|
||||
}
|
||||
`;
|
||||
|
||||
const StyledFileDown = styled(FileDown)`
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
color: #333333;
|
||||
padding-right: 10px;
|
||||
`;
|
||||
|
||||
|
||||
const MenuDivider = styled("div")`
|
||||
width: 100%;
|
||||
margin: auto;
|
||||
margin-top: 4px;
|
||||
@@ -178,6 +305,7 @@ const MenuDivider = styled.div`
|
||||
const MenuItemText = styled.div`
|
||||
color: #000;
|
||||
font-size: 12px;
|
||||
flex-grow: 1;
|
||||
`;
|
||||
|
||||
const MenuItemWrapper = styled(MenuItem)`
|
||||
@@ -190,26 +318,19 @@ const MenuItemWrapper = styled(MenuItem)`
|
||||
border-radius: 4px;
|
||||
padding: 8px;
|
||||
height: 32px;
|
||||
`;
|
||||
|
||||
const FileMenuWrapper = styled.button<{ $isActive: boolean }>`
|
||||
display: flex;
|
||||
align-items: center;
|
||||
font-size: 12px;
|
||||
font-family: Inter;
|
||||
padding: 8px;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
background-color: ${(props) => (props.$isActive ? "#e6e6e6" : "transparent")};
|
||||
border: none;
|
||||
background: none;
|
||||
&:hover {
|
||||
background-color: #f2f2f2;
|
||||
min-height: 32px;
|
||||
svg {
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
}
|
||||
`;
|
||||
|
||||
const CheckIndicator = styled.span`
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
min-width: 26px;
|
||||
const StyledMenu = styled(Menu)`
|
||||
.MuiPaper-root {
|
||||
border-radius: 8px;
|
||||
padding: 4px 0px;
|
||||
},
|
||||
.MuiList-root {
|
||||
padding: 0;
|
||||
},
|
||||
`;
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
import styled from "@emotion/styled";
|
||||
import WorkbookList from "./WorkbookList";
|
||||
|
||||
interface DrawerContentProps {
|
||||
setModel: (key: string) => void;
|
||||
onDelete: (uuid: string) => void;
|
||||
}
|
||||
|
||||
function DrawerContent(props: DrawerContentProps) {
|
||||
const { setModel, onDelete } = props;
|
||||
|
||||
return (
|
||||
<ContentContainer>
|
||||
<WorkbookList setModel={setModel} onDelete={onDelete} />
|
||||
</ContentContainer>
|
||||
);
|
||||
}
|
||||
|
||||
const ContentContainer = styled("div")`
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 4px;
|
||||
padding: 16px 12px;
|
||||
height: 100%;
|
||||
overflow: scroll;
|
||||
font-size: 12px;
|
||||
`;
|
||||
|
||||
export default DrawerContent;
|
||||
@@ -0,0 +1,71 @@
|
||||
import styled from "@emotion/styled";
|
||||
import { BookOpen } from "lucide-react";
|
||||
|
||||
function DrawerFooter() {
|
||||
return (
|
||||
<StyledDrawerFooter>
|
||||
<FooterLink
|
||||
href="https://docs.ironcalc.com/"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
<OpenBookIcon>
|
||||
<BookOpen />
|
||||
</OpenBookIcon>
|
||||
<FooterLinkText>Documentation</FooterLinkText>
|
||||
</FooterLink>
|
||||
</StyledDrawerFooter>
|
||||
);
|
||||
}
|
||||
|
||||
const StyledDrawerFooter = styled("div")`
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding: 12px;
|
||||
justify-content: space-between;
|
||||
max-height: 60px;
|
||||
height: 60px;
|
||||
border-top: 1px solid #e0e0e0;
|
||||
box-sizing: border-box;
|
||||
`;
|
||||
|
||||
const FooterLink = styled("a")`
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
justify-content: flex-start;
|
||||
font-size: 14px;
|
||||
width: 100%;
|
||||
min-width: 172px;
|
||||
border-radius: 8px;
|
||||
padding: 8px 4px 8px 8px;
|
||||
transition: gap 0.5s;
|
||||
background-color: transparent;
|
||||
color: #000;
|
||||
text-decoration: none;
|
||||
align-items: center;
|
||||
|
||||
&:hover {
|
||||
background-color: #e0e0e0 !important;
|
||||
}
|
||||
`;
|
||||
|
||||
const OpenBookIcon = styled("div")`
|
||||
height: 16px;
|
||||
width: 16px;
|
||||
svg {
|
||||
height: 16px;
|
||||
width: 16px;
|
||||
stroke: #9e9e9e;
|
||||
}
|
||||
`;
|
||||
|
||||
const FooterLinkText = styled("div")`
|
||||
color: #000;
|
||||
font-size: 12px;
|
||||
width: 100%;
|
||||
max-width: 240px;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
`;
|
||||
|
||||
export default DrawerFooter;
|
||||
@@ -0,0 +1,61 @@
|
||||
import styled from "@emotion/styled";
|
||||
import { IronCalcLogo } from "@ironcalc/workbook";
|
||||
import { IconButton } from "@mui/material";
|
||||
import { Plus } from "lucide-react";
|
||||
|
||||
interface DrawerHeaderProps {
|
||||
onNewModel: () => void;
|
||||
}
|
||||
|
||||
function DrawerHeader({ onNewModel }: DrawerHeaderProps) {
|
||||
return (
|
||||
<HeaderContainer>
|
||||
<StyledDesktopLogo />
|
||||
<AddButton onClick={onNewModel} title="New workbook">
|
||||
<PlusIcon />
|
||||
</AddButton>
|
||||
</HeaderContainer>
|
||||
);
|
||||
}
|
||||
|
||||
const HeaderContainer = styled("div")`
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding: 12px 8px 12px 16px;
|
||||
justify-content: space-between;
|
||||
max-height: 60px;
|
||||
min-height: 60px;
|
||||
border-bottom: 1px solid #e0e0e0;
|
||||
box-sizing: border-box;
|
||||
`;
|
||||
|
||||
const StyledDesktopLogo = styled(IronCalcLogo)`
|
||||
width: 120px;
|
||||
height: 28px;
|
||||
`;
|
||||
|
||||
const AddButton = styled(IconButton)`
|
||||
background: none;
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 8px;
|
||||
height: 32px;
|
||||
width: 32px;
|
||||
border-radius: 4px;
|
||||
margin-left: 10px;
|
||||
color: #333333;
|
||||
stroke-width: 2px;
|
||||
&:hover {
|
||||
background-color: #e0e0e0;
|
||||
}
|
||||
`;
|
||||
|
||||
const PlusIcon = styled(Plus)`
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
`;
|
||||
|
||||
export default DrawerHeader;
|
||||
@@ -0,0 +1,51 @@
|
||||
import styled from "@emotion/styled";
|
||||
import { Drawer } from "@mui/material";
|
||||
import DrawerContent from "./DrawerContent";
|
||||
import DrawerFooter from "./DrawerFooter";
|
||||
import DrawerHeader from "./DrawerHeader";
|
||||
|
||||
interface LeftDrawerProps {
|
||||
open: boolean;
|
||||
onClose: () => void;
|
||||
newModel: () => void;
|
||||
setModel: (key: string) => void;
|
||||
onDelete: (uuid: string) => void;
|
||||
}
|
||||
|
||||
function LeftDrawer({
|
||||
open,
|
||||
onClose,
|
||||
newModel,
|
||||
setModel,
|
||||
onDelete,
|
||||
}: LeftDrawerProps) {
|
||||
return (
|
||||
<DrawerWrapper
|
||||
variant="persistent"
|
||||
anchor="left"
|
||||
open={open}
|
||||
onClose={onClose}
|
||||
>
|
||||
<DrawerHeader onNewModel={newModel} />
|
||||
<DrawerContent setModel={setModel} onDelete={onDelete} />
|
||||
|
||||
<DrawerFooter />
|
||||
</DrawerWrapper>
|
||||
);
|
||||
}
|
||||
|
||||
const DrawerWrapper = styled(Drawer)`
|
||||
width: 264px;
|
||||
height: 100%;
|
||||
flex-shrink: 0;
|
||||
font-family: "Inter", sans-serif;
|
||||
|
||||
.MuiDrawer-paper {
|
||||
width: 264px;
|
||||
background-color: #f5f5f5;
|
||||
overflow: hidden;
|
||||
border-right: 1px solid #e0e0e0;
|
||||
}
|
||||
`;
|
||||
|
||||
export default LeftDrawer;
|
||||
@@ -0,0 +1,379 @@
|
||||
import styled from "@emotion/styled";
|
||||
import { Menu, MenuItem, Modal } from "@mui/material";
|
||||
import {
|
||||
EllipsisVertical,
|
||||
FileDown,
|
||||
FileSpreadsheet,
|
||||
Trash2,
|
||||
} from "lucide-react";
|
||||
import type React from "react";
|
||||
import { useEffect, useState } from "react";
|
||||
import DeleteWorkbookDialog from "../DeleteWorkbookDialog";
|
||||
import { downloadModel } from "../rpc";
|
||||
import {
|
||||
getModelsMetadata,
|
||||
getSelectedUuid,
|
||||
selectModelFromStorage,
|
||||
} from "../storage";
|
||||
|
||||
interface WorkbookListProps {
|
||||
setModel: (key: string) => void;
|
||||
onDelete: (uuid: string) => void;
|
||||
}
|
||||
|
||||
function WorkbookList({ setModel, onDelete }: WorkbookListProps) {
|
||||
const [menuAnchorEl, setMenuAnchorEl] = useState<null | HTMLElement>(null);
|
||||
const [selectedWorkbookUuid, setSelectedWorkbookUuid] = useState<
|
||||
string | null
|
||||
>(null);
|
||||
const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false);
|
||||
const [workbookToDelete, setWorkbookToDelete] = useState<string | null>(null);
|
||||
const [intendedSelection, setIntendedSelection] = useState<string | null>(
|
||||
null,
|
||||
);
|
||||
|
||||
const selectedUuid = getSelectedUuid();
|
||||
|
||||
// Clear intended selection when selectedUuid changes from outside
|
||||
useEffect(() => {
|
||||
if (intendedSelection && selectedUuid === intendedSelection) {
|
||||
setIntendedSelection(null);
|
||||
}
|
||||
}, [selectedUuid, intendedSelection]);
|
||||
|
||||
const handleMenuOpen = (
|
||||
event: React.MouseEvent<HTMLButtonElement>,
|
||||
uuid: string,
|
||||
) => {
|
||||
console.log("Menu open", uuid);
|
||||
event.stopPropagation();
|
||||
setSelectedWorkbookUuid(uuid);
|
||||
setMenuAnchorEl(event.currentTarget);
|
||||
setIntendedSelection(uuid);
|
||||
setModel(uuid);
|
||||
};
|
||||
|
||||
const handleMenuClose = () => {
|
||||
console.log(
|
||||
"Menu closing, selectedWorkbookUuid:",
|
||||
selectedWorkbookUuid,
|
||||
"intendedSelection:",
|
||||
intendedSelection,
|
||||
);
|
||||
setMenuAnchorEl(null);
|
||||
// If we have an intended selection, make sure it's still selected
|
||||
if (intendedSelection && intendedSelection !== selectedUuid) {
|
||||
console.log("Re-selecting intended workbook:", intendedSelection);
|
||||
setModel(intendedSelection);
|
||||
}
|
||||
// Don't reset selectedWorkbookUuid here - we want to keep track of which workbook was selected
|
||||
// The selectedWorkbookUuid will be used for download/delete operations
|
||||
};
|
||||
|
||||
const handleDeleteClick = (uuid: string) => {
|
||||
console.log("Delete workbook:", uuid);
|
||||
setWorkbookToDelete(uuid);
|
||||
setIsDeleteDialogOpen(true);
|
||||
setIntendedSelection(null);
|
||||
handleMenuClose();
|
||||
};
|
||||
|
||||
const handleDeleteConfirm = () => {
|
||||
if (workbookToDelete) {
|
||||
onDelete(workbookToDelete);
|
||||
setWorkbookToDelete(null);
|
||||
}
|
||||
setIsDeleteDialogOpen(false);
|
||||
};
|
||||
|
||||
const handleDeleteCancel = () => {
|
||||
setWorkbookToDelete(null);
|
||||
setIsDeleteDialogOpen(false);
|
||||
};
|
||||
|
||||
const handleDownload = async (uuid: string) => {
|
||||
try {
|
||||
const model = selectModelFromStorage(uuid);
|
||||
if (model) {
|
||||
const bytes = model.toBytes();
|
||||
const fileName = model.getName();
|
||||
await downloadModel(bytes, fileName);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to download workbook:", error);
|
||||
}
|
||||
};
|
||||
|
||||
// Group workbooks by creation date
|
||||
const groupWorkbooks = () => {
|
||||
const now = Date.now();
|
||||
const millisecondsInDay = 24 * 60 * 60 * 1000;
|
||||
const millisecondsIn30Days = 30 * millisecondsInDay;
|
||||
|
||||
const modelsCreatedToday = [];
|
||||
const modelsCreatedThisMonth = [];
|
||||
const olderModels = [];
|
||||
const modelsMetadata = getModelsMetadata();
|
||||
|
||||
for (const uuid in modelsMetadata) {
|
||||
const createdAt = modelsMetadata[uuid].createdAt;
|
||||
const age = now - createdAt;
|
||||
|
||||
if (age < millisecondsInDay) {
|
||||
modelsCreatedToday.push(uuid);
|
||||
} else if (age < millisecondsIn30Days) {
|
||||
modelsCreatedThisMonth.push(uuid);
|
||||
} else {
|
||||
olderModels.push(uuid);
|
||||
}
|
||||
}
|
||||
|
||||
// Sort each group by creation timestamp (newest first)
|
||||
const sortByNewest = (uuids: string[]) =>
|
||||
uuids.sort(
|
||||
(a, b) => modelsMetadata[b].createdAt - modelsMetadata[a].createdAt,
|
||||
);
|
||||
|
||||
return {
|
||||
modelsCreatedToday: sortByNewest(modelsCreatedToday),
|
||||
modelsCreatedThisMonth: sortByNewest(modelsCreatedThisMonth),
|
||||
olderModels: sortByNewest(olderModels),
|
||||
};
|
||||
};
|
||||
|
||||
const { modelsCreatedToday, modelsCreatedThisMonth, olderModels } =
|
||||
groupWorkbooks();
|
||||
|
||||
const renderWorkbookItem = (uuid: string) => {
|
||||
const isMenuOpen = menuAnchorEl !== null && selectedWorkbookUuid === uuid;
|
||||
const isAnyMenuOpen = menuAnchorEl !== null;
|
||||
const models = getModelsMetadata();
|
||||
return (
|
||||
<WorkbookListItem
|
||||
key={uuid}
|
||||
onClick={() => {
|
||||
// Prevent clicking on list items when any menu is open
|
||||
if (isAnyMenuOpen) {
|
||||
return;
|
||||
}
|
||||
setModel(uuid);
|
||||
}}
|
||||
selected={uuid === selectedUuid}
|
||||
disableRipple
|
||||
style={{ pointerEvents: isAnyMenuOpen ? "none" : "auto" }}
|
||||
>
|
||||
<StorageIndicator>
|
||||
<FileSpreadsheet />
|
||||
</StorageIndicator>
|
||||
<WorkbookListText>{models[uuid].name}</WorkbookListText>
|
||||
<EllipsisButton
|
||||
onClick={(e) => handleMenuOpen(e, uuid)}
|
||||
isOpen={isMenuOpen}
|
||||
onMouseDown={(e) => e.stopPropagation()}
|
||||
style={{ pointerEvents: "auto" }}
|
||||
>
|
||||
<EllipsisVertical />
|
||||
</EllipsisButton>
|
||||
</WorkbookListItem>
|
||||
);
|
||||
};
|
||||
|
||||
const renderSection = (title: string, uuids: string[]) => {
|
||||
if (uuids.length === 0) return null;
|
||||
|
||||
return (
|
||||
<SectionContainer key={title}>
|
||||
<SectionTitle>{title}</SectionTitle>
|
||||
{uuids.map(renderWorkbookItem)}
|
||||
</SectionContainer>
|
||||
);
|
||||
};
|
||||
|
||||
const models = getModelsMetadata();
|
||||
|
||||
return (
|
||||
<>
|
||||
{renderSection("Today", modelsCreatedToday)}
|
||||
{renderSection("Last 30 Days", modelsCreatedThisMonth)}
|
||||
{renderSection("Older", olderModels)}
|
||||
|
||||
<StyledMenu
|
||||
anchorEl={menuAnchorEl}
|
||||
open={Boolean(menuAnchorEl)}
|
||||
onClose={handleMenuClose}
|
||||
MenuListProps={{
|
||||
dense: true,
|
||||
}}
|
||||
anchorOrigin={{
|
||||
vertical: "bottom",
|
||||
horizontal: "right",
|
||||
}}
|
||||
transformOrigin={{
|
||||
vertical: "top",
|
||||
horizontal: "right",
|
||||
}}
|
||||
>
|
||||
<MenuItemWrapper
|
||||
onClick={() => {
|
||||
console.log(
|
||||
"Download clicked, selectedWorkbookUuid:",
|
||||
selectedWorkbookUuid,
|
||||
);
|
||||
if (selectedWorkbookUuid) {
|
||||
handleDownload(selectedWorkbookUuid);
|
||||
}
|
||||
setIntendedSelection(null);
|
||||
handleMenuClose();
|
||||
}}
|
||||
disableRipple
|
||||
>
|
||||
<FileDown />
|
||||
Download (.xlsx)
|
||||
</MenuItemWrapper>
|
||||
<MenuDivider />
|
||||
<MenuItemWrapper
|
||||
selected={false}
|
||||
onClick={() => {
|
||||
if (selectedWorkbookUuid) {
|
||||
handleDeleteClick(selectedWorkbookUuid);
|
||||
}
|
||||
}}
|
||||
disableRipple
|
||||
>
|
||||
<Trash2 size={16} />
|
||||
Delete workbook
|
||||
</MenuItemWrapper>
|
||||
</StyledMenu>
|
||||
|
||||
<Modal
|
||||
open={isDeleteDialogOpen}
|
||||
onClose={handleDeleteCancel}
|
||||
aria-labelledby="delete-dialog-title"
|
||||
aria-describedby="delete-dialog-description"
|
||||
>
|
||||
<DeleteWorkbookDialog
|
||||
onClose={handleDeleteCancel}
|
||||
onConfirm={handleDeleteConfirm}
|
||||
workbookName={workbookToDelete ? models[workbookToDelete].name : ""}
|
||||
/>
|
||||
</Modal>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
const StorageIndicator = styled("div")`
|
||||
height: 16px;
|
||||
width: 16px;
|
||||
svg {
|
||||
height: 16px;
|
||||
width: 16px;
|
||||
stroke: #9e9e9e;
|
||||
}
|
||||
`;
|
||||
|
||||
const EllipsisButton = styled("button")<{ isOpen: boolean }>`
|
||||
background: none;
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 4px;
|
||||
height: 24px;
|
||||
width: 24px;
|
||||
border-radius: 4px;
|
||||
color: #333333;
|
||||
stroke-width: 2px;
|
||||
background-color: ${({ isOpen }) => (isOpen ? "#E0E0E0" : "none")};
|
||||
opacity: ${({ isOpen }) => (isOpen ? "1" : "0.5")};
|
||||
transition: opacity 0.3s, background-color 0.3s;
|
||||
&:hover {
|
||||
background: none;
|
||||
opacity: 1;
|
||||
}
|
||||
&:active {
|
||||
background: #bdbdbd;
|
||||
opacity: 1;
|
||||
}
|
||||
`;
|
||||
|
||||
const WorkbookListItem = styled(MenuItem)<{ selected: boolean }>`
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
justify-content: flex-start;
|
||||
font-size: 14px;
|
||||
width: 100%;
|
||||
min-width: 172px;
|
||||
border-radius: 8px;
|
||||
padding: 8px 4px 8px 8px;
|
||||
height: 32px;
|
||||
min-height: 32px;
|
||||
transition: gap 0.5s;
|
||||
background-color: ${({ selected }) =>
|
||||
selected ? "#e0e0e0 !important" : "transparent"};
|
||||
|
||||
/* Prevent hover effects when menu is open */
|
||||
&:hover {
|
||||
background-color: ${({ selected }) =>
|
||||
selected ? "#e0e0e0 !important" : "transparent"};
|
||||
}
|
||||
`;
|
||||
|
||||
const WorkbookListText = styled("div")`
|
||||
color: #000;
|
||||
font-size: 12px;
|
||||
width: 100%;
|
||||
max-width: 240px;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
`;
|
||||
|
||||
const StyledMenu = styled(Menu)`
|
||||
.MuiPaper-root {
|
||||
border-radius: 8px;
|
||||
padding: 4px 0px;
|
||||
box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.01);
|
||||
},
|
||||
.MuiList-root {
|
||||
padding: 0;
|
||||
},
|
||||
`;
|
||||
|
||||
const MenuDivider = styled("div")`
|
||||
width: 100%;
|
||||
margin: auto;
|
||||
margin-top: 4px;
|
||||
margin-bottom: 4px;
|
||||
border-top: 1px solid #eeeeee;
|
||||
`;
|
||||
|
||||
const MenuItemWrapper = styled(MenuItem)`
|
||||
display: flex;
|
||||
justify-content: flex-start;
|
||||
font-size: 12px;
|
||||
width: calc(100% - 8px);
|
||||
min-width: 140px;
|
||||
margin: 0px 4px;
|
||||
border-radius: 4px;
|
||||
padding: 8px;
|
||||
height: 32px;
|
||||
gap: 8px;
|
||||
svg {
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
}
|
||||
`;
|
||||
|
||||
const SectionContainer = styled("div")`
|
||||
margin-bottom: 16px;
|
||||
`;
|
||||
|
||||
const SectionTitle = styled("div")`
|
||||
font-weight: 600;
|
||||
color: #9e9e9e;
|
||||
margin-bottom: 8px;
|
||||
padding: 0px 8px;
|
||||
font-size: 12px;
|
||||
`;
|
||||
|
||||
export default WorkbookList;
|
||||
@@ -72,10 +72,10 @@ export function WorkbookTitle(properties: {
|
||||
}
|
||||
|
||||
const Container = styled("div")`
|
||||
text-align: center;
|
||||
padding: 8px;
|
||||
text-align: left;
|
||||
padding: 6px 4px;
|
||||
font-size: 14px;
|
||||
font-weight: 700;
|
||||
font-weight: 600;
|
||||
font-family: Inter;
|
||||
`;
|
||||
|
||||
@@ -108,7 +108,7 @@ const TitleInput = styled("input")`
|
||||
background-color: #f2f2f2;
|
||||
}
|
||||
&:focus {
|
||||
border: 1px solid grey;
|
||||
outline: 1px solid grey;
|
||||
}
|
||||
font-weight: inherit;
|
||||
font-family: inherit;
|
||||
|
||||
@@ -3,7 +3,7 @@ import { base64ToBytes, bytesToBase64 } from "./util";
|
||||
|
||||
const MAX_WORKBOOKS = 50;
|
||||
|
||||
type ModelsMetadata = Record<string, string>;
|
||||
type ModelsMetadata = Record<string, { name: string; createdAt: number }>;
|
||||
|
||||
export function updateNameSelectedWorkbook(model: Model, newName: string) {
|
||||
const uuid = localStorage.getItem("selected");
|
||||
@@ -12,7 +12,11 @@ export function updateNameSelectedWorkbook(model: Model, newName: string) {
|
||||
if (modelsJson) {
|
||||
try {
|
||||
const models = JSON.parse(modelsJson);
|
||||
models[uuid] = newName;
|
||||
if (models[uuid]) {
|
||||
models[uuid].name = newName;
|
||||
} else {
|
||||
models[uuid] = { name: newName, createdAt: Date.now() };
|
||||
}
|
||||
localStorage.setItem("models", JSON.stringify(models));
|
||||
} catch (e) {
|
||||
console.warn("Failed saving new name");
|
||||
@@ -28,7 +32,26 @@ export function getModelsMetadata(): ModelsMetadata {
|
||||
if (!modelsJson) {
|
||||
modelsJson = "{}";
|
||||
}
|
||||
return JSON.parse(modelsJson);
|
||||
const models = JSON.parse(modelsJson);
|
||||
|
||||
// Migrate old format to new format
|
||||
const migratedModels: ModelsMetadata = {};
|
||||
for (const [uuid, value] of Object.entries(models)) {
|
||||
if (typeof value === "string") {
|
||||
// Old format: just the name string
|
||||
migratedModels[uuid] = { name: value, createdAt: Date.now() };
|
||||
} else if (typeof value === "object" && value !== null && "name" in value) {
|
||||
// New format: object with name and createdAt
|
||||
migratedModels[uuid] = value as { name: string; createdAt: number };
|
||||
}
|
||||
}
|
||||
|
||||
// Save migrated data back to localStorage
|
||||
if (JSON.stringify(models) !== JSON.stringify(migratedModels)) {
|
||||
localStorage.setItem("models", JSON.stringify(migratedModels));
|
||||
}
|
||||
|
||||
return migratedModels;
|
||||
}
|
||||
|
||||
// Pick a different name Workbook{N} where N = 1, 2, 3
|
||||
@@ -48,14 +71,14 @@ function getNewName(existingNames: string[]): string {
|
||||
|
||||
export function createNewModel(): Model {
|
||||
const models = getModelsMetadata();
|
||||
const name = getNewName(Object.values(models));
|
||||
const name = getNewName(Object.values(models).map((m) => m.name));
|
||||
|
||||
const model = new Model(name, "en", "UTC");
|
||||
const uuid = crypto.randomUUID();
|
||||
localStorage.setItem("selected", uuid);
|
||||
localStorage.setItem(uuid, bytesToBase64(model.toBytes()));
|
||||
|
||||
models[uuid] = name;
|
||||
models[uuid] = { name, createdAt: Date.now() };
|
||||
localStorage.setItem("models", JSON.stringify(models));
|
||||
return model;
|
||||
}
|
||||
@@ -95,7 +118,7 @@ export function saveModelToStorage(model: Model) {
|
||||
modelsJson = "{}";
|
||||
}
|
||||
const models = JSON.parse(modelsJson);
|
||||
models[uuid] = model.getName();
|
||||
models[uuid] = { name: model.getName(), createdAt: Date.now() };
|
||||
localStorage.setItem("models", JSON.stringify(models));
|
||||
}
|
||||
|
||||
@@ -127,3 +150,37 @@ export function deleteSelectedModel(): Model | null {
|
||||
}
|
||||
return selectModelFromStorage(uuids[0]);
|
||||
}
|
||||
|
||||
export function deleteModelByUuid(uuid: string): Model | null {
|
||||
localStorage.removeItem(uuid);
|
||||
const metadata = getModelsMetadata();
|
||||
delete metadata[uuid];
|
||||
localStorage.setItem("models", JSON.stringify(metadata));
|
||||
|
||||
// If this was the selected model, we need to select a different one
|
||||
const selectedUuid = localStorage.getItem("selected");
|
||||
if (selectedUuid === uuid) {
|
||||
const uuids = Object.keys(metadata);
|
||||
if (uuids.length === 0) {
|
||||
return createNewModel();
|
||||
}
|
||||
// Find the newest workbook by creation timestamp
|
||||
const newestUuid = uuids.reduce((newest, current) => {
|
||||
const newestTime = metadata[newest]?.createdAt || 0;
|
||||
const currentTime = metadata[current]?.createdAt || 0;
|
||||
return currentTime > newestTime ? current : newest;
|
||||
});
|
||||
return selectModelFromStorage(newestUuid);
|
||||
}
|
||||
|
||||
// If it wasn't the selected model, return the currently selected model
|
||||
if (selectedUuid) {
|
||||
const modelBytesString = localStorage.getItem(selectedUuid);
|
||||
if (modelBytesString) {
|
||||
return Model.from_bytes(base64ToBytes(modelBytesString));
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to creating a new model if no valid selected model
|
||||
return createNewModel();
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
//! This is primary for QA internal testing and will be superseded by an official
|
||||
//! IronCalc CLI.
|
||||
//!
|
||||
//! Usage: test file.xlsx
|
||||
//! Usage: test file.xlsx [output.icalc]
|
||||
|
||||
use std::path;
|
||||
|
||||
@@ -15,15 +15,20 @@ use ironcalc::{export::save_to_icalc, import::load_from_xlsx};
|
||||
|
||||
fn main() {
|
||||
let args: Vec<_> = std::env::args().collect();
|
||||
if args.len() != 2 {
|
||||
panic!("Usage: {} <file.xlsx>", args[0]);
|
||||
if args.len() != 2 && args.len() != 3 {
|
||||
panic!("Usage: {} <file.xlsx> [output.icalc]", args[0]);
|
||||
}
|
||||
// first test the file
|
||||
let file_name = &args[1];
|
||||
|
||||
let file_path = path::Path::new(file_name);
|
||||
let base_name = file_path.file_stem().unwrap().to_str().unwrap();
|
||||
let output_file_name = &format!("{base_name}.ic");
|
||||
let output_file_name = if args.len() == 3 {
|
||||
&args[2]
|
||||
} else {
|
||||
let file_path = path::Path::new(file_name);
|
||||
let base_name = file_path.file_stem().unwrap().to_str().unwrap();
|
||||
&format!("{base_name}.ic")
|
||||
};
|
||||
|
||||
let model = load_from_xlsx(file_name, "en", "UTC").unwrap();
|
||||
save_to_icalc(&model, output_file_name).unwrap();
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ use itertools::Itertools;
|
||||
|
||||
use ironcalc_base::{
|
||||
expressions::{
|
||||
parser::{static_analysis::StaticResult, stringify::to_excel_string, Node},
|
||||
parser::{stringify::to_excel_string, Node},
|
||||
types::CellReferenceRC,
|
||||
utils::number_to_column,
|
||||
},
|
||||
@@ -56,7 +56,7 @@ fn get_formula_attribute(
|
||||
|
||||
pub(crate) fn get_worksheet_xml(
|
||||
worksheet: &Worksheet,
|
||||
parsed_formulas: &[(Node, StaticResult)],
|
||||
parsed_formulas: &[Node],
|
||||
dimension: &str,
|
||||
is_sheet_selected: bool,
|
||||
) -> String {
|
||||
@@ -104,7 +104,7 @@ pub(crate) fn get_worksheet_xml(
|
||||
let style = get_cell_style_attribute(*s);
|
||||
row_data_str.push(format!("<c r=\"{cell_name}\"{style}/>"));
|
||||
}
|
||||
Cell::SpillBooleanCell { v, s, .. } | Cell::BooleanCell { v, s } => {
|
||||
Cell::BooleanCell { v, s } => {
|
||||
// <c r="A8" t="b" s="1">
|
||||
// <v>1</v>
|
||||
// </c>
|
||||
@@ -114,7 +114,7 @@ pub(crate) fn get_worksheet_xml(
|
||||
"<c r=\"{cell_name}\" t=\"b\"{style}><v>{b}</v></c>"
|
||||
));
|
||||
}
|
||||
Cell::SpillNumberCell { v, s, .. } | Cell::NumberCell { v, s } => {
|
||||
Cell::NumberCell { v, s } => {
|
||||
// Normally the type number is left out. Example:
|
||||
// <c r="C6" s="1">
|
||||
// <v>3</v>
|
||||
@@ -122,7 +122,7 @@ pub(crate) fn get_worksheet_xml(
|
||||
let style = get_cell_style_attribute(*s);
|
||||
row_data_str.push(format!("<c r=\"{cell_name}\"{style}><v>{v}</v></c>"));
|
||||
}
|
||||
Cell::SpillErrorCell { ei, s, .. } | Cell::ErrorCell { ei, s } => {
|
||||
Cell::ErrorCell { ei, s } => {
|
||||
let style = get_cell_style_attribute(*s);
|
||||
row_data_str.push(format!(
|
||||
"<c r=\"{cell_name}\" t=\"e\"{style}><v>{ei}</v></c>"
|
||||
@@ -153,7 +153,7 @@ pub(crate) fn get_worksheet_xml(
|
||||
worksheet.get_name(),
|
||||
*row_index,
|
||||
*column_index,
|
||||
&parsed_formulas[*f as usize].0,
|
||||
&parsed_formulas[*f as usize],
|
||||
);
|
||||
|
||||
let b = i32::from(*v);
|
||||
@@ -172,7 +172,7 @@ pub(crate) fn get_worksheet_xml(
|
||||
worksheet.get_name(),
|
||||
*row_index,
|
||||
*column_index,
|
||||
&parsed_formulas[*f as usize].0,
|
||||
&parsed_formulas[*f as usize],
|
||||
);
|
||||
let style = get_cell_style_attribute(*s);
|
||||
|
||||
@@ -189,14 +189,14 @@ pub(crate) fn get_worksheet_xml(
|
||||
worksheet.get_name(),
|
||||
*row_index,
|
||||
*column_index,
|
||||
&parsed_formulas[*f as usize].0,
|
||||
&parsed_formulas[*f as usize],
|
||||
);
|
||||
let style = get_cell_style_attribute(*s);
|
||||
let escaped_v = escape_xml(v);
|
||||
|
||||
row_data_str.push(format!(
|
||||
"<c r=\"{cell_name}\" t=\"str\"{style}><f>{formula}</f><v>{escaped_v}</v></c>"
|
||||
));
|
||||
"<c r=\"{cell_name}\" t=\"str\"{style}><f>{formula}</f><v>{escaped_v}</v></c>"
|
||||
));
|
||||
}
|
||||
Cell::CellFormulaError {
|
||||
f,
|
||||
@@ -213,135 +213,13 @@ pub(crate) fn get_worksheet_xml(
|
||||
worksheet.get_name(),
|
||||
*row_index,
|
||||
*column_index,
|
||||
&parsed_formulas[*f as usize].0,
|
||||
&parsed_formulas[*f as usize],
|
||||
);
|
||||
let style = get_cell_style_attribute(*s);
|
||||
row_data_str.push(format!(
|
||||
"<c r=\"{cell_name}\" t=\"e\"{style}><f>{formula}</f><v>{ei}</v></c>"
|
||||
));
|
||||
}
|
||||
Cell::SpillStringCell { v, s, .. } => {
|
||||
// inline string
|
||||
// <c r="A1" t="str">
|
||||
let style = get_cell_style_attribute(*s);
|
||||
let escaped_v = escape_xml(v);
|
||||
row_data_str.push(format!(
|
||||
"<c r=\"{cell_name}\" t=\"str\"{style}><v>{escaped_v}</v></c>"
|
||||
));
|
||||
}
|
||||
Cell::DynamicCellFormula { .. } => {
|
||||
panic!("Model needs to be evaluated before saving!");
|
||||
}
|
||||
Cell::DynamicCellFormulaBoolean { f, v, s, r, a: _ } => {
|
||||
// <c r="A1" s="3" cm="1">
|
||||
// <f t="array" ref="A1:A10">A1:A10</f>
|
||||
// <v>1</v>
|
||||
// </c>
|
||||
let style = get_cell_style_attribute(*s);
|
||||
let range = format!(
|
||||
"{}{}:{}{}",
|
||||
column_name,
|
||||
row_index,
|
||||
number_to_column(r.0 + column_index).unwrap(),
|
||||
r.1 + row_index
|
||||
);
|
||||
|
||||
let formula = get_formula_attribute(
|
||||
worksheet.get_name(),
|
||||
*row_index,
|
||||
*column_index,
|
||||
&parsed_formulas[*f as usize].0,
|
||||
);
|
||||
|
||||
let b = i32::from(*v);
|
||||
row_data_str.push(format!(
|
||||
r#"<c r="{cell_name}" t="b" s="{style}" cm="1"><f t="array" ref="{range}">{formula}</f><v>{b}</v></c>"#
|
||||
));
|
||||
}
|
||||
Cell::DynamicCellFormulaNumber { f, v, s, r, a: _ } => {
|
||||
// <c r="C4" s="3" cm="1">
|
||||
// <f t="array" ref="C4:C10">C4:C10</f>
|
||||
// <v>123</v>
|
||||
// </c>
|
||||
let style = get_cell_style_attribute(*s);
|
||||
let range = format!(
|
||||
"{}{}:{}{}",
|
||||
column_name,
|
||||
row_index,
|
||||
number_to_column(r.0 + column_index).unwrap(),
|
||||
r.1 + row_index
|
||||
);
|
||||
|
||||
let formula = get_formula_attribute(
|
||||
worksheet.get_name(),
|
||||
*row_index,
|
||||
*column_index,
|
||||
&parsed_formulas[*f as usize].0,
|
||||
);
|
||||
|
||||
row_data_str.push(format!(
|
||||
r#"<c r="{cell_name}" s="{style}" cm="1"><f t="array" ref="{range}">{formula}</f><v>{v}</v></c>"#
|
||||
));
|
||||
}
|
||||
Cell::DynamicCellFormulaString { f, v, s, r, a: _ } => {
|
||||
// <c r="C6" t="str" s="5" cm="1">
|
||||
// <f t="array" ref="C6:C10">C6:C10</f>
|
||||
// <v>Hello world!</v>
|
||||
// </c>
|
||||
let style = get_cell_style_attribute(*s);
|
||||
let range = format!(
|
||||
"{}{}:{}{}",
|
||||
column_name,
|
||||
row_index,
|
||||
number_to_column(r.0 + column_index).unwrap(),
|
||||
r.1 + row_index
|
||||
);
|
||||
|
||||
let formula = get_formula_attribute(
|
||||
worksheet.get_name(),
|
||||
*row_index,
|
||||
*column_index,
|
||||
&parsed_formulas[*f as usize].0,
|
||||
);
|
||||
let escaped_v = escape_xml(v);
|
||||
|
||||
row_data_str.push(format!(
|
||||
r#"<c r="{cell_name}" t="str" s="{style}" cm="1"><f t="array" ref="{range}">{formula}</f><v>{escaped_v}</v></c>"#
|
||||
));
|
||||
}
|
||||
Cell::DynamicCellFormulaError {
|
||||
f,
|
||||
ei,
|
||||
s,
|
||||
o: _,
|
||||
m: _,
|
||||
r,
|
||||
a: _,
|
||||
} => {
|
||||
// <c r="C6" t="e" s="4" cm="1">
|
||||
// <f t="array" ref="C6:C10">C6:C10</f>
|
||||
// <v>#DIV/0!</v>
|
||||
// </c>
|
||||
let style = get_cell_style_attribute(*s);
|
||||
let range = format!(
|
||||
"{}{}:{}{}",
|
||||
column_name,
|
||||
row_index,
|
||||
number_to_column(r.0 + column_index).unwrap(),
|
||||
r.1 + row_index
|
||||
);
|
||||
|
||||
let formula = get_formula_attribute(
|
||||
worksheet.get_name(),
|
||||
*row_index,
|
||||
*column_index,
|
||||
&parsed_formulas[*f as usize].0,
|
||||
);
|
||||
|
||||
row_data_str.push(format!(
|
||||
r#"<c r="{cell_name}" t="e" s="{style}" cm="1"><f t="array" ref="{range}">{formula}</f><v>{ei}</v></c>"#
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
let row_style_str = match row_style_dict.get(row_index) {
|
||||
|
||||
@@ -264,30 +264,29 @@ enum ParseReferenceError {
|
||||
// There is a similar named function in ironcalc_base. We probably should fix both at the same time.
|
||||
// NB: Maybe use regexes for this?
|
||||
fn parse_reference(s: &str) -> Result<CellReferenceRC, ParseReferenceError> {
|
||||
let bytes = s.as_bytes();
|
||||
let mut sheet_name = "".to_string();
|
||||
let mut column = "".to_string();
|
||||
let mut row = "".to_string();
|
||||
let mut state = "sheet"; // "sheet", "col", "row"
|
||||
for &byte in bytes {
|
||||
for ch in s.chars() {
|
||||
match state {
|
||||
"sheet" => {
|
||||
if byte == b'!' {
|
||||
if ch == '!' {
|
||||
state = "col"
|
||||
} else {
|
||||
sheet_name.push(byte as char);
|
||||
sheet_name.push(ch);
|
||||
}
|
||||
}
|
||||
"col" => {
|
||||
if byte.is_ascii_alphabetic() {
|
||||
column.push(byte as char);
|
||||
if ch.is_ascii_alphabetic() {
|
||||
column.push(ch);
|
||||
} else {
|
||||
state = "row";
|
||||
row.push(byte as char);
|
||||
row.push(ch);
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
row.push(byte as char);
|
||||
row.push(ch);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -304,15 +303,13 @@ fn from_a1_to_rc(
|
||||
context: String,
|
||||
tables: HashMap<String, Table>,
|
||||
defined_names: Vec<DefinedNameS>,
|
||||
is_array: bool,
|
||||
) -> Result<String, XlsxError> {
|
||||
let mut parser = Parser::new(worksheets.to_owned(), defined_names, tables);
|
||||
let cell_reference =
|
||||
parse_reference(&context).map_err(|error| XlsxError::Xml(error.to_string()))?;
|
||||
let mut t = parser.parse(&formula, &cell_reference);
|
||||
if !is_array {
|
||||
add_implicit_intersection(&mut t, true);
|
||||
}
|
||||
add_implicit_intersection(&mut t, true);
|
||||
|
||||
Ok(to_rc_format(&t))
|
||||
}
|
||||
|
||||
@@ -840,7 +837,6 @@ pub(super) fn load_sheet<R: Read + std::io::Seek>(
|
||||
};
|
||||
|
||||
let cell_metadata = cell.attribute("cm");
|
||||
let is_dynamic_array = cell_metadata == Some("1");
|
||||
|
||||
// type, the default type being "n" for number
|
||||
// If the cell does not have a value is an empty cell
|
||||
@@ -907,7 +903,6 @@ pub(super) fn load_sheet<R: Read + std::io::Seek>(
|
||||
context,
|
||||
tables.clone(),
|
||||
defined_names.clone(),
|
||||
is_dynamic_array,
|
||||
)?;
|
||||
match index_map.get(&si) {
|
||||
Some(index) => {
|
||||
@@ -956,6 +951,7 @@ pub(super) fn load_sheet<R: Read + std::io::Seek>(
|
||||
return Err(XlsxError::NotImplemented("data table formulas".to_string()));
|
||||
}
|
||||
"array" | "normal" => {
|
||||
let is_dynamic_array = cell_metadata == Some("1");
|
||||
if formula_type == "array" && !is_dynamic_array {
|
||||
// Dynamic formulas in Excel are formulas of type array with the cm=1, those we support.
|
||||
// On the other hand the old CSE formulas or array formulas are not supported in IronCalc for the time being
|
||||
@@ -970,7 +966,6 @@ pub(super) fn load_sheet<R: Read + std::io::Seek>(
|
||||
context,
|
||||
tables.clone(),
|
||||
defined_names.clone(),
|
||||
is_dynamic_array,
|
||||
)?;
|
||||
|
||||
match get_formula_index(&formula, &shared_formulas) {
|
||||
@@ -1126,3 +1121,16 @@ pub(super) fn load_sheets<R: Read + std::io::Seek>(
|
||||
}
|
||||
Ok((sheets, selected_sheet))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::import::worksheets::parse_reference;
|
||||
|
||||
#[test]
|
||||
fn parse_reference_works() {
|
||||
let cell_reference = parse_reference("📈 Overview!B2");
|
||||
assert!(cell_reference.is_ok());
|
||||
let cell_reference = cell_reference.unwrap();
|
||||
assert_eq!(cell_reference.sheet, "📈 Overview");
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
BIN
xlsx/tests/templates/mortgage_calculator.xlsx
Normal file
BIN
xlsx/tests/templates/mortgage_calculator.xlsx
Normal file
Binary file not shown.
BIN
xlsx/tests/templates/travel_expenses_tracker.xlsx
Normal file
BIN
xlsx/tests/templates/travel_expenses_tracker.xlsx
Normal file
Binary file not shown.
@@ -472,6 +472,45 @@ fn test_exporting_merged_cells() {
|
||||
fs::remove_file(temp_file_name).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_templates_xlsx() {
|
||||
let mut entries = fs::read_dir("tests/templates/")
|
||||
.unwrap()
|
||||
.map(|res| res.map(|e| e.path()))
|
||||
.collect::<Result<Vec<_>, io::Error>>()
|
||||
.unwrap();
|
||||
entries.sort();
|
||||
let temp_folder = env::temp_dir();
|
||||
let path = format!("{}", Uuid::new_v4());
|
||||
let dir = temp_folder.join(path);
|
||||
fs::create_dir(&dir).unwrap();
|
||||
let mut is_error = false;
|
||||
for file_path in entries {
|
||||
let file_name_str = file_path.file_name().unwrap().to_str().unwrap();
|
||||
let file_path_str = file_path.to_str().unwrap();
|
||||
println!("Testing file: {file_path_str}");
|
||||
if file_name_str.ends_with(".xlsx") && !file_name_str.starts_with('~') {
|
||||
if let Err(message) = test_file(file_path_str) {
|
||||
println!("Error with file: '{file_path_str}'");
|
||||
println!("{message}");
|
||||
is_error = true;
|
||||
}
|
||||
let t = test_load_and_saving(file_path_str, &dir);
|
||||
if t.is_err() {
|
||||
println!("Error while load and saving file: {file_path_str}");
|
||||
is_error = true;
|
||||
}
|
||||
} else {
|
||||
println!("skipping");
|
||||
}
|
||||
}
|
||||
fs::remove_dir_all(&dir).unwrap();
|
||||
assert!(
|
||||
!is_error,
|
||||
"Models were evaluated inconsistently with XLSX data."
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_documentation_xlsx() {
|
||||
let mut entries = fs::read_dir("tests/docs/")
|
||||
|
||||
Reference in New Issue
Block a user