ironcalc / IronCalc

Main engine of the IronCalc ecosystem
Apache License 2.0
60 stars 2 forks source link

Adding new example counting formulas and errors #6

Closed fosdickio closed 7 months ago

fosdickio commented 7 months ago

Summary

I'm adding a new example to check whether a cell contains an error or formula. I found this missing when trying to work through an example to read in a workbook and do something simple (count the total number of cells, formulas, and errors).

External Usage

I originally worked on this from a separate project and pulled in the dependency to Cargo.toml like so (similar to TironCalc):

[dependencies]
ironcalc = { git = "https://github.com/ironcalc/IronCalc", version = "0.1.0"}

From there, I was able to load a local .xlsx file and perform the operation on it (cargo run test.xlsx).

use ironcalc::import::load_model_from_xlsx;

fn main() {
    let args: Vec<String> = std::env::args().collect();
    if args.len() != 2 {
        panic!("Usage: {} <file.xlsx>", args[0]);
    }
    let file_name = &args[1];

    let mut model = load_model_from_xlsx(file_name, "en", "UTC").unwrap();
    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 model.cell_contains_formula(cell.index, cell.row, cell.column) {
            formula_count += 1;
        }

        if model.cell_contains_error(cell.index, cell.row, cell.column) {
            error_count += 1;
        }

        cells_count += 1;
    }

    println!("Cell count: {}", cells_count);
    println!("Formula count: {}", formula_count);
    println!("Error count: {}", error_count);
}

Other Considerations

I've moved this example into base, as it is an example mostly demonstrating functionality on ironcalc_base::model::Model.

Testing

This code was ran locally and tested using make tests without error.

codecov[bot] commented 7 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (d2bab66) 90.36% compared to head (a4970e0) 90.36%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #6 +/- ## ======================================= Coverage 90.36% 90.36% ======================================= Files 156 156 Lines 27401 27401 ======================================= Hits 24761 24761 Misses 2640 2640 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

nhatcher commented 7 months ago

Summary

I'm adding a new public API endpoint to check whether a cell contains an error or formula. I found this missing when trying to work through an example to read in a workbook and do something simple (count the total number of cells, formulas, and errors). I'm adding an example to accompany it.

External Usage

I originally worked on this from a separate project and pulled in the dependency to Cargo.toml like so (similar to TironCalc):

That's a nice idea but what you are doing here is better.

[dependencies]
ironcalc = { git = "https://github.com/ironcalc/IronCalc", version = "0.1.0"}

I think you can drop the version (my mistake!)

From there, I was able to load a local .xlsx file and perform the operation on it (cargo run test.xlsx).

use ironcalc::import::load_model_from_xlsx;

fn main() {
    let args: Vec<String> = std::env::args().collect();
    if args.len() != 2 {
        panic!("Usage: {} <file.xlsx>", args[0]);
    }
    let file_name = &args[1];

    let mut model = load_model_from_xlsx(file_name, "en", "UTC").unwrap();
    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 model.cell_contains_formula(cell.index, cell.row, cell.column) {
            formula_count += 1;
        } else if model.cell_contains_error(cell.index, cell.row, cell.column) {
            error_count += 1;
        }

        cells_count += 1;
    }

    println!("Cell count: {}", cells_count);
    println!("Formula count: {}", formula_count);
    println!("Error count: {}", error_count);
}

Other Considerations

I've moved this example into base, as it is an example mostly demonstrating functionality on ironcalc_base::model::Model.

Testing

This code was ran locally and tested using make tests without error.

fosdickio commented 7 months ago

@nhatcher This is ready for review again based on your feedback. You might want to squash-and-merge this one, as there are quite a few small little changes.