How to Create a Cells FOSS Model in Rust
Aspose.Cells FOSS for Rust models a workbook as a tree of typed objects —
worksheets, cell collections, and rule objects such as validations. This
guide builds a complete workbook model from scratch: typed cell values, data
validation rules over CellArea ranges, and a saved XLSX file at the end.
Step-by-Step Guide
Step 1: Install the Package
Add the crate to your Cargo.toml as a Cargo git dependency (the crate is
not yet published on crates.io):
# Cargo.toml
[dependencies]
aspose-cells-foss-rust = { git = "https://github.com/aspose-cells-foss/Aspose.Cells-FOSS-for-Rust" }Then build the project:
cargo buildStep 2: Import Required Classes
use aspose_cells_foss_rust::{
CellArea, OperatorType, ValidationAlertType, ValidationType, Workbook,
};
use std::error::Error;Step 3: Create the Workbook and Cell Data
Start the model with a Workbook, name the sheet, and write typed values:
let mut workbook = Workbook::new();
{
let mut worksheets = workbook.get_worksheets_mut();
let sheet = worksheets.get(0)?;
sheet.set_name("Validation Sheet")?;
let mut cells = sheet.get_cells_mut();
cells.get("A1")?.put_value_string("Open")?;
cells.get("B2")?.put_value_i32(5)?;
cells.get("G1")?.put_value_string("ABCDE")?;
}Step 4: Add a List Validation
Validation rules attach to CellArea ranges through the sheet’s validation
collection. A list validation constrains cells to fixed choices with an
in-cell dropdown:
let mut validations = sheet.get_validations();
let list_index = validations.add(CellArea::create_cell_area_a1("A1", "A3")?)?;
let mut list_validation = validations
.get(list_index)
.expect("validation should exist");
list_validation.set_type(ValidationType::List);
list_validation.set_formula1("\"Open,Closed\"");
list_validation.set_in_cell_drop_down(true);
list_validation.set_show_input(true);
list_validation.set_input_title("Status");
list_validation.set_input_message("Pick a status");Step 5: Add Range and Custom Rules
OperatorType drives numeric range rules, and ValidationType::Custom
accepts a formula; ValidationAlertType selects the alert severity:
let decimal_index = validations.add(CellArea::create_cell_area_a1("B2", "C3")?)?;
let mut decimal_validation = validations
.get(decimal_index)
.expect("validation should exist");
decimal_validation.set_type(ValidationType::Decimal);
decimal_validation.set_operator(OperatorType::Between);
decimal_validation.set_formula1("1.5");
decimal_validation.set_formula2("9.5");
decimal_validation.set_show_error(true);
let custom_index = validations.add(CellArea::create_cell_area_a1("G1", "G1")?)?;
let mut custom_validation = validations
.get(custom_index)
.expect("validation should exist");
custom_validation.set_type(ValidationType::Custom);
custom_validation.set_alert_style(ValidationAlertType::Warning);
custom_validation.set_formula1("LEN(G1)<=5");Step 6: Save the Model
workbook.save("validations-sample.xlsx")?;The saved file opens in Excel with the dropdowns, range checks, and custom rule active.
Common Issues and Fixes
The validation has no visible effect in Excel.
Enable the prompts: set_show_input / set_show_error control whether the
input hint and error alert appear.
A rule should cover several separate ranges.
Add more ranges to an existing rule with add_area and additional
CellArea::create_cell_area_a1 ranges.
Borrow errors while adding validations.
Keep the worksheet borrow scoped: fetch get_validations inside the same
block as the mutations, and save the workbook after the block ends.
List choices are misread as a formula.
Wrap the choices in escaped quotes exactly as shown:
set_formula1("\"Open,Closed\"").
Frequently Asked Questions
Which validation types are available?
ValidationType includes List, Decimal, and Custom as shown here —
list dropdowns, numeric ranges via OperatorType, and formula-based rules.
Can validations show custom messages?
Yes — set_input_title / set_input_message control the prompt, and
set_error_title / set_error_message control the failure alert.
Does the model survive a round-trip?
Yes. Save with Workbook::save and reload with Workbook::load_xlsx; the
validation rules are preserved in the XLSX file.