Skip to content

Commit

Permalink
UPDATE: Implement the implicit Intersection Operator
Browse files Browse the repository at this point in the history
The II operator takes a range and returns a single cell that is in the same column or the same row
as the present cell.

This is needed for backwards compatibility with old Excel models and as a first step towards dynamic arrays.

In the past Excel would evaluate `=A1:A10` in cell `C3` as `A3`, but today in results in an array containing all
values in the range. To be compatible with old workbooks Excel inserts the II operator
on those cases.

So this PR performs an static analysis on all formulas inserting on import automatically the II operator
where necessary. This we call the _automatic implicit operator_. When exporting to Excel the operator is striped away.
You can also manually use the II. For instance `=SUM(@A1:A10)` in cell `C3`.
This was not possible before and such a formula would break backwards compatibility with Excel. To Excel that "non automatic"
form of the II is exported as `_xlfn.SINGLE()`.

Th static analysis has to be done for all arithmetic operations and all functions.
This is a bit of a daunting task and it is not done fully in this PR. We also need to implement arrays and dynamic arrays.
My believe is that once the core operations have been implemented we can go formula by formula writing proper tests and documentation.

After this PR formulas like `=A1:A10` for instance will return `#N/IMPL!` instead of performing the implicit intersection
  • Loading branch information
nhatcher committed Mar 3, 2025
1 parent 9076304 commit da017b6
Show file tree
Hide file tree
Showing 31 changed files with 1,623 additions and 694 deletions.
52 changes: 41 additions & 11 deletions base/src/actions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::constants::{LAST_COLUMN, LAST_ROW};
use crate::expressions::parser::stringify::DisplaceData;
use crate::expressions::parser::stringify::{to_string, to_string_displaced, DisplaceData};
use crate::expressions::types::CellReferenceRC;
use crate::model::Model;

// NOTE: There is a difference with Excel behaviour when deleting cells/rows/columns
Expand All @@ -8,16 +9,45 @@ use crate::model::Model;
// I feel this is unimportant for now.

impl Model {
fn shift_cell_formula(
&mut self,
sheet: u32,
row: i32,
column: i32,
displace_data: &DisplaceData,
) -> Result<(), String> {
if let Some(f) = self
.workbook
.worksheet(sheet)?
.cell(row, column)
.and_then(|c| c.get_formula())
{
let node = &self.parsed_formulas[sheet as usize][f as usize].clone();
let cell_reference = CellReferenceRC {
sheet: self.workbook.worksheets[sheet as usize].get_name(),
row,
column,
};
// FIXME: This is not a very performant way if the formula has changed :S.
let formula = to_string(node, &cell_reference);
let formula_displaced = to_string_displaced(node, &cell_reference, displace_data);
if formula != formula_displaced {
self.update_cell_with_formula(sheet, row, column, format!("={formula_displaced}"))?;
}
}
Ok(())
}
/// This function iterates over all cells in the model and shifts their formulas according to the displacement data.
///
/// # Arguments
///
/// * `displace_data` - A reference to `DisplaceData` describing the displacement's direction and magnitude.
fn displace_cells(&mut self, displace_data: &DisplaceData) {
fn displace_cells(&mut self, displace_data: &DisplaceData) -> Result<(), String> {
let cells = self.get_all_cells();
for cell in cells {
self.shift_cell_formula(cell.index, cell.row, cell.column, displace_data);
self.shift_cell_formula(cell.index, cell.row, cell.column, displace_data)?;
}
Ok(())
}

/// Retrieves the column indices for a specific row in a given sheet, sorted in ascending or descending order.
Expand Down Expand Up @@ -134,7 +164,7 @@ impl Model {
column,
delta: column_count,
}),
);
)?;

// In the list of columns:
// * Keep all the columns to the left
Expand Down Expand Up @@ -214,7 +244,7 @@ impl Model {
column,
delta: -column_count,
}),
);
)?;
let worksheet = &mut self.workbook.worksheet_mut(sheet)?;

// deletes all the column styles
Expand Down Expand Up @@ -338,7 +368,7 @@ impl Model {
row,
delta: row_count,
}),
);
)?;

Ok(())
}
Expand Down Expand Up @@ -399,7 +429,7 @@ impl Model {
row,
delta: -row_count,
}),
);
)?;
Ok(())
}

Expand All @@ -420,14 +450,14 @@ impl Model {
sheet: u32,
column: i32,
delta: i32,
) -> Result<(), &'static str> {
) -> Result<(), String> {
// Check boundaries
let target_column = column + delta;
if !(1..=LAST_COLUMN).contains(&target_column) {
return Err("Target column out of boundaries");
return Err("Target column out of boundaries".to_string());
}
if !(1..=LAST_COLUMN).contains(&column) {
return Err("Initial column out of boundaries");
return Err("Initial column out of boundaries".to_string());
}

// TODO: Add the actual displacement of data and styles
Expand All @@ -439,7 +469,7 @@ impl Model {
column,
delta,
}),
);
)?;

