Compare commits
2 Commits
feature/ni
...
feature/dy
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
945897a455 | ||
|
|
6f577575c7 |
@@ -22,7 +22,7 @@ impl Model {
|
|||||||
.cell(row, column)
|
.cell(row, column)
|
||||||
.and_then(|c| c.get_formula())
|
.and_then(|c| c.get_formula())
|
||||||
{
|
{
|
||||||
let node = &self.parsed_formulas[sheet as usize][f as usize].clone();
|
let node = &self.parsed_formulas[sheet as usize][f as usize].0.clone();
|
||||||
let cell_reference = CellReferenceRC {
|
let cell_reference = CellReferenceRC {
|
||||||
sheet: self.workbook.worksheets[sheet as usize].get_name(),
|
sheet: self.workbook.worksheets[sheet as usize].get_name(),
|
||||||
row,
|
row,
|
||||||
|
|||||||
@@ -77,8 +77,6 @@ impl Model {
|
|||||||
match to_f64(&node) {
|
match to_f64(&node) {
|
||||||
Ok(f2) => match op(f1, f2) {
|
Ok(f2) => match op(f1, f2) {
|
||||||
Ok(x) => data_row.push(ArrayNode::Number(x)),
|
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) => data_row.push(ArrayNode::Error(e)),
|
||||||
},
|
},
|
||||||
Err(err) => data_row.push(ArrayNode::Error(err)),
|
Err(err) => data_row.push(ArrayNode::Error(err)),
|
||||||
@@ -100,8 +98,6 @@ impl Model {
|
|||||||
match to_f64(&node) {
|
match to_f64(&node) {
|
||||||
Ok(f1) => match op(f1, f2) {
|
Ok(f1) => match op(f1, f2) {
|
||||||
Ok(x) => data_row.push(ArrayNode::Number(x)),
|
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) => data_row.push(ArrayNode::Error(e)),
|
||||||
},
|
},
|
||||||
Err(err) => data_row.push(ArrayNode::Error(err)),
|
Err(err) => data_row.push(ArrayNode::Error(err)),
|
||||||
@@ -137,10 +133,6 @@ impl Model {
|
|||||||
(Some(v1), Some(v2)) => match (to_f64(v1), to_f64(v2)) {
|
(Some(v1), Some(v2)) => match (to_f64(v1), to_f64(v2)) {
|
||||||
(Ok(f1), Ok(f2)) => match op(f1, f2) {
|
(Ok(f1), Ok(f2)) => match op(f1, f2) {
|
||||||
Ok(x) => data_row.push(ArrayNode::Number(x)),
|
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) => data_row.push(ArrayNode::Error(e)),
|
||||||
},
|
},
|
||||||
(Err(e), _) | (_, Err(e)) => data_row.push(ArrayNode::Error(e)),
|
(Err(e), _) | (_, Err(e)) => data_row.push(ArrayNode::Error(e)),
|
||||||
|
|||||||
106
base/src/cell.rs
106
base/src/cell.rs
@@ -64,12 +64,50 @@ impl Cell {
|
|||||||
/// Returns the formula of a cell if any.
|
/// Returns the formula of a cell if any.
|
||||||
pub fn get_formula(&self) -> Option<i32> {
|
pub fn get_formula(&self) -> Option<i32> {
|
||||||
match self {
|
match self {
|
||||||
Cell::CellFormula { f, .. } => Some(*f),
|
Cell::CellFormula { f, .. }
|
||||||
Cell::CellFormulaBoolean { f, .. } => Some(*f),
|
| Cell::CellFormulaBoolean { f, .. }
|
||||||
Cell::CellFormulaNumber { f, .. } => Some(*f),
|
| Cell::CellFormulaNumber { f, .. }
|
||||||
Cell::CellFormulaString { f, .. } => Some(*f),
|
| Cell::CellFormulaString { f, .. }
|
||||||
Cell::CellFormulaError { f, .. } => Some(*f),
|
| Cell::CellFormulaError { f, .. }
|
||||||
_ => None,
|
| 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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,8 +127,15 @@ impl Cell {
|
|||||||
Cell::CellFormulaNumber { s, .. } => *s = style,
|
Cell::CellFormulaNumber { s, .. } => *s = style,
|
||||||
Cell::CellFormulaString { s, .. } => *s = style,
|
Cell::CellFormulaString { s, .. } => *s = style,
|
||||||
Cell::CellFormulaError { s, .. } => *s = style,
|
Cell::CellFormulaError { s, .. } => *s = style,
|
||||||
// Should we throw an error here?
|
Cell::SpillBooleanCell { s, .. } => *s = style,
|
||||||
Cell::Merged { .. } => {}
|
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,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,8 +151,15 @@ impl Cell {
|
|||||||
Cell::CellFormulaNumber { s, .. } => *s,
|
Cell::CellFormulaNumber { s, .. } => *s,
|
||||||
Cell::CellFormulaString { s, .. } => *s,
|
Cell::CellFormulaString { s, .. } => *s,
|
||||||
Cell::CellFormulaError { s, .. } => *s,
|
Cell::CellFormulaError { s, .. } => *s,
|
||||||
// A merged cell has no style
|
Cell::SpillBooleanCell { s, .. } => *s,
|
||||||
Cell::Merged { .. } => 0,
|
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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,7 +175,15 @@ impl Cell {
|
|||||||
Cell::CellFormulaNumber { .. } => CellType::Number,
|
Cell::CellFormulaNumber { .. } => CellType::Number,
|
||||||
Cell::CellFormulaString { .. } => CellType::Text,
|
Cell::CellFormulaString { .. } => CellType::Text,
|
||||||
Cell::CellFormulaError { .. } => CellType::ErrorValue,
|
Cell::CellFormulaError { .. } => CellType::ErrorValue,
|
||||||
Cell::Merged { .. } => CellType::Number,
|
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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,7 +201,7 @@ impl Cell {
|
|||||||
Cell::EmptyCell { .. } => CellValue::None,
|
Cell::EmptyCell { .. } => CellValue::None,
|
||||||
Cell::BooleanCell { v, s: _ } => CellValue::Boolean(*v),
|
Cell::BooleanCell { v, s: _ } => CellValue::Boolean(*v),
|
||||||
Cell::NumberCell { v, s: _ } => CellValue::Number(*v),
|
Cell::NumberCell { v, s: _ } => CellValue::Number(*v),
|
||||||
Cell::ErrorCell { ei, .. } => {
|
Cell::ErrorCell { ei, .. } | Cell::SpillErrorCell { ei, .. } => {
|
||||||
let v = ei.to_localized_error_string(language);
|
let v = ei.to_localized_error_string(language);
|
||||||
CellValue::String(v)
|
CellValue::String(v)
|
||||||
}
|
}
|
||||||
@@ -153,15 +213,25 @@ impl Cell {
|
|||||||
};
|
};
|
||||||
CellValue::String(v)
|
CellValue::String(v)
|
||||||
}
|
}
|
||||||
Cell::CellFormula { .. } => CellValue::String("#ERROR!".to_string()),
|
Cell::DynamicCellFormula { .. } | Cell::CellFormula { .. } => {
|
||||||
Cell::CellFormulaBoolean { v, .. } => CellValue::Boolean(*v),
|
CellValue::String("#ERROR!".to_string())
|
||||||
Cell::CellFormulaNumber { v, .. } => CellValue::Number(*v),
|
}
|
||||||
Cell::CellFormulaString { v, .. } => CellValue::String(v.clone()),
|
Cell::DynamicCellFormulaBoolean { v, .. } | Cell::CellFormulaBoolean { v, .. } => {
|
||||||
Cell::CellFormulaError { ei, .. } => {
|
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, .. } => {
|
||||||
let v = ei.to_localized_error_string(language);
|
let v = ei.to_localized_error_string(language);
|
||||||
CellValue::String(v)
|
CellValue::String(v)
|
||||||
}
|
}
|
||||||
Cell::Merged { .. } => CellValue::None,
|
Cell::SpillBooleanCell { v, .. } => CellValue::Boolean(*v),
|
||||||
|
Cell::SpillNumberCell { v, .. } => CellValue::Number(*v),
|
||||||
|
Cell::SpillStringCell { v, .. } => CellValue::String(v.clone()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -182,7 +182,7 @@ pub fn add_implicit_intersection(node: &mut Node, add: bool) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) enum StaticResult {
|
pub enum StaticResult {
|
||||||
Scalar,
|
Scalar,
|
||||||
Array(i32, i32),
|
Array(i32, i32),
|
||||||
Range(i32, i32),
|
Range(i32, i32),
|
||||||
@@ -218,7 +218,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.
|
// * 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.
|
// * Range(a, b) if we know it will be a a x b range.
|
||||||
// * Unknown if we cannot guaranty either
|
// * Unknown if we cannot guaranty either
|
||||||
fn run_static_analysis_on_node(node: &Node) -> StaticResult {
|
pub(crate) fn run_static_analysis_on_node(node: &Node) -> StaticResult {
|
||||||
match node {
|
match node {
|
||||||
Node::BooleanKind(_)
|
Node::BooleanKind(_)
|
||||||
| Node::NumberKind(_)
|
| Node::NumberKind(_)
|
||||||
|
|||||||
@@ -96,7 +96,7 @@ impl Model {
|
|||||||
|
|
||||||
match cell.get_formula() {
|
match cell.get_formula() {
|
||||||
Some(f) => {
|
Some(f) => {
|
||||||
let node = &self.parsed_formulas[sheet_index as usize][f as usize];
|
let node = &self.parsed_formulas[sheet_index as usize][f as usize].0;
|
||||||
matches!(
|
matches!(
|
||||||
node,
|
node,
|
||||||
Node::FunctionKind {
|
Node::FunctionKind {
|
||||||
|
|||||||
@@ -59,7 +59,6 @@ pub mod mock_time;
|
|||||||
|
|
||||||
pub use model::get_milliseconds_since_epoch;
|
pub use model::get_milliseconds_since_epoch;
|
||||||
pub use model::Model;
|
pub use model::Model;
|
||||||
pub use model::CellStructure;
|
|
||||||
pub use user_model::BorderArea;
|
pub use user_model::BorderArea;
|
||||||
pub use user_model::ClipboardData;
|
pub use user_model::ClipboardData;
|
||||||
pub use user_model::UserModel;
|
pub use user_model::UserModel;
|
||||||
|
|||||||
@@ -11,8 +11,9 @@ use crate::{
|
|||||||
lexer::LexerMode,
|
lexer::LexerMode,
|
||||||
parser::{
|
parser::{
|
||||||
move_formula::{move_formula, MoveContext},
|
move_formula::{move_formula, MoveContext},
|
||||||
|
static_analysis::{run_static_analysis_on_node, StaticResult},
|
||||||
stringify::{rename_defined_name_in_node, to_rc_format, to_string},
|
stringify::{rename_defined_name_in_node, to_rc_format, to_string},
|
||||||
Node, Parser,
|
ArrayNode, Node, Parser,
|
||||||
},
|
},
|
||||||
token::{get_error_by_name, Error, OpCompare, OpProduct, OpSum, OpUnary},
|
token::{get_error_by_name, Error, OpCompare, OpProduct, OpSum, OpUnary},
|
||||||
types::*,
|
types::*,
|
||||||
@@ -31,7 +32,6 @@ use crate::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use chrono_tz::Tz;
|
use chrono_tz::Tz;
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub use crate::mock_time::get_milliseconds_since_epoch;
|
pub use crate::mock_time::get_milliseconds_since_epoch;
|
||||||
@@ -73,27 +73,6 @@ pub(crate) enum CellState {
|
|||||||
Evaluating,
|
Evaluating,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Cell structure indicates if the cell is part of a merged cell or not
|
|
||||||
#[derive(Clone, Serialize, Deserialize)]
|
|
||||||
pub enum CellStructure {
|
|
||||||
/// The cell is not part of a merged cell
|
|
||||||
Simple,
|
|
||||||
/// The cell is part of a merged cell, and teh root cell is (row, column)
|
|
||||||
Merged {
|
|
||||||
/// Row of the root cell
|
|
||||||
row: i32,
|
|
||||||
/// Column of the root cell
|
|
||||||
column: i32,
|
|
||||||
},
|
|
||||||
/// The cell is the root of a merged cell of dimensions (width, height)
|
|
||||||
MergedRoot {
|
|
||||||
/// Width of the merged cell
|
|
||||||
width: i32,
|
|
||||||
/// Height of the merged cell
|
|
||||||
height: i32,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A parsed formula for a defined name
|
/// A parsed formula for a defined name
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub(crate) enum ParsedDefinedName {
|
pub(crate) enum ParsedDefinedName {
|
||||||
@@ -121,7 +100,7 @@ pub struct Model {
|
|||||||
/// A Rust internal representation of an Excel workbook
|
/// A Rust internal representation of an Excel workbook
|
||||||
pub workbook: Workbook,
|
pub workbook: Workbook,
|
||||||
/// A list of parsed formulas
|
/// A list of parsed formulas
|
||||||
pub parsed_formulas: Vec<Vec<Node>>,
|
pub parsed_formulas: Vec<Vec<(Node, StaticResult)>>,
|
||||||
/// A list of parsed defined names
|
/// A list of parsed defined names
|
||||||
pub(crate) parsed_defined_names: HashMap<(Option<u32>, String), ParsedDefinedName>,
|
pub(crate) parsed_defined_names: HashMap<(Option<u32>, String), ParsedDefinedName>,
|
||||||
/// An optimization to lookup strings faster
|
/// An optimization to lookup strings faster
|
||||||
@@ -544,14 +523,195 @@ impl Model {
|
|||||||
}
|
}
|
||||||
Ok(format!("{}!{}{}", sheet.name, column, cell_reference.row))
|
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(())
|
||||||
|
}
|
||||||
|
|
||||||
/// Sets `result` in the cell given by `sheet` sheet index, row and column
|
/// Sets `result` in the cell given by `sheet` sheet index, row and column
|
||||||
/// Note that will panic if the cell does not exist
|
/// Note that will panic if the cell does not exist
|
||||||
/// It will do nothing if the cell does not have a formula
|
/// It will do nothing if the cell does not have a formula
|
||||||
#[allow(clippy::expect_used)]
|
#[allow(clippy::expect_used)]
|
||||||
fn set_cell_value(&mut self, cell_reference: CellReferenceIndex, result: &CalcResult) {
|
fn set_cell_value(
|
||||||
|
&mut self,
|
||||||
|
cell_reference: CellReferenceIndex,
|
||||||
|
result: &CalcResult,
|
||||||
|
) -> Result<(), String> {
|
||||||
let CellReferenceIndex { sheet, column, row } = cell_reference;
|
let CellReferenceIndex { sheet, column, row } = cell_reference;
|
||||||
let cell = &self.workbook.worksheets[sheet as usize].sheet_data[&row][&column];
|
let cell = self
|
||||||
|
.workbook
|
||||||
|
.worksheet(sheet)?
|
||||||
|
.cell(row, column)
|
||||||
|
.cloned()
|
||||||
|
.unwrap_or_default();
|
||||||
let s = cell.get_style();
|
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() {
|
if let Some(f) = cell.get_formula() {
|
||||||
match result {
|
match result {
|
||||||
CalcResult::Number(value) => {
|
CalcResult::Number(value) => {
|
||||||
@@ -616,19 +776,138 @@ impl Model {
|
|||||||
ei: error.clone(),
|
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 } => {
|
CalcResult::Range { left, right } => {
|
||||||
if left.sheet == right.sheet
|
if left.sheet == right.sheet
|
||||||
&& left.row == right.row
|
&& left.row == right.row
|
||||||
&& left.column == right.column
|
&& left.column == right.column
|
||||||
{
|
{
|
||||||
let intersection_cell = CellReferenceIndex {
|
// There is only one cell
|
||||||
|
let single_cell = CellReferenceIndex {
|
||||||
sheet: left.sheet,
|
sheet: left.sheet,
|
||||||
column: left.column,
|
column: left.column,
|
||||||
row: left.row,
|
row: left.row,
|
||||||
};
|
};
|
||||||
let v = self.evaluate_cell(intersection_cell);
|
let v = self.evaluate_cell(single_cell);
|
||||||
self.set_cell_value(cell_reference, &v);
|
self.set_cell_value(cell_reference, &v)?;
|
||||||
} else {
|
} 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 {
|
||||||
|
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(),
|
||||||
|
};
|
||||||
|
*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) {
|
let o = match self.cell_reference_to_string(&cell_reference) {
|
||||||
Ok(s) => s,
|
Ok(s) => s,
|
||||||
Err(_) => "".to_string(),
|
Err(_) => "".to_string(),
|
||||||
@@ -642,57 +921,65 @@ impl Model {
|
|||||||
f,
|
f,
|
||||||
s,
|
s,
|
||||||
o,
|
o,
|
||||||
m: "Implicit Intersection not implemented".to_string(),
|
m: "Result would spill to non empty cells".to_string(),
|
||||||
ei: Error::NIMPL,
|
ei: Error::SPILL,
|
||||||
};
|
};
|
||||||
|
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.
|
/// Sets the color of the sheet tab.
|
||||||
@@ -736,16 +1023,18 @@ impl Model {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EmptyCell, Boolean, Number, String, Error
|
||||||
fn get_cell_value(&self, cell: &Cell, cell_reference: CellReferenceIndex) -> CalcResult {
|
fn get_cell_value(&self, cell: &Cell, cell_reference: CellReferenceIndex) -> CalcResult {
|
||||||
use Cell::*;
|
use Cell::*;
|
||||||
match cell {
|
match cell {
|
||||||
EmptyCell { .. } => CalcResult::EmptyCell,
|
EmptyCell { .. } => CalcResult::EmptyCell,
|
||||||
BooleanCell { v, .. } => CalcResult::Boolean(*v),
|
BooleanCell { v, .. } | SpillBooleanCell { v, .. } => CalcResult::Boolean(*v),
|
||||||
NumberCell { v, .. } => CalcResult::Number(*v),
|
NumberCell { v, .. } | SpillNumberCell { v, .. } => CalcResult::Number(*v),
|
||||||
ErrorCell { ei, .. } => {
|
ErrorCell { ei, .. } | SpillErrorCell { ei, .. } => {
|
||||||
let message = ei.to_localized_error_string(&self.language);
|
let message = ei.to_localized_error_string(&self.language);
|
||||||
CalcResult::new_error(ei.clone(), cell_reference, message)
|
CalcResult::new_error(ei.clone(), cell_reference, message)
|
||||||
}
|
}
|
||||||
|
SpillStringCell { v, .. } => CalcResult::String(v.clone()),
|
||||||
SharedString { si, .. } => {
|
SharedString { si, .. } => {
|
||||||
if let Some(s) = self.workbook.shared_strings.get(*si as usize) {
|
if let Some(s) = self.workbook.shared_strings.get(*si as usize) {
|
||||||
CalcResult::String(s.clone())
|
CalcResult::String(s.clone())
|
||||||
@@ -754,15 +1043,21 @@ impl Model {
|
|||||||
CalcResult::new_error(Error::ERROR, cell_reference, message)
|
CalcResult::new_error(Error::ERROR, cell_reference, message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
CellFormula { .. } => CalcResult::Error {
|
DynamicCellFormula { .. } | CellFormula { .. } => CalcResult::Error {
|
||||||
error: Error::ERROR,
|
error: Error::ERROR,
|
||||||
origin: cell_reference,
|
origin: cell_reference,
|
||||||
message: "Unevaluated formula".to_string(),
|
message: "Unevaluated formula".to_string(),
|
||||||
},
|
},
|
||||||
CellFormulaBoolean { v, .. } => CalcResult::Boolean(*v),
|
DynamicCellFormulaBoolean { v, .. } | CellFormulaBoolean { v, .. } => {
|
||||||
CellFormulaNumber { v, .. } => CalcResult::Number(*v),
|
CalcResult::Boolean(*v)
|
||||||
CellFormulaString { v, .. } => CalcResult::String(v.clone()),
|
}
|
||||||
CellFormulaError { ei, o, m, .. } => {
|
DynamicCellFormulaNumber { v, .. } | CellFormulaNumber { v, .. } => {
|
||||||
|
CalcResult::Number(*v)
|
||||||
|
}
|
||||||
|
DynamicCellFormulaString { v, .. } | CellFormulaString { v, .. } => {
|
||||||
|
CalcResult::String(v.clone())
|
||||||
|
}
|
||||||
|
DynamicCellFormulaError { ei, o, m, .. } | CellFormulaError { ei, o, m, .. } => {
|
||||||
if let Some(cell_reference) = self.parse_reference(o) {
|
if let Some(cell_reference) = self.parse_reference(o) {
|
||||||
CalcResult::new_error(ei.clone(), cell_reference, m.clone())
|
CalcResult::new_error(ei.clone(), cell_reference, m.clone())
|
||||||
} else {
|
} else {
|
||||||
@@ -773,7 +1068,6 @@ impl Model {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Merged { .. } => CalcResult::EmptyCell,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -833,9 +1127,10 @@ impl Model {
|
|||||||
self.cells.insert(key, CellState::Evaluating);
|
self.cells.insert(key, CellState::Evaluating);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let node = &self.parsed_formulas[cell_reference.sheet as usize][f as usize].clone();
|
let (node, _static_result) =
|
||||||
let result = self.evaluate_node_in_context(node, cell_reference);
|
&self.parsed_formulas[cell_reference.sheet as usize][f as usize];
|
||||||
self.set_cell_value(cell_reference, &result);
|
let result = self.evaluate_node_in_context(&node.clone(), cell_reference);
|
||||||
|
let _ = self.set_cell_value(cell_reference, &result);
|
||||||
// mark cell as evaluated
|
// mark cell as evaluated
|
||||||
self.cells.insert(key, CellState::Evaluated);
|
self.cells.insert(key, CellState::Evaluated);
|
||||||
result
|
result
|
||||||
@@ -1123,7 +1418,7 @@ impl Model {
|
|||||||
Some(cell) => match cell.get_formula() {
|
Some(cell) => match cell.get_formula() {
|
||||||
None => cell.get_text(&self.workbook.shared_strings, &self.language),
|
None => cell.get_text(&self.workbook.shared_strings, &self.language),
|
||||||
Some(i) => {
|
Some(i) => {
|
||||||
let formula = &self.parsed_formulas[sheet as usize][i as usize];
|
let formula = &self.parsed_formulas[sheet as usize][i as usize].0;
|
||||||
let cell_ref = CellReferenceRC {
|
let cell_ref = CellReferenceRC {
|
||||||
sheet: self.workbook.worksheets[sheet as usize].get_name(),
|
sheet: self.workbook.worksheets[sheet as usize].get_name(),
|
||||||
row: target_row,
|
row: target_row,
|
||||||
@@ -1226,7 +1521,8 @@ impl Model {
|
|||||||
.get(sheet as usize)
|
.get(sheet as usize)
|
||||||
.ok_or("missing sheet")?
|
.ok_or("missing sheet")?
|
||||||
.get(formula_index as usize)
|
.get(formula_index as usize)
|
||||||
.ok_or("missing formula")?;
|
.ok_or("missing formula")?
|
||||||
|
.0;
|
||||||
let cell_ref = CellReferenceRC {
|
let cell_ref = CellReferenceRC {
|
||||||
sheet: worksheet.get_name(),
|
sheet: worksheet.get_name(),
|
||||||
row,
|
row,
|
||||||
@@ -1460,11 +1756,26 @@ impl Model {
|
|||||||
column: i32,
|
column: i32,
|
||||||
value: String,
|
value: String,
|
||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
// If value starts with "'" then we force the style to be quote_prefix
|
// We need to check if the cell is part of a dynamic array
|
||||||
let cell = self.workbook.worksheet(sheet)?.cell(row, column);
|
let cell = self
|
||||||
if matches!(cell, Some(Cell::Merged { .. })) {
|
.workbook
|
||||||
return Err("Cannot set value on merged cell".to_string());
|
.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)?;
|
let style_index = self.get_cell_style_index(sheet, row, column)?;
|
||||||
if let Some(new_value) = value.strip_prefix('\'') {
|
if let Some(new_value) = value.strip_prefix('\'') {
|
||||||
// First check if it needs quoting
|
// First check if it needs quoting
|
||||||
@@ -1489,7 +1800,8 @@ impl Model {
|
|||||||
self.set_cell_with_formula(sheet, row, column, formula, new_style_index)?;
|
self.set_cell_with_formula(sheet, row, column, formula, new_style_index)?;
|
||||||
// Update the style if needed
|
// Update the style if needed
|
||||||
let cell = CellReferenceIndex { sheet, row, column };
|
let cell = CellReferenceIndex { sheet, row, column };
|
||||||
let parsed_formula = &self.parsed_formulas[sheet as usize][formula_index as usize];
|
let parsed_formula =
|
||||||
|
&self.parsed_formulas[sheet as usize][formula_index as usize].0;
|
||||||
if let Some(units) = self.compute_node_units(parsed_formula, &cell) {
|
if let Some(units) = self.compute_node_units(parsed_formula, &cell) {
|
||||||
let new_style_index = self
|
let new_style_index = self
|
||||||
.workbook
|
.workbook
|
||||||
@@ -1571,6 +1883,7 @@ impl Model {
|
|||||||
_ => parsed_formula = new_parsed_formula,
|
_ => parsed_formula = new_parsed_formula,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
let static_result = run_static_analysis_on_node(&parsed_formula);
|
||||||
|
|
||||||
let s = to_rc_format(&parsed_formula);
|
let s = to_rc_format(&parsed_formula);
|
||||||
let mut formula_index: i32 = -1;
|
let mut formula_index: i32 = -1;
|
||||||
@@ -1579,7 +1892,7 @@ impl Model {
|
|||||||
}
|
}
|
||||||
if formula_index == -1 {
|
if formula_index == -1 {
|
||||||
shared_formulas.push(s);
|
shared_formulas.push(s);
|
||||||
self.parsed_formulas[sheet as usize].push(parsed_formula);
|
self.parsed_formulas[sheet as usize].push((parsed_formula, static_result));
|
||||||
formula_index = (shared_formulas.len() as i32) - 1;
|
formula_index = (shared_formulas.len() as i32) - 1;
|
||||||
}
|
}
|
||||||
worksheet.set_cell_with_formula(row, column, formula_index, style)?;
|
worksheet.set_cell_with_formula(row, column, formula_index, style)?;
|
||||||
@@ -1774,7 +2087,7 @@ impl Model {
|
|||||||
};
|
};
|
||||||
match cell.get_formula() {
|
match cell.get_formula() {
|
||||||
Some(formula_index) => {
|
Some(formula_index) => {
|
||||||
let formula = &self.parsed_formulas[sheet as usize][formula_index as usize];
|
let formula = &self.parsed_formulas[sheet as usize][formula_index as usize].0;
|
||||||
let cell_ref = CellReferenceRC {
|
let cell_ref = CellReferenceRC {
|
||||||
sheet: worksheet.get_name(),
|
sheet: worksheet.get_name(),
|
||||||
row,
|
row,
|
||||||
@@ -1789,6 +2102,14 @@ impl Model {
|
|||||||
/// Returns a list of all cells
|
/// Returns a list of all cells
|
||||||
pub fn get_all_cells(&self) -> Vec<CellIndex> {
|
pub fn get_all_cells(&self) -> Vec<CellIndex> {
|
||||||
let mut cells = Vec::new();
|
let mut cells = Vec::new();
|
||||||
|
for (sheet, row, column) in &self.workbook.calc_chain {
|
||||||
|
let cell = CellIndex {
|
||||||
|
row: *row,
|
||||||
|
column: *column,
|
||||||
|
index: *sheet,
|
||||||
|
};
|
||||||
|
cells.push(cell);
|
||||||
|
}
|
||||||
for (index, sheet) in self.workbook.worksheets.iter().enumerate() {
|
for (index, sheet) in self.workbook.worksheets.iter().enumerate() {
|
||||||
let mut sorted_rows: Vec<_> = sheet.sheet_data.keys().collect();
|
let mut sorted_rows: Vec<_> = sheet.sheet_data.keys().collect();
|
||||||
sorted_rows.sort_unstable();
|
sorted_rows.sort_unstable();
|
||||||
@@ -1815,6 +2136,8 @@ impl Model {
|
|||||||
|
|
||||||
let cells = self.get_all_cells();
|
let cells = self.get_all_cells();
|
||||||
|
|
||||||
|
// First evaluate all dynamic arrays
|
||||||
|
|
||||||
for cell in cells {
|
for cell in cells {
|
||||||
self.evaluate_cell(CellReferenceIndex {
|
self.evaluate_cell(CellReferenceIndex {
|
||||||
sheet: cell.index,
|
sheet: cell.index,
|
||||||
@@ -1845,9 +2168,22 @@ impl Model {
|
|||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn cell_clear_contents(&mut self, sheet: u32, row: i32, column: i32) -> Result<(), String> {
|
pub fn cell_clear_contents(&mut self, sheet: u32, row: i32, column: i32) -> Result<(), String> {
|
||||||
self.workbook
|
// If it has a spill formula we need to delete the contents of all the spilled cells
|
||||||
.worksheet_mut(sheet)?
|
let worksheet = self.workbook.worksheet_mut(sheet)?;
|
||||||
.cell_clear_contents(row, column)?;
|
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)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1872,6 +2208,18 @@ impl Model {
|
|||||||
/// # }
|
/// # }
|
||||||
pub fn cell_clear_all(&mut self, sheet: u32, row: i32, column: i32) -> Result<(), String> {
|
pub fn cell_clear_all(&mut self, sheet: u32, row: i32, column: i32) -> Result<(), String> {
|
||||||
let worksheet = self.workbook.worksheet_mut(sheet)?;
|
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;
|
let sheet_data = &mut worksheet.sheet_data;
|
||||||
if let Some(row_data) = sheet_data.get_mut(&row) {
|
if let Some(row_data) = sheet_data.get_mut(&row) {
|
||||||
@@ -2285,91 +2633,6 @@ impl Model {
|
|||||||
pub fn delete_row_style(&mut self, sheet: u32, row: i32) -> Result<(), String> {
|
pub fn delete_row_style(&mut self, sheet: u32, row: i32) -> Result<(), String> {
|
||||||
self.workbook.worksheet_mut(sheet)?.delete_row_style(row)
|
self.workbook.worksheet_mut(sheet)?.delete_row_style(row)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the geometric structure of a cell
|
|
||||||
pub fn get_cell_structure(
|
|
||||||
&self,
|
|
||||||
sheet: u32,
|
|
||||||
row: i32,
|
|
||||||
column: i32,
|
|
||||||
) -> Result<CellStructure, String> {
|
|
||||||
let worksheet = self.workbook.worksheet(sheet)?;
|
|
||||||
worksheet.get_cell_structure(row, column)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Merges cells
|
|
||||||
pub fn merge_cells(
|
|
||||||
&mut self,
|
|
||||||
sheet: u32,
|
|
||||||
row: i32,
|
|
||||||
column: i32,
|
|
||||||
width: i32,
|
|
||||||
height: i32,
|
|
||||||
) -> Result<(), String> {
|
|
||||||
let worksheet = self.workbook.worksheet_mut(sheet)?;
|
|
||||||
let sheet_data = &mut worksheet.sheet_data;
|
|
||||||
// First check that it is possible to merge the cells
|
|
||||||
for r in row..(row + height) {
|
|
||||||
for c in column..(column + width) {
|
|
||||||
if let Some(Cell::Merged { .. }) =
|
|
||||||
sheet_data.get(&r).and_then(|row_data| row_data.get(&c))
|
|
||||||
{
|
|
||||||
return Err("Cannot merge cells".to_string());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
worksheet
|
|
||||||
.merged_cells
|
|
||||||
.insert((row, column), (width, height));
|
|
||||||
for r in row..(row + height) {
|
|
||||||
for c in column..(column + width) {
|
|
||||||
// We remove everything except the "root" cell:
|
|
||||||
if r == row && c == column {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if let Some(row_data) = sheet_data.get_mut(&r) {
|
|
||||||
row_data.remove(&c);
|
|
||||||
row_data.insert(c, Cell::Merged { r: row, c: column });
|
|
||||||
} else {
|
|
||||||
let mut row_data = HashMap::new();
|
|
||||||
row_data.insert(c, Cell::Merged { r: row, c: column });
|
|
||||||
sheet_data.insert(r, row_data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Unmerges cells
|
|
||||||
pub fn unmerge_cells(&mut self, sheet: u32, row: i32, column: i32) -> Result<(), String> {
|
|
||||||
let s = self.get_cell_style_index(sheet, row, column)?;
|
|
||||||
let worksheet = self.workbook.worksheet_mut(sheet)?;
|
|
||||||
let sheet_data = &mut worksheet.sheet_data;
|
|
||||||
let (width, height) = match worksheet.merged_cells.get(&(row, column)) {
|
|
||||||
Some((w, h)) => (*w, *h),
|
|
||||||
None => return Ok(()),
|
|
||||||
};
|
|
||||||
worksheet.merged_cells.remove(&(row, column));
|
|
||||||
for r in row..(row + width) {
|
|
||||||
for c in column..(column + height) {
|
|
||||||
// We remove everything except the "root" cell:
|
|
||||||
if r == row && c == column {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if let Some(row_data) = sheet_data.get_mut(&r) {
|
|
||||||
row_data.remove(&c);
|
|
||||||
if s != 0 {
|
|
||||||
row_data.insert(c, Cell::EmptyCell { s });
|
|
||||||
}
|
|
||||||
} else if s != 0 {
|
|
||||||
let mut row_data = HashMap::new();
|
|
||||||
row_data.insert(c, Cell::EmptyCell { s });
|
|
||||||
sheet_data.insert(r, row_data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ use crate::{
|
|||||||
expressions::{
|
expressions::{
|
||||||
lexer::LexerMode,
|
lexer::LexerMode,
|
||||||
parser::{
|
parser::{
|
||||||
|
static_analysis::run_static_analysis_on_node,
|
||||||
stringify::{rename_sheet_in_node, to_rc_format, to_string},
|
stringify::{rename_sheet_in_node, to_rc_format, to_string},
|
||||||
Parser,
|
Parser,
|
||||||
},
|
},
|
||||||
@@ -58,10 +59,10 @@ impl Model {
|
|||||||
rows: vec![],
|
rows: vec![],
|
||||||
comments: vec![],
|
comments: vec![],
|
||||||
dimension: "A1".to_string(),
|
dimension: "A1".to_string(),
|
||||||
|
merge_cells: vec![],
|
||||||
name: name.to_string(),
|
name: name.to_string(),
|
||||||
shared_formulas: vec![],
|
shared_formulas: vec![],
|
||||||
sheet_data: Default::default(),
|
sheet_data: Default::default(),
|
||||||
merged_cells: HashMap::new(),
|
|
||||||
sheet_id,
|
sheet_id,
|
||||||
state: SheetState::Visible,
|
state: SheetState::Visible,
|
||||||
color: Default::default(),
|
color: Default::default(),
|
||||||
@@ -94,7 +95,8 @@ impl Model {
|
|||||||
let mut parse_formula = Vec::new();
|
let mut parse_formula = Vec::new();
|
||||||
for formula in shared_formulas {
|
for formula in shared_formulas {
|
||||||
let t = self.parser.parse(formula, &cell_reference);
|
let t = self.parser.parse(formula, &cell_reference);
|
||||||
parse_formula.push(t);
|
let static_result = run_static_analysis_on_node(&t);
|
||||||
|
parse_formula.push((t, static_result));
|
||||||
}
|
}
|
||||||
self.parsed_formulas.push(parse_formula);
|
self.parsed_formulas.push(parse_formula);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ mod test_fn_offset;
|
|||||||
mod test_number_format;
|
mod test_number_format;
|
||||||
|
|
||||||
mod test_arrays;
|
mod test_arrays;
|
||||||
|
mod test_dynamic_arrays;
|
||||||
mod test_escape_quotes;
|
mod test_escape_quotes;
|
||||||
mod test_extend;
|
mod test_extend;
|
||||||
mod test_fn_fv;
|
mod test_fn_fv;
|
||||||
|
|||||||
50
base/src/test/test_dynamic_arrays.rs
Normal file
50
base/src/test/test_dynamic_arrays.rs
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
#![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");
|
||||||
|
}
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
use crate::test::util::new_empty_model;
|
use crate::test::util::new_empty_model;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn simple_colum() {
|
fn simple_column() {
|
||||||
let mut model = new_empty_model();
|
let mut model = new_empty_model();
|
||||||
// We populate cells A1 to A3
|
// We populate cells A1 to A3
|
||||||
model._set("A1", "1");
|
model._set("A1", "1");
|
||||||
@@ -30,7 +30,7 @@ fn return_of_array_is_n_impl() {
|
|||||||
|
|
||||||
model.evaluate();
|
model.evaluate();
|
||||||
|
|
||||||
assert_eq!(model._get_text("C2"), "#N/IMPL!".to_string());
|
assert_eq!(model._get_text("C2"), "1".to_string());
|
||||||
assert_eq!(model._get_text("D2"), "1.89188842".to_string());
|
assert_eq!(model._get_text("D2"), "1.89188842".to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,8 +5,10 @@ mod test_border;
|
|||||||
mod test_clear_cells;
|
mod test_clear_cells;
|
||||||
mod test_column_style;
|
mod test_column_style;
|
||||||
mod test_defined_names;
|
mod test_defined_names;
|
||||||
|
mod test_delete_evaluates;
|
||||||
mod test_delete_row_column_formatting;
|
mod test_delete_row_column_formatting;
|
||||||
mod test_diff_queue;
|
mod test_diff_queue;
|
||||||
|
mod test_dynamic_array;
|
||||||
mod test_evaluation;
|
mod test_evaluation;
|
||||||
mod test_general;
|
mod test_general;
|
||||||
mod test_grid_lines;
|
mod test_grid_lines;
|
||||||
|
|||||||
47
base/src/test/user_model/test_delete_evaluates.rs
Normal file
47
base/src/test/user_model/test_delete_evaluates.rs
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
#![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()));
|
||||||
|
}
|
||||||
130
base/src/test/user_model/test_dynamic_array.rs
Normal file
130
base/src/test/user_model/test_dynamic_array.rs
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
#![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 => ={34,35,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,6 +51,9 @@ pub struct Workbook {
|
|||||||
pub metadata: Metadata,
|
pub metadata: Metadata,
|
||||||
pub tables: HashMap<String, Table>,
|
pub tables: HashMap<String, Table>,
|
||||||
pub views: HashMap<u32, WorkbookView>,
|
pub views: HashMap<u32, WorkbookView>,
|
||||||
|
/// Calculation chain of the dynamic arrays.
|
||||||
|
/// List of tuples (sheet_id, row, column)
|
||||||
|
pub calc_chain: Vec<(u32, i32, i32)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A defined name. The `sheet_id` is the sheet index in case the name is local
|
/// A defined name. The `sheet_id` is the sheet index in case the name is local
|
||||||
@@ -110,7 +113,7 @@ pub struct Worksheet {
|
|||||||
pub sheet_id: u32,
|
pub sheet_id: u32,
|
||||||
pub state: SheetState,
|
pub state: SheetState,
|
||||||
pub color: Option<String>,
|
pub color: Option<String>,
|
||||||
pub merged_cells: HashMap<(i32, i32), (i32, i32)>,
|
pub merge_cells: Vec<String>,
|
||||||
pub comments: Vec<Comment>,
|
pub comments: Vec<Comment>,
|
||||||
pub frozen_rows: i32,
|
pub frozen_rows: i32,
|
||||||
pub frozen_columns: i32,
|
pub frozen_columns: i32,
|
||||||
@@ -159,17 +162,17 @@ pub enum CellType {
|
|||||||
CompoundData = 128,
|
CompoundData = 128,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Cell types
|
||||||
|
/// s is always the style index of the cell
|
||||||
#[derive(Encode, Decode, Debug, Clone, PartialEq)]
|
#[derive(Encode, Decode, Debug, Clone, PartialEq)]
|
||||||
pub enum Cell {
|
pub enum Cell {
|
||||||
EmptyCell {
|
EmptyCell {
|
||||||
s: i32,
|
s: i32,
|
||||||
},
|
},
|
||||||
|
|
||||||
BooleanCell {
|
BooleanCell {
|
||||||
v: bool,
|
v: bool,
|
||||||
s: i32,
|
s: i32,
|
||||||
},
|
},
|
||||||
|
|
||||||
NumberCell {
|
NumberCell {
|
||||||
v: f64,
|
v: f64,
|
||||||
s: i32,
|
s: i32,
|
||||||
@@ -181,6 +184,7 @@ pub enum Cell {
|
|||||||
},
|
},
|
||||||
// Always a shared string
|
// Always a shared string
|
||||||
SharedString {
|
SharedString {
|
||||||
|
// string index
|
||||||
si: i32,
|
si: i32,
|
||||||
s: i32,
|
s: i32,
|
||||||
},
|
},
|
||||||
@@ -189,13 +193,11 @@ pub enum Cell {
|
|||||||
f: i32,
|
f: i32,
|
||||||
s: i32,
|
s: i32,
|
||||||
},
|
},
|
||||||
|
|
||||||
CellFormulaBoolean {
|
CellFormulaBoolean {
|
||||||
f: i32,
|
f: i32,
|
||||||
v: bool,
|
v: bool,
|
||||||
s: i32,
|
s: i32,
|
||||||
},
|
},
|
||||||
|
|
||||||
CellFormulaNumber {
|
CellFormulaNumber {
|
||||||
f: i32,
|
f: i32,
|
||||||
v: f64,
|
v: f64,
|
||||||
@@ -207,9 +209,9 @@ pub enum Cell {
|
|||||||
v: String,
|
v: String,
|
||||||
s: i32,
|
s: i32,
|
||||||
},
|
},
|
||||||
|
|
||||||
CellFormulaError {
|
CellFormulaError {
|
||||||
f: i32,
|
f: i32,
|
||||||
|
// error index
|
||||||
ei: Error,
|
ei: Error,
|
||||||
s: i32,
|
s: i32,
|
||||||
// Origin: Sheet3!C4
|
// Origin: Sheet3!C4
|
||||||
@@ -217,10 +219,81 @@ pub enum Cell {
|
|||||||
// Error Message: "Not implemented function"
|
// Error Message: "Not implemented function"
|
||||||
m: String,
|
m: String,
|
||||||
},
|
},
|
||||||
Merged {
|
// All Spill/dynamic cells have a boolean, a for array, if true it is an array formula
|
||||||
r: i32,
|
// Spill cells point to a mother cell (row, column)
|
||||||
c: i32,
|
SpillNumberCell {
|
||||||
}, // TODO: Array formulas
|
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,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Cell {
|
impl Default for Cell {
|
||||||
|
|||||||
@@ -11,10 +11,10 @@ use crate::{
|
|||||||
types::{Area, CellReferenceIndex},
|
types::{Area, CellReferenceIndex},
|
||||||
utils::{is_valid_column_number, is_valid_row},
|
utils::{is_valid_column_number, is_valid_row},
|
||||||
},
|
},
|
||||||
model::{CellStructure, Model},
|
model::Model,
|
||||||
types::{
|
types::{
|
||||||
Alignment, BorderItem, CellType, Col, HorizontalAlignment, SheetProperties, SheetState,
|
Alignment, BorderItem, Cell, CellType, Col, HorizontalAlignment, SheetProperties,
|
||||||
Style, VerticalAlignment,
|
SheetState, Style, VerticalAlignment,
|
||||||
},
|
},
|
||||||
utils::is_valid_hex_color,
|
utils::is_valid_hex_color,
|
||||||
};
|
};
|
||||||
@@ -24,6 +24,18 @@ use crate::user_model::history::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use super::border_utils::is_max_border;
|
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
|
/// Data for the clipboard
|
||||||
pub type ClipboardData = HashMap<i32, HashMap<i32, ClipboardCell>>;
|
pub type ClipboardData = HashMap<i32, HashMap<i32, ClipboardCell>>;
|
||||||
|
|
||||||
@@ -627,6 +639,7 @@ impl UserModel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.push_diff_list(diff_list);
|
self.push_diff_list(diff_list);
|
||||||
|
self.evaluate_if_not_paused();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -656,6 +669,7 @@ impl UserModel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.push_diff_list(diff_list);
|
self.push_diff_list(diff_list);
|
||||||
|
self.evaluate_if_not_paused();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1595,6 +1609,65 @@ impl UserModel {
|
|||||||
Ok(self.model.workbook.worksheet(sheet)?.show_grid_lines)
|
Ok(self.model.workbook.worksheet(sheet)?.show_grid_lines)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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
|
/// Returns a copy of the selected area
|
||||||
pub fn copy_to_clipboard(&self) -> Result<Clipboard, String> {
|
pub fn copy_to_clipboard(&self) -> Result<Clipboard, String> {
|
||||||
let selected_area = self.get_selected_view();
|
let selected_area = self.get_selected_view();
|
||||||
@@ -1869,57 +1942,6 @@ impl UserModel {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Merges cells
|
|
||||||
pub fn merge_cells(
|
|
||||||
&mut self,
|
|
||||||
sheet: u32,
|
|
||||||
row: i32,
|
|
||||||
column: i32,
|
|
||||||
width: i32,
|
|
||||||
height: i32,
|
|
||||||
) -> Result<(), String> {
|
|
||||||
let old_data = Vec::new();
|
|
||||||
let diff_list = vec![Diff::MergeCells {
|
|
||||||
sheet,
|
|
||||||
row,
|
|
||||||
column,
|
|
||||||
width,
|
|
||||||
height,
|
|
||||||
old_data,
|
|
||||||
}];
|
|
||||||
self.model.merge_cells(sheet, row, column, width, height)?;
|
|
||||||
self.push_diff_list(diff_list);
|
|
||||||
self.evaluate_if_not_paused();
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Check if cell is part of a merged cell
|
|
||||||
pub fn get_cell_structure(&self, sheet: u32, row: i32, column: i32) -> Result<CellStructure, String> {
|
|
||||||
self.model.get_cell_structure(sheet, row, column)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Unmerges cells
|
|
||||||
pub fn unmerge_cells(&mut self, sheet: u32, row: i32, column: i32) -> Result<(), String> {
|
|
||||||
let (width, height) = self
|
|
||||||
.model
|
|
||||||
.workbook
|
|
||||||
.worksheet(sheet)?
|
|
||||||
.merged_cells
|
|
||||||
.get(&(row, column))
|
|
||||||
.ok_or("No merged cells found")?;
|
|
||||||
let diff_list = vec![Diff::UnmergeCells {
|
|
||||||
sheet,
|
|
||||||
row,
|
|
||||||
column,
|
|
||||||
width: *width,
|
|
||||||
height: *height,
|
|
||||||
}];
|
|
||||||
self.model.unmerge_cells(sheet, row, column)?;
|
|
||||||
self.push_diff_list(diff_list);
|
|
||||||
self.evaluate_if_not_paused();
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
// **** Private methods ****** //
|
// **** Private methods ****** //
|
||||||
|
|
||||||
pub(crate) fn push_diff_list(&mut self, diff_list: DiffList) {
|
pub(crate) fn push_diff_list(&mut self, diff_list: DiffList) {
|
||||||
@@ -1948,6 +1970,24 @@ impl UserModel {
|
|||||||
old_value,
|
old_value,
|
||||||
} => {
|
} => {
|
||||||
needs_evaluation = true;
|
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() {
|
match *old_value.clone() {
|
||||||
Some(value) => {
|
Some(value) => {
|
||||||
self.model
|
self.model
|
||||||
@@ -2163,6 +2203,7 @@ impl UserModel {
|
|||||||
worksheet.frozen_rows = old_data.frozen_rows;
|
worksheet.frozen_rows = old_data.frozen_rows;
|
||||||
worksheet.state = old_data.state.clone();
|
worksheet.state = old_data.state.clone();
|
||||||
worksheet.color = old_data.color.clone();
|
worksheet.color = old_data.color.clone();
|
||||||
|
worksheet.merge_cells = old_data.merge_cells.clone();
|
||||||
worksheet.shared_formulas = old_data.shared_formulas.clone();
|
worksheet.shared_formulas = old_data.shared_formulas.clone();
|
||||||
self.model.reset_parsed_structures();
|
self.model.reset_parsed_structures();
|
||||||
|
|
||||||
@@ -2213,34 +2254,6 @@ impl UserModel {
|
|||||||
self.model.delete_row_style(*sheet, *row)?;
|
self.model.delete_row_style(*sheet, *row)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Diff::MergeCells {
|
|
||||||
sheet,
|
|
||||||
row,
|
|
||||||
column,
|
|
||||||
width,
|
|
||||||
height,
|
|
||||||
old_data,
|
|
||||||
} => {
|
|
||||||
needs_evaluation = true;
|
|
||||||
self.model.unmerge_cells(*sheet, *row, *column)?;
|
|
||||||
// for (r, c, v) in old_data.iter() {
|
|
||||||
// self.model
|
|
||||||
// .workbook
|
|
||||||
// .worksheet_mut(*sheet)?
|
|
||||||
// .update_cell(*r, *c, v.clone())?;
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
Diff::UnmergeCells {
|
|
||||||
sheet,
|
|
||||||
row,
|
|
||||||
column,
|
|
||||||
width,
|
|
||||||
height,
|
|
||||||
} => {
|
|
||||||
needs_evaluation = true;
|
|
||||||
self.model
|
|
||||||
.merge_cells(*sheet, *row, *column, *width, *height)?;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if needs_evaluation {
|
if needs_evaluation {
|
||||||
@@ -2442,34 +2455,6 @@ impl UserModel {
|
|||||||
} => {
|
} => {
|
||||||
self.model.delete_row_style(*sheet, *row)?;
|
self.model.delete_row_style(*sheet, *row)?;
|
||||||
}
|
}
|
||||||
Diff::MergeCells {
|
|
||||||
sheet,
|
|
||||||
row,
|
|
||||||
column,
|
|
||||||
width,
|
|
||||||
height,
|
|
||||||
old_data: _,
|
|
||||||
} => {
|
|
||||||
needs_evaluation = true;
|
|
||||||
self.model
|
|
||||||
.merge_cells(*sheet, *row, *column, *width, *height)?;
|
|
||||||
// for (r, c, v) in old_data.iter() {
|
|
||||||
// self.model
|
|
||||||
// .workbook
|
|
||||||
// .worksheet_mut(*sheet)?
|
|
||||||
// .update_cell(*r, *c, v.clone())?;
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
Diff::UnmergeCells {
|
|
||||||
sheet,
|
|
||||||
row,
|
|
||||||
column,
|
|
||||||
width,
|
|
||||||
height,
|
|
||||||
} => {
|
|
||||||
needs_evaluation = true;
|
|
||||||
self.model.unmerge_cells(*sheet, *row, *column)?;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,18 +5,21 @@ use bitcode::{Decode, Encode};
|
|||||||
use crate::types::{Cell, Col, Row, SheetState, Style, Worksheet};
|
use crate::types::{Cell, Col, Row, SheetState, Style, Worksheet};
|
||||||
|
|
||||||
#[derive(Clone, Encode, Decode)]
|
#[derive(Clone, Encode, Decode)]
|
||||||
|
#[cfg_attr(debug_assertions, derive(Debug))]
|
||||||
pub(crate) struct RowData {
|
pub(crate) struct RowData {
|
||||||
pub(crate) row: Option<Row>,
|
pub(crate) row: Option<Row>,
|
||||||
pub(crate) data: HashMap<i32, Cell>,
|
pub(crate) data: HashMap<i32, Cell>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Encode, Decode)]
|
#[derive(Clone, Encode, Decode)]
|
||||||
|
#[cfg_attr(debug_assertions, derive(Debug))]
|
||||||
pub(crate) struct ColumnData {
|
pub(crate) struct ColumnData {
|
||||||
pub(crate) column: Option<Col>,
|
pub(crate) column: Option<Col>,
|
||||||
pub(crate) data: HashMap<i32, Cell>,
|
pub(crate) data: HashMap<i32, Cell>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Encode, Decode)]
|
#[derive(Clone, Encode, Decode)]
|
||||||
|
#[cfg_attr(debug_assertions, derive(Debug))]
|
||||||
pub(crate) enum Diff {
|
pub(crate) enum Diff {
|
||||||
// Cell diffs
|
// Cell diffs
|
||||||
SetCellValue {
|
SetCellValue {
|
||||||
@@ -161,21 +164,7 @@ pub(crate) enum Diff {
|
|||||||
new_scope: Option<u32>,
|
new_scope: Option<u32>,
|
||||||
new_formula: String,
|
new_formula: String,
|
||||||
},
|
},
|
||||||
MergeCells {
|
// FIXME: we are missing SetViewDiffs
|
||||||
sheet: u32,
|
|
||||||
row: i32,
|
|
||||||
column: i32,
|
|
||||||
width: i32,
|
|
||||||
height: i32,
|
|
||||||
old_data: Vec<(Cell, Style)>,
|
|
||||||
},
|
|
||||||
UnmergeCells {
|
|
||||||
sheet: u32,
|
|
||||||
row: i32,
|
|
||||||
column: i32,
|
|
||||||
width: i32,
|
|
||||||
height: i32,
|
|
||||||
}, // FIXME: we are missing SetViewDiffs
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) type DiffList = Vec<Diff>;
|
pub(crate) type DiffList = Vec<Diff>;
|
||||||
|
|||||||
@@ -2,10 +2,7 @@
|
|||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::{
|
use crate::expressions::utils::{is_valid_column_number, is_valid_row};
|
||||||
expressions::utils::{is_valid_column_number, is_valid_row},
|
|
||||||
CellStructure,
|
|
||||||
};
|
|
||||||
|
|
||||||
use super::common::UserModel;
|
use super::common::UserModel;
|
||||||
|
|
||||||
@@ -100,47 +97,26 @@ impl UserModel {
|
|||||||
if !is_valid_row(row) {
|
if !is_valid_row(row) {
|
||||||
return Err(format!("Invalid row: '{row}'"));
|
return Err(format!("Invalid row: '{row}'"));
|
||||||
}
|
}
|
||||||
let worksheet = self.model.workbook.worksheet_mut(sheet)?;
|
if self.model.workbook.worksheet(sheet).is_err() {
|
||||||
let structure = worksheet.get_cell_structure(row, column)?;
|
return Err(format!("Invalid worksheet index {}", sheet));
|
||||||
// check if the selected cell is a merged cell
|
}
|
||||||
let [row_start, columns_start, row_end, columns_end] = match structure {
|
if let Ok(worksheet) = self.model.workbook.worksheet_mut(sheet) {
|
||||||
CellStructure::Simple => [row, column, row, column],
|
if let Some(view) = worksheet.views.get_mut(&0) {
|
||||||
CellStructure::Merged {
|
view.row = row;
|
||||||
row: row_start,
|
view.column = column;
|
||||||
column: column_start,
|
view.range = [row, column, row, column];
|
||||||
} => {
|
}
|
||||||
let (width, height) = match worksheet.merged_cells.get(&(row_start, column_start)) {
|
|
||||||
Some(s) => s,
|
|
||||||
None => return Err(format!("Merged cell not found: ({row_start}, {column_start}) when clicking at ({row}, {column}).")),
|
|
||||||
};
|
|
||||||
let row_end = row_start + height - 1;
|
|
||||||
let column_end = column_start + width - 1;
|
|
||||||
[row_start, column_start, row_end, column_end]
|
|
||||||
}
|
|
||||||
CellStructure::MergedRoot { width, height } => {
|
|
||||||
let row_start = row;
|
|
||||||
let columns_start = column;
|
|
||||||
let row_end = row + height - 1;
|
|
||||||
let columns_end = column + width - 1;
|
|
||||||
[row_start, columns_start, row_end, columns_end]
|
|
||||||
}
|
|
||||||
};
|
|
||||||
if let Some(view) = worksheet.views.get_mut(&0) {
|
|
||||||
view.row = row_start;
|
|
||||||
view.column = columns_start;
|
|
||||||
view.range = [row_start, columns_start, row_end, columns_end];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the selected range. Note that the selected cell must be in one of the corners.
|
/// Sets the selected range. Note that the selected cell must be in one of the corners.
|
||||||
pub fn set_selected_range(
|
pub fn set_selected_range(
|
||||||
&mut self,
|
&mut self,
|
||||||
row_start: i32,
|
start_row: i32,
|
||||||
column_start: i32,
|
start_column: i32,
|
||||||
row_end: i32,
|
end_row: i32,
|
||||||
column_end: i32,
|
end_column: i32,
|
||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
let sheet = if let Some(view) = self.model.workbook.views.get(&self.model.view_id) {
|
let sheet = if let Some(view) = self.model.workbook.views.get(&self.model.view_id) {
|
||||||
view.sheet
|
view.sheet
|
||||||
@@ -148,72 +124,42 @@ impl UserModel {
|
|||||||
0
|
0
|
||||||
};
|
};
|
||||||
|
|
||||||
if !is_valid_column_number(column_start) {
|
if !is_valid_column_number(start_column) {
|
||||||
return Err(format!("Invalid column: '{column_start}'"));
|
return Err(format!("Invalid column: '{start_column}'"));
|
||||||
}
|
}
|
||||||
if !is_valid_row(row_start) {
|
if !is_valid_row(start_row) {
|
||||||
return Err(format!("Invalid row: '{row_start}'"));
|
return Err(format!("Invalid row: '{start_row}'"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if !is_valid_column_number(column_end) {
|
if !is_valid_column_number(end_column) {
|
||||||
return Err(format!("Invalid column: '{column_end}'"));
|
return Err(format!("Invalid column: '{end_column}'"));
|
||||||
}
|
}
|
||||||
if !is_valid_row(row_end) {
|
if !is_valid_row(end_row) {
|
||||||
return Err(format!("Invalid row: '{row_end}'"));
|
return Err(format!("Invalid row: '{end_row}'"));
|
||||||
}
|
}
|
||||||
let mut start_row = row_start;
|
if self.model.workbook.worksheet(sheet).is_err() {
|
||||||
let mut start_column = column_start;
|
return Err(format!("Invalid worksheet index {}", sheet));
|
||||||
let mut end_row = row_end;
|
}
|
||||||
let mut end_column = column_end;
|
if let Ok(worksheet) = self.model.workbook.worksheet_mut(sheet) {
|
||||||
let worksheet = self.model.workbook.worksheet_mut(sheet)?;
|
if let Some(view) = worksheet.views.get_mut(&0) {
|
||||||
let merged_cells = &worksheet.merged_cells;
|
let selected_row = view.row;
|
||||||
if !merged_cells.is_empty() {
|
let selected_column = view.column;
|
||||||
// We need to check if there are merged cells in the selected range
|
// The selected cells must be on one of the corners of the selected range:
|
||||||
for row in row_start..=row_end {
|
if selected_row != start_row && selected_row != end_row {
|
||||||
for column in column_start..=column_end {
|
return Err(format!(
|
||||||
let structure = &worksheet.get_cell_structure(row, column)?;
|
"The selected cells is not in one of the corners. Row: '{}' and row range '({}, {})'",
|
||||||
match structure {
|
selected_row, start_row, end_row
|
||||||
CellStructure::Simple => {}
|
));
|
||||||
CellStructure::Merged { row: r, column: c } => {
|
|
||||||
// The selected range must contain the merged cell
|
|
||||||
let (width, height) = match merged_cells.get(&(*r, *c)) {
|
|
||||||
Some(s) => s,
|
|
||||||
None => return Err(format!("Merged cell not found: ({r}, {c}) when selecting range ({start_row}, {start_column}, {end_row}, {end_column}).")),
|
|
||||||
};
|
|
||||||
start_row = start_row.min(*r);
|
|
||||||
start_column = start_column.min(*c);
|
|
||||||
end_row = end_row.max(*r + height - 1);
|
|
||||||
end_column = end_column.max(*c + width - 1);
|
|
||||||
|
|
||||||
}
|
|
||||||
CellStructure::MergedRoot { width, height } => {
|
|
||||||
// The selected range must contain the merged cell
|
|
||||||
end_row = end_row.max(row + height - 1);
|
|
||||||
end_column = end_column.max(column + width - 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if selected_column != start_column && selected_column != end_column {
|
||||||
|
return Err(format!(
|
||||||
|
"The selected cells is not in one of the corners. Column '{}' and column range '({}, {})'",
|
||||||
|
selected_column, start_column, end_column
|
||||||
|
));
|
||||||
|
}
|
||||||
|
view.range = [start_row, start_column, end_row, end_column];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(view) = worksheet.views.get_mut(&0) {
|
|
||||||
// let selected_row = view.row;
|
|
||||||
// let selected_column = view.column;
|
|
||||||
// // The selected cells must be on one of the corners of the selected range:
|
|
||||||
// if selected_row != start_row && selected_row != end_row {
|
|
||||||
// return Err(format!(
|
|
||||||
// "The selected cells is not in one of the corners. Row: '{}' and row range '({}, {})'",
|
|
||||||
// selected_row, start_row, end_row
|
|
||||||
// ));
|
|
||||||
// }
|
|
||||||
// if selected_column != start_column && selected_column != end_column {
|
|
||||||
// return Err(format!(
|
|
||||||
// "The selected cells is not in one of the corners. Column '{}' and column range '({}, {})'",
|
|
||||||
// selected_column, start_column, end_column
|
|
||||||
// ));
|
|
||||||
// }
|
|
||||||
view.range = [start_row, start_column, end_row, end_column];
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
use crate::constants::{self, LAST_COLUMN, LAST_ROW};
|
use crate::constants::{self, LAST_COLUMN, LAST_ROW};
|
||||||
use crate::expressions::types::CellReferenceIndex;
|
use crate::expressions::types::CellReferenceIndex;
|
||||||
use crate::expressions::utils::{is_valid_column_number, is_valid_row};
|
use crate::expressions::utils::{is_valid_column_number, is_valid_row};
|
||||||
use crate::CellStructure;
|
|
||||||
use crate::{expressions::token::Error, types::*};
|
use crate::{expressions::token::Error, types::*};
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
@@ -39,24 +38,6 @@ impl Worksheet {
|
|||||||
self.sheet_data.get(&row)?.get(&column)
|
self.sheet_data.get(&row)?.get(&column)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_cell_structure(&self, row: i32, column: i32) -> Result<CellStructure, String> {
|
|
||||||
if let Some((width, height)) = self.merged_cells.get(&(row, column)) {
|
|
||||||
return Ok(CellStructure::MergedRoot {
|
|
||||||
width: *width,
|
|
||||||
height: *height,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
let cell = self.cell(row, column);
|
|
||||||
if let Some(Cell::Merged { r, c }) = cell {
|
|
||||||
return Ok(CellStructure::Merged {
|
|
||||||
row: *r,
|
|
||||||
column: *c,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(CellStructure::Simple)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn cell_mut(&mut self, row: i32, column: i32) -> Option<&mut Cell> {
|
pub(crate) fn cell_mut(&mut self, row: i32, column: i32) -> Option<&mut Cell> {
|
||||||
self.sheet_data.get_mut(&row)?.get_mut(&column)
|
self.sheet_data.get_mut(&row)?.get_mut(&column)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -201,24 +201,16 @@ defined_name_list_types = r"""
|
|||||||
getDefinedNameList(): DefinedName[];
|
getDefinedNameList(): DefinedName[];
|
||||||
"""
|
"""
|
||||||
|
|
||||||
merged_cells = r"""
|
cell_structure = r"""
|
||||||
/**
|
|
||||||
* @param {number} sheet
|
|
||||||
* @param {number} row
|
|
||||||
* @param {number} column
|
|
||||||
* @returns {any}
|
* @returns {any}
|
||||||
*/
|
*/
|
||||||
getCellStructure(sheet: number, row: number, column: number): any;
|
getCellArrayStructure(sheet: number, row: number, column: number): any;
|
||||||
"""
|
"""
|
||||||
|
|
||||||
merged_cells_types = r"""
|
cell_structure_types = r"""
|
||||||
/**
|
* @returns {CellArrayStructure}
|
||||||
* @param {number} sheet
|
|
||||||
* @param {number} row
|
|
||||||
* @param {number} column
|
|
||||||
* @returns {CellStructure}
|
|
||||||
*/
|
*/
|
||||||
getCellStructure(sheet: number, row: number, column: number): CellStructure;
|
getCellArrayStructure(sheet: number, row: number, column: number): CellArrayStructure;
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def fix_types(text):
|
def fix_types(text):
|
||||||
@@ -235,7 +227,7 @@ def fix_types(text):
|
|||||||
text = text.replace(clipboard, clipboard_types)
|
text = text.replace(clipboard, clipboard_types)
|
||||||
text = text.replace(paste_from_clipboard, paste_from_clipboard_types)
|
text = text.replace(paste_from_clipboard, paste_from_clipboard_types)
|
||||||
text = text.replace(defined_name_list, defined_name_list_types)
|
text = text.replace(defined_name_list, defined_name_list_types)
|
||||||
text = text.replace(merged_cells, merged_cells_types)
|
text = text.replace(cell_structure, cell_structure_types)
|
||||||
with open("types.ts") as f:
|
with open("types.ts") as f:
|
||||||
types_str = f.read()
|
types_str = f.read()
|
||||||
header_types = "{}\n\n{}".format(header, types_str)
|
header_types = "{}\n\n{}".format(header, types_str)
|
||||||
|
|||||||
@@ -5,7 +5,9 @@ use wasm_bindgen::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use ironcalc_base::{
|
use ironcalc_base::{
|
||||||
expressions::{lexer::util::get_tokens as tokenizer, types::Area, utils::number_to_column}, types::{CellType, Style}, BorderArea, ClipboardData, UserModel as BaseModel
|
expressions::{lexer::util::get_tokens as tokenizer, types::Area, utils::number_to_column},
|
||||||
|
types::{CellType, Style},
|
||||||
|
BorderArea, ClipboardData, UserModel as BaseModel,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn to_js_error(error: String) -> JsError {
|
fn to_js_error(error: String) -> JsError {
|
||||||
@@ -671,35 +673,17 @@ impl Model {
|
|||||||
.map_err(|e| to_js_error(e.to_string()))
|
.map_err(|e| to_js_error(e.to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen(js_name = "mergeCells")]
|
#[wasm_bindgen(js_name = "getCellArrayStructure")]
|
||||||
pub fn merge_cells(
|
pub fn get_cell_array_structure(
|
||||||
&mut self,
|
|
||||||
sheet: u32,
|
|
||||||
row: i32,
|
|
||||||
column: i32,
|
|
||||||
width: i32,
|
|
||||||
height: i32,
|
|
||||||
) -> Result<(), JsError> {
|
|
||||||
self.model
|
|
||||||
.merge_cells(sheet, row, column, width, height)
|
|
||||||
.map_err(to_js_error)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[wasm_bindgen(js_name = "unmergeCells")]
|
|
||||||
pub fn unmerge_cells(&mut self, sheet: u32, row: i32, column: i32) -> Result<(), JsError> {
|
|
||||||
self.model
|
|
||||||
.unmerge_cells(sheet, row, column)
|
|
||||||
.map_err(to_js_error)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[wasm_bindgen(js_name = "getCellStructure")]
|
|
||||||
pub fn get_cell_structure(
|
|
||||||
&self,
|
&self,
|
||||||
sheet: u32,
|
sheet: u32,
|
||||||
row: i32,
|
row: i32,
|
||||||
column: i32,
|
column: i32,
|
||||||
) -> Result<JsValue, JsError> {
|
) -> Result<JsValue, JsError> {
|
||||||
let data = self.model.get_cell_structure(sheet, row, column).map_err(|e| to_js_error(e.to_string()))?;
|
let cell_structure = self
|
||||||
serde_wasm_bindgen::to_value(&data).map_err(|e| to_js_error(e.to_string()))
|
.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,6 +109,11 @@ export interface MarkedToken {
|
|||||||
end: number;
|
end: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type CellArrayStructure =
|
||||||
|
| "SingleCell"
|
||||||
|
| { DynamicChild: [number, number, number, number] }
|
||||||
|
| { DynamicMother: [number, number] };
|
||||||
|
|
||||||
export interface WorksheetProperties {
|
export interface WorksheetProperties {
|
||||||
name: string;
|
name: string;
|
||||||
color: string;
|
color: string;
|
||||||
@@ -234,8 +239,3 @@ export interface DefinedName {
|
|||||||
scope?: number;
|
scope?: number;
|
||||||
formula: string;
|
formula: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type CellStructure =
|
|
||||||
| "Simple"
|
|
||||||
| { Merged: { row: number; column: number } }
|
|
||||||
| { MergedRoot: { width: number; height: number } };
|
|
||||||
|
|||||||
1047
webapp/IronCalc/package-lock.json
generated
1047
webapp/IronCalc/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -13,6 +13,7 @@ import type { WorkbookState } from "../workbookState";
|
|||||||
type FormulaBarProps = {
|
type FormulaBarProps = {
|
||||||
cellAddress: string;
|
cellAddress: string;
|
||||||
formulaValue: string;
|
formulaValue: string;
|
||||||
|
isPartOfArray: boolean;
|
||||||
model: Model;
|
model: Model;
|
||||||
workbookState: WorkbookState;
|
workbookState: WorkbookState;
|
||||||
onChange: () => void;
|
onChange: () => void;
|
||||||
@@ -23,6 +24,7 @@ function FormulaBar(properties: FormulaBarProps) {
|
|||||||
const {
|
const {
|
||||||
cellAddress,
|
cellAddress,
|
||||||
formulaValue,
|
formulaValue,
|
||||||
|
isPartOfArray,
|
||||||
model,
|
model,
|
||||||
onChange,
|
onChange,
|
||||||
onTextUpdated,
|
onTextUpdated,
|
||||||
@@ -62,6 +64,9 @@ function FormulaBar(properties: FormulaBarProps) {
|
|||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
}}
|
}}
|
||||||
|
sx={{
|
||||||
|
color: isPartOfArray ? "grey" : "black",
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
<Editor
|
<Editor
|
||||||
originalText={formulaValue}
|
originalText={formulaValue}
|
||||||
|
|||||||
@@ -40,8 +40,6 @@ import {
|
|||||||
ArrowMiddleFromLine,
|
ArrowMiddleFromLine,
|
||||||
DecimalPlacesDecreaseIcon,
|
DecimalPlacesDecreaseIcon,
|
||||||
DecimalPlacesIncreaseIcon,
|
DecimalPlacesIncreaseIcon,
|
||||||
MergeCellsIcon,
|
|
||||||
UnmergeCellsIcon,
|
|
||||||
} from "../../icons";
|
} from "../../icons";
|
||||||
import { theme } from "../../theme";
|
import { theme } from "../../theme";
|
||||||
import BorderPicker from "../BorderPicker/BorderPicker";
|
import BorderPicker from "../BorderPicker/BorderPicker";
|
||||||
@@ -76,8 +74,6 @@ type ToolbarProperties = {
|
|||||||
onClearFormatting: () => void;
|
onClearFormatting: () => void;
|
||||||
onIncreaseFontSize: (delta: number) => void;
|
onIncreaseFontSize: (delta: number) => void;
|
||||||
onDownloadPNG: () => void;
|
onDownloadPNG: () => void;
|
||||||
onMergeCells: () => void;
|
|
||||||
onUnmergeCells: () => void;
|
|
||||||
fillColor: string;
|
fillColor: string;
|
||||||
fontColor: string;
|
fontColor: string;
|
||||||
fontSize: number;
|
fontSize: number;
|
||||||
@@ -433,28 +429,6 @@ function Toolbar(properties: ToolbarProperties) {
|
|||||||
>
|
>
|
||||||
<ImageDown />
|
<ImageDown />
|
||||||
</StyledButton>
|
</StyledButton>
|
||||||
<StyledButton
|
|
||||||
type="button"
|
|
||||||
$pressed={false}
|
|
||||||
disabled={!canEdit}
|
|
||||||
onClick={() => {
|
|
||||||
properties.onMergeCells();
|
|
||||||
}}
|
|
||||||
title={t("toolbar.merge_cells")}
|
|
||||||
>
|
|
||||||
<MergeCellsIcon />
|
|
||||||
</StyledButton>
|
|
||||||
<StyledButton
|
|
||||||
type="button"
|
|
||||||
$pressed={false}
|
|
||||||
disabled={!canEdit}
|
|
||||||
onClick={() => {
|
|
||||||
properties.onUnmergeCells();
|
|
||||||
}}
|
|
||||||
title={t("toolbar.unmerge_cells")}
|
|
||||||
>
|
|
||||||
<UnmergeCellsIcon />
|
|
||||||
</StyledButton>
|
|
||||||
|
|
||||||
<ColorPicker
|
<ColorPicker
|
||||||
color={properties.fontColor}
|
color={properties.fontColor}
|
||||||
|
|||||||
@@ -348,7 +348,28 @@ const Workbook = (props: { model: Model; workbookState: WorkbookState }) => {
|
|||||||
return workbookState.getEditingText();
|
return workbookState.getEditingText();
|
||||||
}
|
}
|
||||||
const { sheet, row, column } = model.getSelectedView();
|
const { sheet, row, column } = model.getSelectedView();
|
||||||
return model.getCellContent(sheet, row, column);
|
const r = model.getCellArrayStructure(sheet, row, column);
|
||||||
|
if (r === "SingleCell") {
|
||||||
|
return model.getCellContent(sheet, row, column);
|
||||||
|
}
|
||||||
|
if ("DynamicMother" in r) {
|
||||||
|
return model.getCellContent(sheet, row, column);
|
||||||
|
}
|
||||||
|
const [mother_row, mother_column, _] = r.DynamicChild;
|
||||||
|
return model.getCellContent(sheet, mother_row, mother_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 getCellStyle = useCallback(() => {
|
||||||
@@ -611,29 +632,6 @@ const Workbook = (props: { model: Model; workbookState: WorkbookState }) => {
|
|||||||
downloadLink.download = "ironcalc.png";
|
downloadLink.download = "ironcalc.png";
|
||||||
downloadLink.click();
|
downloadLink.click();
|
||||||
}}
|
}}
|
||||||
onMergeCells={() => {
|
|
||||||
const {
|
|
||||||
sheet,
|
|
||||||
range: [rowStart, columnStart, rowEnd, columnEnd],
|
|
||||||
} = model.getSelectedView();
|
|
||||||
const row = Math.min(rowStart, rowEnd);
|
|
||||||
const column = Math.min(columnStart, columnEnd);
|
|
||||||
|
|
||||||
const width = Math.abs(columnEnd - columnStart) + 1;
|
|
||||||
const height = Math.abs(rowEnd - rowStart) + 1;
|
|
||||||
model.mergeCells(sheet, row, column, width, height);
|
|
||||||
setRedrawId((id) => id + 1);
|
|
||||||
}}
|
|
||||||
onUnmergeCells={() => {
|
|
||||||
const {
|
|
||||||
sheet,
|
|
||||||
range: [rowStart, columnStart, rowEnd, columnEnd],
|
|
||||||
} = model.getSelectedView();
|
|
||||||
const row = Math.min(rowStart, rowEnd);
|
|
||||||
const column = Math.min(columnStart, columnEnd);
|
|
||||||
model.unmergeCells(sheet, row, column);
|
|
||||||
setRedrawId((id) => id + 1);
|
|
||||||
}}
|
|
||||||
onBorderChanged={(border: BorderOptions): void => {
|
onBorderChanged={(border: BorderOptions): void => {
|
||||||
const {
|
const {
|
||||||
sheet,
|
sheet,
|
||||||
@@ -721,6 +719,7 @@ const Workbook = (props: { model: Model; workbookState: WorkbookState }) => {
|
|||||||
}}
|
}}
|
||||||
model={model}
|
model={model}
|
||||||
workbookState={workbookState}
|
workbookState={workbookState}
|
||||||
|
isPartOfArray={isRootCellOfArray()}
|
||||||
/>
|
/>
|
||||||
<Worksheet
|
<Worksheet
|
||||||
model={model}
|
model={model}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import {
|
|||||||
LAST_COLUMN,
|
LAST_COLUMN,
|
||||||
LAST_ROW,
|
LAST_ROW,
|
||||||
ROW_HEIGH_SCALE,
|
ROW_HEIGH_SCALE,
|
||||||
|
cellArrayStructureColor,
|
||||||
outlineBackgroundColor,
|
outlineBackgroundColor,
|
||||||
outlineColor,
|
outlineColor,
|
||||||
} from "../WorksheetCanvas/constants";
|
} from "../WorksheetCanvas/constants";
|
||||||
@@ -60,6 +61,7 @@ const Worksheet = forwardRef(
|
|||||||
const cellOutline = useRef<HTMLDivElement>(null);
|
const cellOutline = useRef<HTMLDivElement>(null);
|
||||||
const areaOutline = useRef<HTMLDivElement>(null);
|
const areaOutline = useRef<HTMLDivElement>(null);
|
||||||
const cellOutlineHandle = useRef<HTMLDivElement>(null);
|
const cellOutlineHandle = useRef<HTMLDivElement>(null);
|
||||||
|
const cellArrayStructure = useRef<HTMLDivElement>(null);
|
||||||
const extendToOutline = useRef<HTMLDivElement>(null);
|
const extendToOutline = useRef<HTMLDivElement>(null);
|
||||||
const columnResizeGuide = useRef<HTMLDivElement>(null);
|
const columnResizeGuide = useRef<HTMLDivElement>(null);
|
||||||
const rowResizeGuide = useRef<HTMLDivElement>(null);
|
const rowResizeGuide = useRef<HTMLDivElement>(null);
|
||||||
@@ -87,6 +89,7 @@ const Worksheet = forwardRef(
|
|||||||
const outline = cellOutline.current;
|
const outline = cellOutline.current;
|
||||||
const handle = cellOutlineHandle.current;
|
const handle = cellOutlineHandle.current;
|
||||||
const area = areaOutline.current;
|
const area = areaOutline.current;
|
||||||
|
const arrayStructure = cellArrayStructure.current;
|
||||||
const extendTo = extendToOutline.current;
|
const extendTo = extendToOutline.current;
|
||||||
const editor = editorElement.current;
|
const editor = editorElement.current;
|
||||||
|
|
||||||
@@ -101,7 +104,8 @@ const Worksheet = forwardRef(
|
|||||||
!area ||
|
!area ||
|
||||||
!extendTo ||
|
!extendTo ||
|
||||||
!scrollElement.current ||
|
!scrollElement.current ||
|
||||||
!editor
|
!editor ||
|
||||||
|
!arrayStructure
|
||||||
)
|
)
|
||||||
return;
|
return;
|
||||||
// FIXME: This two need to be computed.
|
// FIXME: This two need to be computed.
|
||||||
@@ -119,6 +123,7 @@ const Worksheet = forwardRef(
|
|||||||
columnHeaders: columnHeadersRef,
|
columnHeaders: columnHeadersRef,
|
||||||
cellOutline: outline,
|
cellOutline: outline,
|
||||||
cellOutlineHandle: handle,
|
cellOutlineHandle: handle,
|
||||||
|
cellArrayStructure: arrayStructure,
|
||||||
areaOutline: area,
|
areaOutline: area,
|
||||||
extendToOutline: extendTo,
|
extendToOutline: extendTo,
|
||||||
editor: editor,
|
editor: editor,
|
||||||
@@ -462,6 +467,7 @@ const Worksheet = forwardRef(
|
|||||||
/>
|
/>
|
||||||
</EditorWrapper>
|
</EditorWrapper>
|
||||||
<AreaOutline ref={areaOutline} />
|
<AreaOutline ref={areaOutline} />
|
||||||
|
<CellArrayStructure ref={cellArrayStructure} />
|
||||||
<ExtendToOutline ref={extendToOutline} />
|
<ExtendToOutline ref={extendToOutline} />
|
||||||
<CellOutlineHandle
|
<CellOutlineHandle
|
||||||
ref={cellOutlineHandle}
|
ref={cellOutlineHandle}
|
||||||
@@ -631,6 +637,12 @@ const AreaOutline = styled("div")`
|
|||||||
background-color: ${outlineBackgroundColor};
|
background-color: ${outlineBackgroundColor};
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
const CellArrayStructure = styled("div")`
|
||||||
|
position: absolute;
|
||||||
|
border: 1px solid ${cellArrayStructureColor};
|
||||||
|
border-radius: 3px;
|
||||||
|
`;
|
||||||
|
|
||||||
const CellOutline = styled("div")`
|
const CellOutline = styled("div")`
|
||||||
position: absolute;
|
position: absolute;
|
||||||
border: 2px solid ${outlineColor};
|
border: 2px solid ${outlineColor};
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ export const defaultTextColor = "#2E414D";
|
|||||||
|
|
||||||
export const outlineColor = "#F2994A";
|
export const outlineColor = "#F2994A";
|
||||||
export const outlineBackgroundColor = "#F2994A1A";
|
export const outlineBackgroundColor = "#F2994A1A";
|
||||||
|
export const cellArrayStructureColor = "#64BDFDA1";
|
||||||
|
|
||||||
export const LAST_COLUMN = 16_384;
|
export const LAST_COLUMN = 16_384;
|
||||||
export const LAST_ROW = 1_048_576;
|
export const LAST_ROW = 1_048_576;
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ export interface CanvasSettings {
|
|||||||
cellOutline: HTMLDivElement;
|
cellOutline: HTMLDivElement;
|
||||||
areaOutline: HTMLDivElement;
|
areaOutline: HTMLDivElement;
|
||||||
cellOutlineHandle: HTMLDivElement;
|
cellOutlineHandle: HTMLDivElement;
|
||||||
|
cellArrayStructure: HTMLDivElement;
|
||||||
extendToOutline: HTMLDivElement;
|
extendToOutline: HTMLDivElement;
|
||||||
columnGuide: HTMLDivElement;
|
columnGuide: HTMLDivElement;
|
||||||
rowGuide: HTMLDivElement;
|
rowGuide: HTMLDivElement;
|
||||||
@@ -138,6 +139,8 @@ export default class WorksheetCanvas {
|
|||||||
|
|
||||||
cellOutlineHandle: HTMLDivElement;
|
cellOutlineHandle: HTMLDivElement;
|
||||||
|
|
||||||
|
cellArrayStructure: HTMLDivElement;
|
||||||
|
|
||||||
extendToOutline: HTMLDivElement;
|
extendToOutline: HTMLDivElement;
|
||||||
|
|
||||||
workbookState: WorkbookState;
|
workbookState: WorkbookState;
|
||||||
@@ -170,6 +173,7 @@ export default class WorksheetCanvas {
|
|||||||
|
|
||||||
this.cellOutline = options.elements.cellOutline;
|
this.cellOutline = options.elements.cellOutline;
|
||||||
this.cellOutlineHandle = options.elements.cellOutlineHandle;
|
this.cellOutlineHandle = options.elements.cellOutlineHandle;
|
||||||
|
this.cellArrayStructure = options.elements.cellArrayStructure;
|
||||||
this.areaOutline = options.elements.areaOutline;
|
this.areaOutline = options.elements.areaOutline;
|
||||||
this.extendToOutline = options.elements.extendToOutline;
|
this.extendToOutline = options.elements.extendToOutline;
|
||||||
this.rowGuide = options.elements.rowGuide;
|
this.rowGuide = options.elements.rowGuide;
|
||||||
@@ -386,29 +390,10 @@ export default class WorksheetCanvas {
|
|||||||
column: number,
|
column: number,
|
||||||
x: number,
|
x: number,
|
||||||
y: number,
|
y: number,
|
||||||
width1: number,
|
width: number,
|
||||||
height1: number,
|
height: number,
|
||||||
): void {
|
): void {
|
||||||
const selectedSheet = this.model.getSelectedSheet();
|
const selectedSheet = this.model.getSelectedSheet();
|
||||||
const structure = this.model.getCellStructure(selectedSheet, row, column);
|
|
||||||
if (typeof structure === 'object' && 'Merged' in structure) {
|
|
||||||
// We don't render merged cells
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let width = width1;
|
|
||||||
let height = height1;
|
|
||||||
if (typeof structure === 'object' && 'MergedRoot' in structure) {
|
|
||||||
const root = structure.MergedRoot;
|
|
||||||
const columns = root.width;
|
|
||||||
const rows = root.height;
|
|
||||||
for (let i = 1; i < columns; i += 1) {
|
|
||||||
width += this.getColumnWidth(selectedSheet, column + i);
|
|
||||||
}
|
|
||||||
for (let i = 1; i < rows; i += 1) {
|
|
||||||
height += this.getRowHeight(selectedSheet, row + i);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const style = this.model.getCellStyle(selectedSheet, row, column);
|
const style = this.model.getCellStyle(selectedSheet, row, column);
|
||||||
|
|
||||||
let backgroundColor = "#FFFFFF";
|
let backgroundColor = "#FFFFFF";
|
||||||
@@ -1264,16 +1249,20 @@ export default class WorksheetCanvas {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private drawCellOutline(): void {
|
private drawCellOutline(): void {
|
||||||
const { cellOutline, areaOutline, cellOutlineHandle } = this;
|
const { cellArrayStructure, cellOutline, areaOutline, cellOutlineHandle } =
|
||||||
|
this;
|
||||||
if (this.workbookState.getEditingCell()) {
|
if (this.workbookState.getEditingCell()) {
|
||||||
cellOutline.style.visibility = "hidden";
|
cellOutline.style.visibility = "hidden";
|
||||||
cellOutlineHandle.style.visibility = "hidden";
|
cellOutlineHandle.style.visibility = "hidden";
|
||||||
areaOutline.style.visibility = "hidden";
|
areaOutline.style.visibility = "hidden";
|
||||||
|
cellArrayStructure.style.visibility = "hidden";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
cellOutline.style.visibility = "visible";
|
cellOutline.style.visibility = "visible";
|
||||||
cellOutlineHandle.style.visibility = "visible";
|
cellOutlineHandle.style.visibility = "visible";
|
||||||
areaOutline.style.visibility = "visible";
|
areaOutline.style.visibility = "visible";
|
||||||
|
// This one is hidden by default
|
||||||
|
cellArrayStructure.style.visibility = "hidden";
|
||||||
|
|
||||||
const [selectedSheet, selectedRow, selectedColumn] =
|
const [selectedSheet, selectedRow, selectedColumn] =
|
||||||
this.model.getSelectedCell();
|
this.model.getSelectedCell();
|
||||||
@@ -1329,6 +1318,34 @@ export default class WorksheetCanvas {
|
|||||||
[handleX, handleY] = this.getCoordinatesByCell(rowStart, columnStart);
|
[handleX, handleY] = this.getCoordinatesByCell(rowStart, columnStart);
|
||||||
handleX += this.getColumnWidth(selectedSheet, columnStart);
|
handleX += this.getColumnWidth(selectedSheet, columnStart);
|
||||||
handleY += this.getRowHeight(selectedSheet, rowStart);
|
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 {
|
} else {
|
||||||
areaOutline.style.visibility = "visible";
|
areaOutline.style.visibility = "visible";
|
||||||
cellOutlineHandle.style.visibility = "visible";
|
cellOutlineHandle.style.visibility = "visible";
|
||||||
|
|||||||
@@ -23,9 +23,6 @@ import InsertRowBelow from "./insert-row-below.svg?react";
|
|||||||
import IronCalcIcon from "./ironcalc_icon.svg?react";
|
import IronCalcIcon from "./ironcalc_icon.svg?react";
|
||||||
import IronCalcLogo from "./orange+black.svg?react";
|
import IronCalcLogo from "./orange+black.svg?react";
|
||||||
|
|
||||||
import MergeCellsIcon from "./merge-cells.svg?react";
|
|
||||||
import UnmergeCellsIcon from "./unmerge-cells.svg?react";
|
|
||||||
|
|
||||||
import Fx from "./fx.svg?react";
|
import Fx from "./fx.svg?react";
|
||||||
|
|
||||||
export {
|
export {
|
||||||
@@ -50,7 +47,5 @@ export {
|
|||||||
InsertRowBelow,
|
InsertRowBelow,
|
||||||
IronCalcIcon,
|
IronCalcIcon,
|
||||||
IronCalcLogo,
|
IronCalcLogo,
|
||||||
MergeCellsIcon,
|
|
||||||
UnmergeCellsIcon,
|
|
||||||
Fx,
|
Fx,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,4 +0,0 @@
|
|||||||
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
||||||
<path d="M8 2L3.33333 2C2.59695 2 2 2.59695 2 3.33333L2 5M8 2L12.6667 2C13.403 2 14 2.59695 14 3.33333L14 5M8 2L8 5M8 14L12.6667 14C13.403 14 14 13.403 14 12.6667L14 11M8 14L3.33333 14C2.59695 14 2 13.403 2 12.6667L2 11M8 14L8 11M2 5L2 11M2 5L14 5M2 11L14 11M14 5L14 11" stroke="#333333" stroke-linecap="round" stroke-linejoin="round"/>
|
|
||||||
<path d="M5 8L11 8M5 8L6 9L6 7L5 8ZM11 8L10 7L10 9L11 8Z" stroke="#333333" stroke-linecap="round" stroke-linejoin="round"/>
|
|
||||||
</svg>
|
|
||||||
|
Before Width: | Height: | Size: 564 B |
@@ -1,5 +0,0 @@
|
|||||||
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
||||||
<path d="M8 2L3.33333 2C2.59695 2 2 2.59695 2 3.33333L2 5M8 2L12.6667 2C13.403 2 14 2.59695 14 3.33333L14 5M8 2L8 5M8 14L12.6667 14C13.403 14 14 13.403 14 12.6667L14 11M8 14L3.33333 14C2.59695 14 2 13.403 2 12.6667L2 11M8 14L8 11M2 5L2 11M2 5L14 5M2 11L14 11M14 5L14 11" stroke="#333333" stroke-linecap="round" stroke-linejoin="round"/>
|
|
||||||
<path d="M2 8L6 8M6 8L5 7L5 9L6 8Z" stroke="#333333" stroke-linecap="round" stroke-linejoin="round"/>
|
|
||||||
<path d="M14 8L10 8M10 8L11 7L11 9L10 8Z" stroke="#333333" stroke-linecap="round" stroke-linejoin="round"/>
|
|
||||||
</svg>
|
|
||||||
|
Before Width: | Height: | Size: 650 B |
@@ -27,8 +27,6 @@
|
|||||||
"vertical_align_top": "Align top",
|
"vertical_align_top": "Align top",
|
||||||
"selected_png": "Export Selected area as PNG",
|
"selected_png": "Export Selected area as PNG",
|
||||||
"wrap_text": "Wrap text",
|
"wrap_text": "Wrap text",
|
||||||
"merge_cells": "Merge cells",
|
|
||||||
"unmerge_cells": "Unmerge cells",
|
|
||||||
"format_menu": {
|
"format_menu": {
|
||||||
"auto": "Auto",
|
"auto": "Auto",
|
||||||
"number": "Number",
|
"number": "Number",
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ use itertools::Itertools;
|
|||||||
|
|
||||||
use ironcalc_base::{
|
use ironcalc_base::{
|
||||||
expressions::{
|
expressions::{
|
||||||
parser::{stringify::to_excel_string, Node},
|
parser::{static_analysis::StaticResult, stringify::to_excel_string, Node},
|
||||||
types::CellReferenceRC,
|
types::CellReferenceRC,
|
||||||
utils::number_to_column,
|
utils::number_to_column,
|
||||||
},
|
},
|
||||||
@@ -56,7 +56,7 @@ fn get_formula_attribute(
|
|||||||
|
|
||||||
pub(crate) fn get_worksheet_xml(
|
pub(crate) fn get_worksheet_xml(
|
||||||
worksheet: &Worksheet,
|
worksheet: &Worksheet,
|
||||||
parsed_formulas: &[Node],
|
parsed_formulas: &[(Node, StaticResult)],
|
||||||
dimension: &str,
|
dimension: &str,
|
||||||
is_sheet_selected: bool,
|
is_sheet_selected: bool,
|
||||||
) -> String {
|
) -> String {
|
||||||
@@ -104,7 +104,7 @@ pub(crate) fn get_worksheet_xml(
|
|||||||
let style = get_cell_style_attribute(*s);
|
let style = get_cell_style_attribute(*s);
|
||||||
row_data_str.push(format!("<c r=\"{cell_name}\"{style}/>"));
|
row_data_str.push(format!("<c r=\"{cell_name}\"{style}/>"));
|
||||||
}
|
}
|
||||||
Cell::BooleanCell { v, s } => {
|
Cell::SpillBooleanCell { v, s, .. } | Cell::BooleanCell { v, s } => {
|
||||||
// <c r="A8" t="b" s="1">
|
// <c r="A8" t="b" s="1">
|
||||||
// <v>1</v>
|
// <v>1</v>
|
||||||
// </c>
|
// </c>
|
||||||
@@ -114,7 +114,7 @@ pub(crate) fn get_worksheet_xml(
|
|||||||
"<c r=\"{cell_name}\" t=\"b\"{style}><v>{b}</v></c>"
|
"<c r=\"{cell_name}\" t=\"b\"{style}><v>{b}</v></c>"
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
Cell::NumberCell { v, s } => {
|
Cell::SpillNumberCell { v, s, .. } | Cell::NumberCell { v, s } => {
|
||||||
// Normally the type number is left out. Example:
|
// Normally the type number is left out. Example:
|
||||||
// <c r="C6" s="1">
|
// <c r="C6" s="1">
|
||||||
// <v>3</v>
|
// <v>3</v>
|
||||||
@@ -122,7 +122,7 @@ pub(crate) fn get_worksheet_xml(
|
|||||||
let style = get_cell_style_attribute(*s);
|
let style = get_cell_style_attribute(*s);
|
||||||
row_data_str.push(format!("<c r=\"{cell_name}\"{style}><v>{v}</v></c>"));
|
row_data_str.push(format!("<c r=\"{cell_name}\"{style}><v>{v}</v></c>"));
|
||||||
}
|
}
|
||||||
Cell::ErrorCell { ei, s } => {
|
Cell::SpillErrorCell { ei, s, .. } | Cell::ErrorCell { ei, s } => {
|
||||||
let style = get_cell_style_attribute(*s);
|
let style = get_cell_style_attribute(*s);
|
||||||
row_data_str.push(format!(
|
row_data_str.push(format!(
|
||||||
"<c r=\"{cell_name}\" t=\"e\"{style}><v>{ei}</v></c>"
|
"<c r=\"{cell_name}\" t=\"e\"{style}><v>{ei}</v></c>"
|
||||||
@@ -153,7 +153,7 @@ pub(crate) fn get_worksheet_xml(
|
|||||||
worksheet.get_name(),
|
worksheet.get_name(),
|
||||||
*row_index,
|
*row_index,
|
||||||
*column_index,
|
*column_index,
|
||||||
&parsed_formulas[*f as usize],
|
&parsed_formulas[*f as usize].0,
|
||||||
);
|
);
|
||||||
|
|
||||||
let b = i32::from(*v);
|
let b = i32::from(*v);
|
||||||
@@ -172,7 +172,7 @@ pub(crate) fn get_worksheet_xml(
|
|||||||
worksheet.get_name(),
|
worksheet.get_name(),
|
||||||
*row_index,
|
*row_index,
|
||||||
*column_index,
|
*column_index,
|
||||||
&parsed_formulas[*f as usize],
|
&parsed_formulas[*f as usize].0,
|
||||||
);
|
);
|
||||||
let style = get_cell_style_attribute(*s);
|
let style = get_cell_style_attribute(*s);
|
||||||
|
|
||||||
@@ -189,14 +189,14 @@ pub(crate) fn get_worksheet_xml(
|
|||||||
worksheet.get_name(),
|
worksheet.get_name(),
|
||||||
*row_index,
|
*row_index,
|
||||||
*column_index,
|
*column_index,
|
||||||
&parsed_formulas[*f as usize],
|
&parsed_formulas[*f as usize].0,
|
||||||
);
|
);
|
||||||
let style = get_cell_style_attribute(*s);
|
let style = get_cell_style_attribute(*s);
|
||||||
let escaped_v = escape_xml(v);
|
let escaped_v = escape_xml(v);
|
||||||
|
|
||||||
row_data_str.push(format!(
|
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 {
|
Cell::CellFormulaError {
|
||||||
f,
|
f,
|
||||||
@@ -213,14 +213,135 @@ pub(crate) fn get_worksheet_xml(
|
|||||||
worksheet.get_name(),
|
worksheet.get_name(),
|
||||||
*row_index,
|
*row_index,
|
||||||
*column_index,
|
*column_index,
|
||||||
&parsed_formulas[*f as usize],
|
&parsed_formulas[*f as usize].0,
|
||||||
);
|
);
|
||||||
let style = get_cell_style_attribute(*s);
|
let style = get_cell_style_attribute(*s);
|
||||||
row_data_str.push(format!(
|
row_data_str.push(format!(
|
||||||
"<c r=\"{cell_name}\" t=\"e\"{style}><f>{formula}</f><v>{ei}</v></c>"
|
"<c r=\"{cell_name}\" t=\"e\"{style}><f>{formula}</f><v>{ei}</v></c>"
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
Cell::Merged { .. } => { /* do nothing */ }
|
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) {
|
let row_style_str = match row_style_dict.get(row_index) {
|
||||||
@@ -248,7 +369,7 @@ pub(crate) fn get_worksheet_xml(
|
|||||||
}
|
}
|
||||||
let sheet_data = sheet_data_str.join("");
|
let sheet_data = sheet_data_str.join("");
|
||||||
|
|
||||||
for merge_cell_ref in &worksheet.merged_cells {
|
for merge_cell_ref in &worksheet.merge_cells {
|
||||||
merged_cells_str.push(format!("<mergeCell ref=\"{merge_cell_ref}\"/>"))
|
merged_cells_str.push(format!("<mergeCell ref=\"{merge_cell_ref}\"/>"))
|
||||||
}
|
}
|
||||||
let merged_cells_count = merged_cells_str.len();
|
let merged_cells_count = merged_cells_str.len();
|
||||||
|
|||||||
@@ -306,13 +306,15 @@ fn from_a1_to_rc(
|
|||||||
context: String,
|
context: String,
|
||||||
tables: HashMap<String, Table>,
|
tables: HashMap<String, Table>,
|
||||||
defined_names: Vec<DefinedNameS>,
|
defined_names: Vec<DefinedNameS>,
|
||||||
|
is_array: bool,
|
||||||
) -> Result<String, XlsxError> {
|
) -> Result<String, XlsxError> {
|
||||||
let mut parser = Parser::new(worksheets.to_owned(), defined_names, tables);
|
let mut parser = Parser::new(worksheets.to_owned(), defined_names, tables);
|
||||||
let cell_reference =
|
let cell_reference =
|
||||||
parse_reference(&context).map_err(|error| XlsxError::Xml(error.to_string()))?;
|
parse_reference(&context).map_err(|error| XlsxError::Xml(error.to_string()))?;
|
||||||
let mut t = parser.parse(&formula, &cell_reference);
|
let mut t = parser.parse(&formula, &cell_reference);
|
||||||
add_implicit_intersection(&mut t, true);
|
if !is_array {
|
||||||
|
add_implicit_intersection(&mut t, true);
|
||||||
|
}
|
||||||
Ok(to_rc_format(&t))
|
Ok(to_rc_format(&t))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -827,6 +829,7 @@ pub(super) fn load_sheet<R: Read + std::io::Seek>(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let cell_metadata = cell.attribute("cm");
|
let cell_metadata = cell.attribute("cm");
|
||||||
|
let is_dynamic_array = cell_metadata == Some("1");
|
||||||
|
|
||||||
// type, the default type being "n" for number
|
// type, the default type being "n" for number
|
||||||
// If the cell does not have a value is an empty cell
|
// If the cell does not have a value is an empty cell
|
||||||
@@ -893,6 +896,7 @@ pub(super) fn load_sheet<R: Read + std::io::Seek>(
|
|||||||
context,
|
context,
|
||||||
tables.clone(),
|
tables.clone(),
|
||||||
defined_names.clone(),
|
defined_names.clone(),
|
||||||
|
is_dynamic_array,
|
||||||
)?;
|
)?;
|
||||||
match index_map.get(&si) {
|
match index_map.get(&si) {
|
||||||
Some(index) => {
|
Some(index) => {
|
||||||
@@ -941,7 +945,6 @@ pub(super) fn load_sheet<R: Read + std::io::Seek>(
|
|||||||
return Err(XlsxError::NotImplemented("data table formulas".to_string()));
|
return Err(XlsxError::NotImplemented("data table formulas".to_string()));
|
||||||
}
|
}
|
||||||
"array" | "normal" => {
|
"array" | "normal" => {
|
||||||
let is_dynamic_array = cell_metadata == Some("1");
|
|
||||||
if formula_type == "array" && !is_dynamic_array {
|
if formula_type == "array" && !is_dynamic_array {
|
||||||
// Dynamic formulas in Excel are formulas of type array with the cm=1, those we support.
|
// 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
|
// On the other hand the old CSE formulas or array formulas are not supported in IronCalc for the time being
|
||||||
@@ -956,6 +959,7 @@ pub(super) fn load_sheet<R: Read + std::io::Seek>(
|
|||||||
context,
|
context,
|
||||||
tables.clone(),
|
tables.clone(),
|
||||||
defined_names.clone(),
|
defined_names.clone(),
|
||||||
|
is_dynamic_array,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
match get_formula_index(&formula, &shared_formulas) {
|
match get_formula_index(&formula, &shared_formulas) {
|
||||||
@@ -989,7 +993,7 @@ pub(super) fn load_sheet<R: Read + std::io::Seek>(
|
|||||||
sheet_data.insert(row_index, data_row);
|
sheet_data.insert(row_index, data_row);
|
||||||
}
|
}
|
||||||
|
|
||||||
let merged_cells = load_merged_cells(ws)?;
|
let merge_cells = load_merge_cells(ws)?;
|
||||||
|
|
||||||
// Conditional Formatting
|
// Conditional Formatting
|
||||||
// <conditionalFormatting sqref="B1:B9">
|
// <conditionalFormatting sqref="B1:B9">
|
||||||
@@ -1028,7 +1032,7 @@ pub(super) fn load_sheet<R: Read + std::io::Seek>(
|
|||||||
sheet_id,
|
sheet_id,
|
||||||
state: state.to_owned(),
|
state: state.to_owned(),
|
||||||
color,
|
color,
|
||||||
merged_cells,
|
merge_cells,
|
||||||
comments: settings.comments,
|
comments: settings.comments,
|
||||||
frozen_rows: sheet_view.frozen_rows,
|
frozen_rows: sheet_view.frozen_rows,
|
||||||
frozen_columns: sheet_view.frozen_columns,
|
frozen_columns: sheet_view.frozen_columns,
|
||||||
|
|||||||
BIN
xlsx/tests/calc_tests/simple_spill.xlsx
Normal file
BIN
xlsx/tests/calc_tests/simple_spill.xlsx
Normal file
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user