|
|
|
|
@@ -3,7 +3,7 @@ use pyo3::{create_exception, prelude::*, wrap_pyfunction};
|
|
|
|
|
|
|
|
|
|
use types::{PySheetProperty, PyStyle};
|
|
|
|
|
use xlsx::base::types::Style;
|
|
|
|
|
use xlsx::base::Model;
|
|
|
|
|
use xlsx::base::{Model, UserModel};
|
|
|
|
|
|
|
|
|
|
use xlsx::export::{save_to_icalc, save_to_xlsx};
|
|
|
|
|
use xlsx::import;
|
|
|
|
|
@@ -14,6 +14,55 @@ use crate::types::PyCellType;
|
|
|
|
|
|
|
|
|
|
create_exception!(_ironcalc, WorkbookError, PyException);
|
|
|
|
|
|
|
|
|
|
#[pyclass]
|
|
|
|
|
pub struct PyUserModel {
|
|
|
|
|
/// The user model, which is a wrapper around the Model
|
|
|
|
|
pub model: UserModel,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[pymethods]
|
|
|
|
|
impl PyUserModel {
|
|
|
|
|
/// Saves the user model to an xlsx file
|
|
|
|
|
pub fn save_to_xlsx(&self, file: &str) -> PyResult<()> {
|
|
|
|
|
let model = self.model.get_model();
|
|
|
|
|
save_to_xlsx(model, file).map_err(|e| WorkbookError::new_err(e.to_string()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Saves the user model to file in the internal binary ic format
|
|
|
|
|
pub fn save_to_icalc(&self, file: &str) -> PyResult<()> {
|
|
|
|
|
let model = self.model.get_model();
|
|
|
|
|
save_to_icalc(model, file).map_err(|e| WorkbookError::new_err(e.to_string()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn apply_external_diffs(&mut self, external_diffs: &[u8]) -> PyResult<()> {
|
|
|
|
|
self.model
|
|
|
|
|
.apply_external_diffs(external_diffs)
|
|
|
|
|
.map_err(|e| WorkbookError::new_err(e.to_string()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn flush_send_queue(&mut self) -> Vec<u8> {
|
|
|
|
|
self.model.flush_send_queue()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_user_input(
|
|
|
|
|
&mut self,
|
|
|
|
|
sheet: u32,
|
|
|
|
|
row: i32,
|
|
|
|
|
column: i32,
|
|
|
|
|
value: &str,
|
|
|
|
|
) -> PyResult<()> {
|
|
|
|
|
self.model
|
|
|
|
|
.set_user_input(sheet, row, column, value)
|
|
|
|
|
.map_err(|e| WorkbookError::new_err(e.to_string()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_formatted_cell_value(&self, sheet: u32, row: i32, column: i32) -> PyResult<String> {
|
|
|
|
|
self.model
|
|
|
|
|
.get_formatted_cell_value(sheet, row, column)
|
|
|
|
|
.map_err(|e| WorkbookError::new_err(e.to_string()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// This is a model implementing the 'raw' API
|
|
|
|
|
#[pyclass]
|
|
|
|
|
pub struct PyModel {
|
|
|
|
|
@@ -257,6 +306,33 @@ pub fn create(name: &str, locale: &str, tz: &str) -> PyResult<PyModel> {
|
|
|
|
|
Ok(PyModel { model })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[pyfunction]
|
|
|
|
|
pub fn create_user_model(name: &str, locale: &str, tz: &str) -> PyResult<PyUserModel> {
|
|
|
|
|
let model = UserModel::new_empty(name, locale, tz)
|
|
|
|
|
.map_err(|e| WorkbookError::new_err(e.to_string()))?;
|
|
|
|
|
Ok(PyUserModel { model })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[pyfunction]
|
|
|
|
|
pub fn create_user_model_from_xlsx(
|
|
|
|
|
file_path: &str,
|
|
|
|
|
locale: &str,
|
|
|
|
|
tz: &str,
|
|
|
|
|
) -> PyResult<PyUserModel> {
|
|
|
|
|
let model = import::load_from_xlsx(file_path, locale, tz)
|
|
|
|
|
.map_err(|e| WorkbookError::new_err(e.to_string()))?;
|
|
|
|
|
let model = UserModel::from_model(model);
|
|
|
|
|
Ok(PyUserModel { model })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[pyfunction]
|
|
|
|
|
pub fn create_user_model_from_icalc(file_name: &str) -> PyResult<PyUserModel> {
|
|
|
|
|
let model =
|
|
|
|
|
import::load_from_icalc(file_name).map_err(|e| WorkbookError::new_err(e.to_string()))?;
|
|
|
|
|
let model = UserModel::from_model(model);
|
|
|
|
|
Ok(PyUserModel { model })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[pyfunction]
|
|
|
|
|
#[allow(clippy::panic)]
|
|
|
|
|
pub fn test_panic() {
|
|
|
|
|
@@ -274,5 +350,10 @@ fn ironcalc(m: &Bound<'_, PyModule>) -> PyResult<()> {
|
|
|
|
|
m.add_function(wrap_pyfunction!(load_from_icalc, m)?)?;
|
|
|
|
|
m.add_function(wrap_pyfunction!(test_panic, m)?)?;
|
|
|
|
|
|
|
|
|
|
// User model functions
|
|
|
|
|
m.add_function(wrap_pyfunction!(create_user_model, m)?)?;
|
|
|
|
|
m.add_function(wrap_pyfunction!(create_user_model_from_xlsx, m)?)?;
|
|
|
|
|
m.add_function(wrap_pyfunction!(create_user_model_from_icalc, m)?)?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|