Ok(())
}
Expand Down
55 changes: 15 additions & 40 deletions base/src/cast.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{
calc_result::{CalcResult, Range},
expressions::{parser::Node, token::Error, types::CellReferenceIndex},
implicit_intersection::implicit_intersection,
model::Model,
};

Expand Down Expand Up @@ -39,19 +38,11 @@ impl Model {
}
CalcResult::EmptyCell | CalcResult::EmptyArg => Ok(0.0),
error @ CalcResult::Error { .. } => Err(error),
CalcResult::Range { left, right } => {
match implicit_intersection(&cell, &Range { left, right }) {
Some(cell_reference) => {
let result = self.evaluate_cell(cell_reference);
self.cast_to_number(result, cell_reference)
}
None => Err(CalcResult::Error {
error: Error::VALUE,
origin: cell,
message: "Invalid reference (number)".to_string(),
}),
}
}
CalcResult::Range { .. } => Err(CalcResult::Error {
error: Error::NIMPL,
origin: cell,
message: "Arrays not supported yet".to_string(),
}),
}
}

Expand Down Expand Up @@ -99,19 +90,11 @@ impl Model {
}
CalcResult::EmptyCell | CalcResult::EmptyArg => Ok("".to_string()),
error @ CalcResult::Error { .. } => Err(error),
CalcResult::Range { left, right } => {
match implicit_intersection(&cell, &Range { left, right }) {
Some(cell_reference) => {
let result = self.evaluate_cell(cell_reference);
self.cast_to_string(result, cell_reference)
}
None => Err(CalcResult::Error {
error: Error::VALUE,
origin: cell,
message: "Invalid reference (string)".to_string(),
}),
}
}
CalcResult::Range { .. } => Err(CalcResult::Error {
error: Error::NIMPL,
origin: cell,
message: "Arrays not supported yet".to_string(),
}),
}
}

Expand Down Expand Up @@ -151,19 +134,11 @@ impl Model {
CalcResult::Boolean(b) => Ok(b),
CalcResult::EmptyCell | CalcResult::EmptyArg => Ok(false),
error @ CalcResult::Error { .. } => Err(error),
CalcResult::Range { left, right } => {
match implicit_intersection(&cell, &Range { left, right }) {
Some(cell_reference) => {
let result = self.evaluate_cell(cell_reference);
self.cast_to_bool(result, cell_reference)
}
None => Err(CalcResult::Error {
error: Error::VALUE,
origin: cell,
message: "Invalid reference (bool)".to_string(),
}),
}
}
CalcResult::Range { .. } => Err(CalcResult::Error {
error: Error::NIMPL,
origin: cell,
message: "Arrays not supported yet".to_string(),
}),
}
}

Expand Down
138 changes: 0 additions & 138 deletions base/src/diffs.rs

This file was deleted.

1 change: 1 addition & 0 deletions base/src/expressions/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ impl Lexer {
']' => TokenType::RightBracket,
':' => TokenType::Colon,
';' => TokenType::Semicolon,
'@' => TokenType::At,
',' => {
if self.locale.numbers.symbols.decimal == "," {
match self.consume_number(',') {
Expand Down
1 change: 1 addition & 0 deletions base/src/expressions/lexer/test/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod test_common;
mod test_implicit_intersection;
mod test_language;
mod test_locale;
mod test_ranges;
Expand Down
25 changes: 25 additions & 0 deletions base/src/expressions/lexer/test/test_implicit_intersection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#![allow(clippy::unwrap_used)]

use crate::expressions::{
lexer::{Lexer, LexerMode},
token::TokenType::*,
};
use crate::language::get_language;
use crate::locale::get_locale;

fn new_lexer(formula: &str) -> Lexer {
let locale = get_locale("en").unwrap();
let language = get_language("en").unwrap();
Lexer::new(formula, LexerMode::A1, locale, language)
}

#[test]
fn sum_implicit_intersection() {
let mut lx = new_lexer("sum(@A1:A3)");
assert_eq!(lx.next_token(), Ident("sum".to_string()));
assert_eq!(lx.next_token(), LeftParenthesis);
assert_eq!(lx.next_token(), At);
assert!(matches!(lx.next_token(), Range { .. }));
assert_eq!(lx.next_token(), RightParenthesis);
assert_eq!(lx.next_token(), EOF);
}
Loading

0 comments on commit da017b6

Please sign in to comment.