Adding new example counting formulas and errors (#6)

This commit is contained in:
fosdick.io
2024-02-20 23:01:00 -07:00
committed by GitHub
parent d2bab6608b
commit 5cf797d382
3 changed files with 53 additions and 0 deletions

View 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(())
}

View File

@@ -18,6 +18,12 @@
//! ```rust
#![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 cell;

View File

@@ -1608,6 +1608,7 @@ impl Model {
None => Ok(cell.get_text(&self.workbook.shared_strings, &self.language)),
}
}
/// Returns a list of all cells
pub fn get_all_cells(&self) -> Vec<CellIndex> {
let mut cells = Vec::new();