sheetsui/src/book/mod.rs

120 lines
3.2 KiB
Rust
Raw Normal View History

2024-11-11 19:27:59 -05:00
use anyhow::{anyhow, Result};
2024-11-13 19:39:47 -05:00
use ironcalc::base::{
types::{SheetData, Worksheet},
Model,
};
2024-11-11 19:27:59 -05:00
2024-11-13 19:02:41 -05:00
/// A spreadsheet book with some internal state tracking.
2024-11-11 19:27:59 -05:00
pub struct Book {
model: Model,
current_sheet: u32,
2024-11-13 19:02:41 -05:00
current_location: (i32, i32),
2024-11-11 19:27:59 -05:00
}
impl Book {
2024-11-13 19:02:41 -05:00
/// Get the currently set sheets name.
2024-11-11 19:27:59 -05:00
pub fn get_sheet_name(&self) -> Result<&str> {
Ok(&self.get_sheet()?.name)
}
2024-11-13 19:02:41 -05:00
/// Get the sheet data for the current worksheet.
2024-11-11 19:27:59 -05:00
pub fn get_sheet_data(&self) -> Result<&SheetData> {
Ok(&self.get_sheet()?.sheet_data)
}
2024-11-13 19:02:41 -05:00
/// Get a cells formatted content.
pub fn get_cell_rendered(&self) -> Result<String> {
2024-11-13 19:39:47 -05:00
Ok(self
.model
.get_formatted_cell_value(
self.current_sheet,
self.current_location.0,
self.current_location.1,
)
2024-11-13 19:02:41 -05:00
.map_err(|s| anyhow!("Unable to format cell {}", s))?)
}
2024-11-13 19:39:47 -05:00
2024-11-13 19:02:41 -05:00
/// Get a cells actual content as a string.
pub fn get_cell_contents(&self) -> Result<String> {
2024-11-13 19:39:47 -05:00
Ok(self
.model
.get_cell_content(
self.current_sheet,
self.current_location.0,
self.current_location.1,
)
2024-11-13 19:02:41 -05:00
.map_err(|s| anyhow!("Unable to format cell {}", s))?)
}
pub fn edit_cell(&mut self, value: String) -> Result<()> {
2024-11-13 19:39:47 -05:00
self.model
.set_user_input(
self.current_sheet,
self.current_location.0,
self.current_location.1,
value,
)
.map_err(|e| anyhow!("Invalid cell contents: {}", e))?;
2024-11-13 19:02:41 -05:00
Ok(())
}
/// Get the current sheets dimensions. This is a somewhat expensive calculation.
pub fn get_dimensions(&self) -> Result<(usize, usize)> {
let dimensions = self.get_sheet()?.dimension();
2024-11-13 19:39:47 -05:00
Ok((
dimensions.max_row.try_into()?,
dimensions.max_column.try_into()?,
))
2024-11-13 19:02:41 -05:00
}
/// Select a sheet by name.
pub fn select_sheet_by_name(&mut self, name: &str) -> bool {
2024-11-13 19:39:47 -05:00
if let Some(sheet) = self
.model
.workbook
.worksheets
.iter()
.find(|sheet| sheet.name == name)
{
2024-11-13 19:02:41 -05:00
self.current_sheet = sheet.sheet_id;
return true;
}
false
}
pub fn get_sheet_names(&self) -> Vec<String> {
self.model.workbook.get_worksheet_names()
}
2024-11-13 19:39:47 -05:00
2024-11-13 19:02:41 -05:00
/// Select a sheet by id.
pub fn select_sheet_by_id(&mut self, id: u32) -> bool {
2024-11-13 19:39:47 -05:00
if let Some(sheet) = self
.model
.workbook
.worksheets
.iter()
.find(|sheet| sheet.sheet_id == id)
{
2024-11-13 19:02:41 -05:00
self.current_sheet = sheet.sheet_id;
return true;
}
false
}
2024-11-13 19:39:47 -05:00
2024-11-11 19:27:59 -05:00
fn get_sheet(&self) -> Result<&Worksheet> {
2024-11-13 19:39:47 -05:00
Ok(self
.model
.workbook
.worksheet(self.current_sheet)
2024-11-11 19:27:59 -05:00
.map_err(|s| anyhow!("Invalid Worksheet: {}", s))?)
}
2024-11-13 19:39:47 -05:00
2024-11-11 19:27:59 -05:00
fn get_sheet_mut(&mut self) -> Result<&mut Worksheet> {
2024-11-13 19:39:47 -05:00
Ok(self
.model
.workbook
.worksheet_mut(self.current_sheet)
2024-11-11 19:27:59 -05:00
.map_err(|s| anyhow!("Invalid Worksheet: {}", s))?)
}
}