FIX: Make XOR, OR, AND functions more consistent with Excel

The way these functions interpret their arguments is inconsistent with
Excel in a few ways:

- EmptyCell: Excel ignores arguments evaluating to these types of
  values, treating them as if they didn't exist.

- Text: Text cells are ignored unless they are "TRUE" or "FALSE" (case
  insensitive). EXCEPT if the string value comes from a reference, in
  which case it is always ignored regardless of its value.

- Error if no args: Excel returns a #VALUE! error for these functions if
  no arguments are provided, or if all arguments are ignored (see
  above).

- EmptyArg: Bizarrely, Unlike EmptyCell, EmptyArg is not ignored and is
  treated as if it were FALSE by Excel.

- ErrorPropagation: Excel propagates errors in the arguments and in
  cells belonging to any Range arguments.

Additionally, these functions are not consistent with each other, XOR,
OR, AND vary in how they handle the cases mentioned above.

Rectify these consistency issues by re-implementing them all in terms of
a single base function which is more consistent with Excel behavior.
This commit is contained in:
Gian Hancock
2024-12-26 23:39:02 +11:00
committed by Nicolás Hatcher Andrés
parent 8ba30fde33
commit 655d663590

View File

@@ -66,91 +66,61 @@ impl Model {
} }
pub(crate) fn fn_and(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult { pub(crate) fn fn_and(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
let mut true_count = 0; self.logical_nary(
for arg in args { args,
match self.evaluate_node_in_context(arg, cell) {
CalcResult::Boolean(b) => {
if !b {
return CalcResult::Boolean(false);
}
true_count += 1;
}
CalcResult::Number(value) => {
if value == 0.0 {
return CalcResult::Boolean(false);
}
true_count += 1;
}
CalcResult::String(_value) => {
true_count += 1;
}
CalcResult::Range { left, right } => {
if left.sheet != right.sheet {
return CalcResult::new_error(
Error::VALUE,
cell, cell,
"Ranges are in different sheets".to_string(), |acc, value| acc.unwrap_or(true) && value,
); Some(false),
} )
for row in left.row..(right.row + 1) {
for column in left.column..(right.column + 1) {
match self.evaluate_cell(CellReferenceIndex {
sheet: left.sheet,
row,
column,
}) {
CalcResult::Boolean(b) => {
if !b {
return CalcResult::Boolean(false);
}
true_count += 1;
}
CalcResult::Number(value) => {
if value == 0.0 {
return CalcResult::Boolean(false);
}
true_count += 1;
}
CalcResult::String(_value) => {
true_count += 1;
}
error @ CalcResult::Error { .. } => return error,
CalcResult::Range { .. } => {}
CalcResult::EmptyCell | CalcResult::EmptyArg => {}
}
}
}
}
error @ CalcResult::Error { .. } => return error,
CalcResult::EmptyCell | CalcResult::EmptyArg => {}
};
}
if true_count == 0 {
return CalcResult::new_error(
Error::VALUE,
cell,
"Boolean values not found".to_string(),
);
}
CalcResult::Boolean(true)
} }
pub(crate) fn fn_or(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult { pub(crate) fn fn_or(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
self.logical_nary(
args,
cell,
|acc, value| acc.unwrap_or(false) || value,
Some(true),
)
}
pub(crate) fn fn_xor(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
self.logical_nary(args, cell, |acc, value| acc.unwrap_or(false) ^ value, None)
}
/// Base function for AND, OR, XOR. These are all n-ary functions that perform a boolean operation on a series of
/// boolean values. These boolean values are sourced from `args`. Note that there is not a 1-1 relationship between
/// arguments and boolean values evaluated (see how Ranges are handled for example).
///
/// Each argument in `args` is evaluated and the resulting value is interpreted as a boolean as follows:
/// - Boolean: The value is used directly.
/// - Number: 0 is FALSE, all other values are TRUE.
/// - Range: Each cell in the range is evaluated as if they were individual arguments with some caveats
/// - Empty arg: FALSE
/// - Empty cell & String: Ignored, behaves exactly like the argument wasn't passed in at all
/// - Error: Propagated
///
/// If no arguments are provided, or all arguments are ignored, the function returns a #VALUE! error
///
/// **`fold_fn`:** The function that combines the running result with the next value boolean value. The running result
/// starts as `None`.
///
/// **`short_circuit_value`:** If the running result reaches `short_circuit_value`, the function returns early.
fn logical_nary(
&mut self,
args: &[Node],
cell: CellReferenceIndex,
fold_fn: fn(Option<bool>, bool) -> bool,
short_circuit_value: Option<bool>,
) -> CalcResult {
if args.is_empty() { if args.is_empty() {
return CalcResult::new_args_number_error(cell); return CalcResult::new_args_number_error(cell);
} }
let mut result = false;
let mut result = None;
for arg in args { for arg in args {
match self.evaluate_node_in_context(arg, cell) { match self.evaluate_node_in_context(arg, cell) {
CalcResult::Boolean(value) => result = value || result, CalcResult::Boolean(value) => result = Some(fold_fn(result, value)),
CalcResult::Number(value) => { CalcResult::Number(value) => result = Some(fold_fn(result, value != 0.0)),
if value != 0.0 {
return CalcResult::Boolean(true);
}
}
CalcResult::String(_value) => {
return CalcResult::Boolean(true);
}
CalcResult::Range { left, right } => { CalcResult::Range { left, right } => {
if left.sheet != right.sheet { if left.sheet != right.sheet {
return CalcResult::new_error( return CalcResult::new_error(
@@ -166,94 +136,58 @@ impl Model {
row, row,
column, column,
}) { }) {
CalcResult::Boolean(value) => { CalcResult::Boolean(value) => result = Some(fold_fn(result, value)),
result = value || result;
}
CalcResult::Number(value) => { CalcResult::Number(value) => {
if value != 0.0 { result = Some(fold_fn(result, value != 0.0))
return CalcResult::Boolean(true);
}
}
CalcResult::String(_value) => {
return CalcResult::Boolean(true);
} }
error @ CalcResult::Error { .. } => return error, error @ CalcResult::Error { .. } => return error,
CalcResult::Range { .. } => {} CalcResult::EmptyArg => {} // unreachable
CalcResult::EmptyCell | CalcResult::EmptyArg => {} CalcResult::Range { .. }
| CalcResult::String { .. }
| CalcResult::EmptyCell => {}
}
if let (Some(current_result), Some(short_circuit_value)) =
(result, short_circuit_value)
{
if current_result == short_circuit_value {
return CalcResult::Boolean(current_result);
}
} }
} }
} }
} }
error @ CalcResult::Error { .. } => return error, error @ CalcResult::Error { .. } => return error,
CalcResult::EmptyCell | CalcResult::EmptyArg => {} CalcResult::EmptyArg => result = Some(result.unwrap_or(false)),
}; // Strings are ignored unless they are "TRUE" or "FALSE" (case insensitive). EXCEPT if the string value
// comes from a reference, in which case it is always ignored regardless of its value.
CalcResult::String(..) => {
if !matches!(arg, Node::ReferenceKind { .. }) {
if let Ok(f) = self.get_boolean(arg, cell) {
result = Some(fold_fn(result, f));
} }
CalcResult::Boolean(result) }
}
// References to empty cells are ignored. If all args are ignored the result is #VALUE!
CalcResult::EmptyCell => {}
} }
/// XOR(logical1, [logical]*,...) if let (Some(current_result), Some(short_circuit_value)) = (result, short_circuit_value)
/// Logical1 is required, subsequent logical values are optional. Can be logical values, arrays, or references. {
/// The result of XOR is TRUE when the number of TRUE inputs is odd and FALSE when the number of TRUE inputs is even. if current_result == short_circuit_value {
pub(crate) fn fn_xor(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult { return CalcResult::Boolean(current_result);
let mut true_count = 0; }
let mut false_count = 0; }
for arg in args { }
match self.evaluate_node_in_context(arg, cell) {
CalcResult::Boolean(b) => { if let Some(result) = result {
if b { CalcResult::Boolean(result)
true_count += 1;
} else { } else {
false_count += 1; CalcResult::new_error(
}
}
CalcResult::Number(value) => {
if value != 0.0 {
true_count += 1;
} else {
false_count += 1;
}
}
CalcResult::Range { left, right } => {
if left.sheet != right.sheet {
return CalcResult::new_error(
Error::VALUE, Error::VALUE,
cell, cell,
"Ranges are in different sheets".to_string(), "No logical values in argument list".to_string(),
); )
} }
for row in left.row..(right.row + 1) {
for column in left.column..(right.column + 1) {
match self.evaluate_cell(CellReferenceIndex {
sheet: left.sheet,
row,
column,
}) {
CalcResult::Boolean(b) => {
if b {
true_count += 1;
} else {
false_count += 1;
}
}
CalcResult::Number(value) => {
if value != 0.0 {
true_count += 1;
} else {
false_count += 1;
}
}
_ => {}
}
}
}
}
_ => {}
};
}
if true_count == 0 && false_count == 0 {
return CalcResult::new_error(Error::VALUE, cell, "No booleans found".to_string());
}
CalcResult::Boolean(true_count % 2 == 1)
} }
/// =SWITCH(expression, case1, value1, [case, value]*, [default]) /// =SWITCH(expression, case1, value1, [case, value]*, [default])