Crate ironcalc

Source
Expand description

§IronCalc - Core API documentation

This technical API documentation is aimed at developers. It is used to build language bindings (like python, javascript or nodejs) or to build full fledged applications like TironCalc in the terminal or IronCalc, the Web application.

§Basic usage

Add the dependency in Cargo.toml:

[dependencies]
ironcalc = { git = "https://github.com/ironcalc/IronCalc", tag = "v0.5.0" }

A simple example with some numbers, a new sheet and a formula:

use ironcalc::{
    base::{expressions::utils::number_to_column, Model},
    export::save_to_xlsx,
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut model = Model::new_empty("hello-calc.xlsx", "en", "UTC")?;
    // Adds a square of numbers in the first sheet
    for row in 1..100 {
        for column in 1..100 {
            let value = row * column;
            model.set_user_input(0, row, column, format!("{}", value))?;
        }
    }
    // Adds a new sheet
    model.add_sheet("Calculation")?;
    // column 100 is CV
    let last_column = number_to_column(100).ok_or("Invalid column number")?;
    let formula = format!("=SUM(Sheet1!A1:{}100)", last_column);
    model.set_user_input(1, 1, 1, formula)?;

    // evaluates
    model.evaluate();

    // saves to disk
    save_to_xlsx(&model, "hello-calc.xlsx")?;
    Ok(())
}

§Examples

This is a collection of full fledged examples you can use as a starting point or for learning purposes. You might find the code in the examples folder

§Styling the workbook

Adding colors, to cells, full columns or full rows is easy

use ironcalc::{base::Model, export::save_to_xlsx};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut model = Model::new_empty("hello_styles", "en", "UTC")?;

    // We are going to change styles in cell A1
    let (sheet, row, column) = (0, 1, 1);
    let mut style = model.get_style_for_cell(sheet, row, column)?;
    style.fill.fg_color = Some("#FF9011".to_string());
    style.font.b = true;
    style.font.color = Some("#E91E63".to_string());
    model.set_cell_style(sheet, row, column, &style)?;

    // saves to disk
    save_to_xlsx(&model, "hello-styles.xlsx")?;
    Ok(())
}

§Changing column width and row heigh

use ironcalc::{base::Model, export::save_to_xlsx};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut model = Model::new_empty("widths-and-heights", "en", "UTC")?;
    // Cell C5
    let (sheet, row, column) = (0, 5, 3);
    // Make the first column 4 times as width
    let worksheet = model.workbook.worksheet_mut(sheet)?;
    let column_width = worksheet.get_column_width(column)? * 4.0;
    worksheet.set_column_width(column, column_width)?;

    // and the first row twice as high.
    let row_height = worksheet.row_height(row)? * 2.0;
    worksheet.set_row_height(row, row_height)?;

    // saves to disk
    save_to_xlsx(&model, "widths-and-heights.xlsx")?;
    Ok(())
}

Re-exports§

pub use ironcalc_base as base;

Modules§

compare
error
export
import