umya-spreadsheet

repository·master·Indexed 19 days ago

https://github.com/mathnya/umya-spreadsheet

A pure Rust library for reading and writing .xlsx and .xlsm files, with support for CSV and WebAssembly. It provides functionality to create workbooks, modify cell values and styles, add charts, and perform coordinate and date conversions between Excel formats and Rust types like chrono and jiff.

Tokens
10.6K
Snippets
42
Records
50
Agent score
66%

What's inside umya-spreadsheet

  1. Pass Workbook to functions

    master

    When writing functions that modify a spreadsheet, pass the Workbook as a mutable reference (&mut Workbook).

    let mut book = umya_spreadsheet::new_file();
    let _unused =  book.new_sheet("Sheet2");
    update_excel(&mut book);
    
    fn update_excel(book: &mut Workbook) {
       book.sheet_by_name_mut("Sheet2").unwrap().cell_mut("A1").set_value("Test"); 
    }
    let mut book = umya_spreadsheet::new_file();
    let _unused =  book.new_sheet("Sheet2");
    update_excel(&mut book);
    
    fn update_excel(book:  &mut Workbook) {
       book.sheet_by_name_mut("Sheet2").unwrap().cell_mut("A1").set_value("Test"); 
    }
  2. Install umya-spreadsheet

    master

    Add umya-spreadsheet to your Cargo.toml. You can enable specific features for WebAssembly support or restricted image processing.

    Standard Installation

    [dependencies]
    umya-spreadsheet = "3.0.1"

    WebAssembly Support

    To use the library in a WebAssembly environment, enable the js feature:

    [dependencies]
    umya-spreadsheet = { version = "3.0.1", features = ["js"] }

    Restricted Image Processing

    To use only png for image processing, enable the image/png feature:

    [dependencies]
    umya-spreadsheet = { version = "3.0.1", features = ["image/png"] }

    In your main.rs, ensure you include:

    extern crate umya_spreadsheet;
  3. Manage worksheets in a workbook

    master

    Worksheets can be created, cloned, or accessed by index or name.

    • Create New: Use book.new_sheet("Name") to add a worksheet.
    • Access by Index: Use book.sheet(index) (0-indexed).
    • Access by Name: Use book.sheet_by_name("Name") or book.sheet_by_name_mut("Name") for mutable access.
    • Clone/Copy: You can clone a worksheet using .clone() and then add it back to the workbook using book.add_sheet(cloned_sheet).
    use umya_spreadsheet::*;
    
    let mut book = new_file();
    
    // New worksheet
    let _ = book.new_sheet("Sheet2");
    
    // Copy Worksheet
    let mut clone_sheet = book.sheet(0).unwrap().clone();
    clone_sheet.set_name("New Sheet");
    let _ = book.add_sheet(clone_sheet);
  4. Read or create Excel files

    master

    You can create a new workbook, read an existing .xlsx file, or use a lazy reader for better performance with large files.

    • New File: Use new_file() to create a workbook with a default theme, stylesheet, and one worksheet named "Sheet1".
    • Standard Reader: Use reader::xlsx::read(path) to load an existing file.
    • Lazy Reader: Use reader::xlsx::lazy_read(path) to delay loading worksheets until they are needed, which improves performance for large files.
    use umya_spreadsheet::*;
    use std::path::Path;
    
    // New file
    let mut book = new_file();
    
    // Reader
    let path = Path::new("./test.xlsx");
    let mut book = reader::xlsx::read(path).unwrap();
    
    // Lazy Reader (better for large files)
    let mut book = reader::xlsx::lazy_read(path).unwrap();
  5. Read and change cell values

    master

    Reading values

    You can access cell values by sheet name and cell address (e.g., "A1") or by coordinates (row, column).

    // By name
    book.sheet_by_name("Sheet1").unwrap().cell("A1").value();
    
    // By coordinates (1, 1)
    book.sheet_by_name("Sheet1").unwrap().cell((1, 1)).value();
    
    // Using index
    book.sheet_mut(&0).unwrap().cell((1, 1)).value();

    Changing values

    To modify a cell, use the mutable versions of the sheet and cell methods.

    // Using sheet name and cell name
    book.sheet_by_name_mut("Sheet1").unwrap().cell_mut("A1").set_value("TEST1");
    
    // Using sheet index and cell name
    book.sheet_mut(&0).unwrap().cell_mut("A1").set_value("TEST2");
    // Read Value
    let mut book = umya_spreadsheet::new_file();
    book.sheet_by_name("Sheet1").unwrap().cell("A1").value();
    book.sheet_by_name("Sheet1").unwrap().cell((1, 1)).value();
    book.sheet_mut(&0).unwrap().cell((1, 1)).value();
    
    // Change Value
    let mut book = umya_spreadsheet::new_file();
    book.sheet_by_name_mut("Sheet1").unwrap().cell_mut("A1").set_value("TEST1");
    book.sheet_mut(&0).unwrap().cell_mut("A1").set_value("TEST2");
  6. Create and write xlsx files

    master

    Create a new file

    Initialize a new workbook using umya_spreadsheet::new_file().

    Write a file

    Save a Workbook to a path using umya_spreadsheet::writer::xlsx::write.

    Write with password

    To save a file with password protection, use umya_spreadsheet::writer::xlsx::write_with_password.

    Set password on existing file

    To apply a password to a file during a copy/save operation, use umya_spreadsheet::writer::xlsx::set_password.

    // New file
    let mut book = umya_spreadsheet::new_file();
    
    // Write file
    let path = std::path::Path::new("./tests/result_files/bbb.xlsx");
    let _unused =  umya_spreadsheet::writer::xlsx::write(&book, path);
    
    // Write file with password
    let path = std::path::Path::new("./tests/result_files/bbb.xlsx");
    let _unused =  umya_spreadsheet::writer::xlsx::write_with_password(&book, path, "password");
    
    // Set password
    let from_path = std::path::Path::new("./tests/test_files/aaa.xlsx");
    let to_path = std::path::Path::new("./tests/result_files/bbb.xlsx");
    let _unused =  umya_spreadsheet::writer::xlsx::set_password(&from_path, &to_path, "password");
  7. Read xlsx files

    master

    Use umya_spreadsheet::reader::xlsx::read to load an existing .xlsx file into a Workbook.

    let path = std::path::Path::new("./path/to/file.xlsx");
    let mut book = umya_spreadsheet::reader::xlsx::read(path).unwrap();
    let path = std::path::Path::new("./tests/test_files/aaa.xlsx");
    let mut book = umya_spreadsheet::reader::xlsx::read(path).unwrap();
  8. Create a new chart

    master

    To add a chart to a worksheet, define the chart type, the marker coordinates (start and end), and the data series list.

    let mut book = umya_spreadsheet::new_file();
    
    // Define start marker
    let mut from_marker = umya_spreadsheet::structs::drawing::spreadsheet::MarkerType::default();
    from_marker.set_coordinate("C1");
    
    // Define end marker
    let mut to_marker = umya_spreadsheet::structs::drawing::spreadsheet::MarkerType::default();
    to_marker.set_coordinate("D11");
    
    // Define data series (Excel-style range strings)
    let area_chart_series_list = vec![
        "Sheet1!$A$1:$A$10",
        "Sheet1!$B$1:$B$10",
    ];
    
    // Create and configure the chart
    let mut chart = umya_spreadsheet::structs::Chart::default();
    chart.new_chart(
        umya_spreadsheet::structs::ChartType::LineChart,
        from_marker,
        to_marker,
        area_chart_series_list,
    );
    
    // Add the chart to the worksheet
    book.sheet_by_name_mut("Sheet1").unwrap().add_chart(chart);
    let mut book = umya_spreadsheet::new_file();
    // Add Chart
    let mut from_marker = umya_spreadsheet::structs::drawing::spreadsheet::MarkerType::default();
    from_marker.set_coordinate("C1");
    let mut to_marker = umya_spreadsheet::structs::drawing::spreadsheet::MarkerType::default();
    to_marker.set_coordinate("D11");
    let area_chart_series_list = vec![
        "Sheet1!$A$1:$A$10",
        "Sheet1!$B$1:$B$10",
    ];
    let mut chart = umya_spreadsheet::structs::Chart::default();
    chart.new_chart(
        umya_spreadsheet::structs::ChartType::LineChart,
        from_marker,
        to_marker,
        area_chart_series_list,
    );
    book.sheet_by_name_mut("Sheet1").unwrap()
        .add_chart(chart);
  9. Read xlsx files lazily

    master

    For large files, use umya_spreadsheet::reader::xlsx::lazy_read. This delays the loading of worksheets until they are actually needed, which can significantly improve response times and memory usage when dealing with massive datasets.

    let path = std::path::Path::new("./tests/test_files/aaa.xlsx");
    let mut book = umya_spreadsheet::reader::xlsx::lazy_read(path).unwrap();
  10. Change cell styles

    master

    You can modify the style of a cell, such as its background color, by accessing its mutable style object.

    let mut book = umya_spreadsheet::new_file();
    let mut style = book.sheet_by_name_mut("Sheet1").unwrap().style_mut("A1");
    
    // Set background color to red
    style.set_background_color(umya_spreadsheet::Color::COLOR_RED);
    let mut book = umya_spreadsheet::new_file();
    let mut style = book.sheet_by_name_mut("Sheet1").unwrap().style_mut("A1");
    // fill color on red.
    style.set_background_color(umya_spreadsheet::Color::COLOR_RED);
  11. Write workbook to a file

    master

    To save your changes to an .xlsx file, use the writer::xlsx::write function, passing the workbook reference and the target file path.

    use umya_spreadsheet::*;
    use std::path::Path;
    
    let mut book = new_file();
    let path = Path::new("output.xlsx");
    let _unused = writer::xlsx::write(&book, path);