Adding new example counting formulas and errors (#6)
This commit is contained in:
46
base/examples/formulas_and_errors.rs
Normal file
46
base/examples/formulas_and_errors.rs
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
use ironcalc_base::{model::Model, types::CellType};
|
||||||
|
|
||||||
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
let mut model = Model::new_empty("formulas-and-errors", "en", "UTC")?;
|
||||||
|
// A1
|
||||||
|
model.set_user_input(0, 1, 1, "1".to_string());
|
||||||
|
// A2
|
||||||
|
model.set_user_input(0, 2, 1, "2".to_string());
|
||||||
|
// A3
|
||||||
|
model.set_user_input(0, 3, 1, "3".to_string());
|
||||||
|
// B1
|
||||||
|
model.set_user_input(0, 1, 2, "=SUM(A1:A3)".to_string());
|
||||||
|
// B2
|
||||||
|
model.set_user_input(0, 2, 2, "=B1/0".to_string());
|
||||||
|
// Evaluate
|
||||||
|
model.evaluate();
|
||||||
|
|
||||||
|
let cells = model.get_all_cells();
|
||||||
|
|
||||||
|
let mut cells_count = 0;
|
||||||
|
let mut formula_count = 0;
|
||||||
|
let mut error_count = 0;
|
||||||
|
|
||||||
|
for cell in cells {
|
||||||
|
if let Some(cell) = model
|
||||||
|
.workbook
|
||||||
|
.worksheet(cell.index)?
|
||||||
|
.cell(cell.row, cell.column)
|
||||||
|
{
|
||||||
|
if cell.get_type() == CellType::ErrorValue {
|
||||||
|
error_count += 1;
|
||||||
|
}
|
||||||
|
if cell.has_formula() {
|
||||||
|
formula_count += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
cells_count += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_eq!(cells_count, 5);
|
||||||
|
assert_eq!(formula_count, 2);
|
||||||
|
assert_eq!(error_count, 1);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -18,6 +18,12 @@
|
|||||||
//! ```rust
|
//! ```rust
|
||||||
#![doc = include_str!("../examples/hello_world.rs")]
|
#![doc = include_str!("../examples/hello_world.rs")]
|
||||||
//! ```
|
//! ```
|
||||||
|
//!
|
||||||
|
//! In this example, we demonstrate our ability to handle formulas and errors:
|
||||||
|
//!
|
||||||
|
//! ```rust
|
||||||
|
#![doc = include_str!("../examples/formulas_and_errors.rs")]
|
||||||
|
//! ```
|
||||||
|
|
||||||
pub mod calc_result;
|
pub mod calc_result;
|
||||||
pub mod cell;
|
pub mod cell;
|
||||||
|
|||||||
@@ -1608,6 +1608,7 @@ impl Model {
|
|||||||
None => Ok(cell.get_text(&self.workbook.shared_strings, &self.language)),
|
None => Ok(cell.get_text(&self.workbook.shared_strings, &self.language)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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();
|
||||||
|
|||||||
Reference in New Issue
Block a user