mirror of
https://github.com/zaphar/sheetsui.git
synced 2025-07-23 13:29:48 -04:00
chore: formatting
This commit is contained in:
parent
df4416aace
commit
a5e90b2bf2
@ -1,5 +1,8 @@
|
|||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use ironcalc::base::{Model, types::{Worksheet, SheetData}};
|
use ironcalc::base::{
|
||||||
|
types::{SheetData, Worksheet},
|
||||||
|
Model,
|
||||||
|
};
|
||||||
|
|
||||||
/// A spreadsheet book with some internal state tracking.
|
/// A spreadsheet book with some internal state tracking.
|
||||||
pub struct Book {
|
pub struct Book {
|
||||||
@ -21,30 +24,58 @@ impl Book {
|
|||||||
|
|
||||||
/// Get a cells formatted content.
|
/// Get a cells formatted content.
|
||||||
pub fn get_cell_rendered(&self) -> Result<String> {
|
pub fn get_cell_rendered(&self) -> Result<String> {
|
||||||
Ok(self.model.get_formatted_cell_value(self.current_sheet, self.current_location.0, self.current_location.1)
|
Ok(self
|
||||||
|
.model
|
||||||
|
.get_formatted_cell_value(
|
||||||
|
self.current_sheet,
|
||||||
|
self.current_location.0,
|
||||||
|
self.current_location.1,
|
||||||
|
)
|
||||||
.map_err(|s| anyhow!("Unable to format cell {}", s))?)
|
.map_err(|s| anyhow!("Unable to format cell {}", s))?)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get a cells actual content as a string.
|
/// Get a cells actual content as a string.
|
||||||
pub fn get_cell_contents(&self) -> Result<String> {
|
pub fn get_cell_contents(&self) -> Result<String> {
|
||||||
Ok(self.model.get_cell_content(self.current_sheet, self.current_location.0, self.current_location.1)
|
Ok(self
|
||||||
|
.model
|
||||||
|
.get_cell_content(
|
||||||
|
self.current_sheet,
|
||||||
|
self.current_location.0,
|
||||||
|
self.current_location.1,
|
||||||
|
)
|
||||||
.map_err(|s| anyhow!("Unable to format cell {}", s))?)
|
.map_err(|s| anyhow!("Unable to format cell {}", s))?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn edit_cell(&mut self, value: String) -> Result<()> {
|
pub fn edit_cell(&mut self, value: String) -> Result<()> {
|
||||||
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))?;
|
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))?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the current sheets dimensions. This is a somewhat expensive calculation.
|
/// Get the current sheets dimensions. This is a somewhat expensive calculation.
|
||||||
pub fn get_dimensions(&self) -> Result<(usize, usize)> {
|
pub fn get_dimensions(&self) -> Result<(usize, usize)> {
|
||||||
let dimensions = self.get_sheet()?.dimension();
|
let dimensions = self.get_sheet()?.dimension();
|
||||||
Ok((dimensions.max_row.try_into()?, dimensions.max_column.try_into()?))
|
Ok((
|
||||||
|
dimensions.max_row.try_into()?,
|
||||||
|
dimensions.max_column.try_into()?,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Select a sheet by name.
|
/// Select a sheet by name.
|
||||||
pub fn select_sheet_by_name(&mut self, name: &str) -> bool {
|
pub fn select_sheet_by_name(&mut self, name: &str) -> bool {
|
||||||
if let Some(sheet) = self.model.workbook.worksheets.iter().find(|sheet| sheet.name == name) {
|
if let Some(sheet) = self
|
||||||
|
.model
|
||||||
|
.workbook
|
||||||
|
.worksheets
|
||||||
|
.iter()
|
||||||
|
.find(|sheet| sheet.name == name)
|
||||||
|
{
|
||||||
self.current_sheet = sheet.sheet_id;
|
self.current_sheet = sheet.sheet_id;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -54,23 +85,35 @@ impl Book {
|
|||||||
pub fn get_sheet_names(&self) -> Vec<String> {
|
pub fn get_sheet_names(&self) -> Vec<String> {
|
||||||
self.model.workbook.get_worksheet_names()
|
self.model.workbook.get_worksheet_names()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Select a sheet by id.
|
/// Select a sheet by id.
|
||||||
pub fn select_sheet_by_id(&mut self, id: u32) -> bool {
|
pub fn select_sheet_by_id(&mut self, id: u32) -> bool {
|
||||||
if let Some(sheet) = self.model.workbook.worksheets.iter().find(|sheet| sheet.sheet_id == id) {
|
if let Some(sheet) = self
|
||||||
|
.model
|
||||||
|
.workbook
|
||||||
|
.worksheets
|
||||||
|
.iter()
|
||||||
|
.find(|sheet| sheet.sheet_id == id)
|
||||||
|
{
|
||||||
self.current_sheet = sheet.sheet_id;
|
self.current_sheet = sheet.sheet_id;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_sheet(&self) -> Result<&Worksheet> {
|
fn get_sheet(&self) -> Result<&Worksheet> {
|
||||||
Ok(self.model.workbook.worksheet(self.current_sheet)
|
Ok(self
|
||||||
|
.model
|
||||||
|
.workbook
|
||||||
|
.worksheet(self.current_sheet)
|
||||||
.map_err(|s| anyhow!("Invalid Worksheet: {}", s))?)
|
.map_err(|s| anyhow!("Invalid Worksheet: {}", s))?)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_sheet_mut(&mut self) -> Result<&mut Worksheet> {
|
fn get_sheet_mut(&mut self) -> Result<&mut Worksheet> {
|
||||||
Ok(self.model.workbook.worksheet_mut(self.current_sheet)
|
Ok(self
|
||||||
|
.model
|
||||||
|
.workbook
|
||||||
|
.worksheet_mut(self.current_sheet)
|
||||||
.map_err(|s| anyhow!("Invalid Worksheet: {}", s))?)
|
.map_err(|s| anyhow!("Invalid Worksheet: {}", s))?)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user