From 1476e8f6da7fd621bc709f23a06903436b07f034 Mon Sep 17 00:00:00 2001 From: Andrew Grosser Date: Tue, 2 Sep 2025 20:48:28 -0700 Subject: [PATCH] added dimensions Signed-off-by: Andrew Grosser --- bindings/python/src/lib.rs | 21 +++++++++++++++++++++ bindings/python/tests/test_create.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/bindings/python/src/lib.rs b/bindings/python/src/lib.rs index 51af9e2..1f29ae5 100644 --- a/bindings/python/src/lib.rs +++ b/bindings/python/src/lib.rs @@ -60,6 +60,17 @@ impl PyUserModel { .map_err(|e| WorkbookError::new_err(e.to_string())) } + /// Gets the dimensions of a worksheet, returning the bounds of all non-empty cells. + /// Returns a tuple of (min_row, max_row, min_column, max_column). + /// For an empty sheet, returns (1, 1, 1, 1). + pub fn get_sheet_dimensions(&self, sheet: u32) -> PyResult<(i32, i32, i32, i32)> { + let model = self.model.get_model(); + let worksheet = model.workbook.worksheet(sheet) + .map_err(|e| WorkbookError::new_err(e.to_string()))?; + let dimension = worksheet.dimension(); + Ok((dimension.min_row, dimension.max_row, dimension.min_column, dimension.max_column)) + } + pub fn to_bytes(&self) -> PyResult> { let bytes = self.model.to_bytes(); Ok(bytes) @@ -283,6 +294,16 @@ impl PyModel { .map_err(|e| WorkbookError::new_err(e.to_string())) } + /// Gets the dimensions of a worksheet, returning the bounds of all non-empty cells. + /// Returns a tuple of (min_row, max_row, min_column, max_column). + /// For an empty sheet, returns (1, 1, 1, 1). + pub fn get_sheet_dimensions(&self, sheet: u32) -> PyResult<(i32, i32, i32, i32)> { + let worksheet = self.model.workbook.worksheet(sheet) + .map_err(|e| WorkbookError::new_err(e.to_string()))?; + let dimension = worksheet.dimension(); + Ok((dimension.min_row, dimension.max_row, dimension.min_column, dimension.max_column)) + } + #[allow(clippy::panic)] pub fn test_panic(&self) -> PyResult<()> { panic!("This function panics for testing panic handling"); diff --git a/bindings/python/tests/test_create.py b/bindings/python/tests/test_create.py index ca029e5..31bae11 100644 --- a/bindings/python/tests/test_create.py +++ b/bindings/python/tests/test_create.py @@ -27,3 +27,31 @@ def test_simple_user(): model2.apply_external_diffs(diffs) assert model2.get_formatted_cell_value(0, 1, 1) == "3" assert model2.get_formatted_cell_value(0, 1, 2) == "6" + + +def test_sheet_dimensions(): + # Test with empty sheet + model = ic.create("model", "en", "UTC") + min_row, max_row, min_col, max_col = model.get_sheet_dimensions(0) + assert (min_row, max_row, min_col, max_col) == (1, 1, 1, 1) + + # Add some cells + model.set_user_input(0, 3, 5, "Hello") + model.set_user_input(0, 10, 8, "World") + model.evaluate() + + # Check dimensions - should span from (3,5) to (10,8) + min_row, max_row, min_col, max_col = model.get_sheet_dimensions(0) + assert (min_row, max_row, min_col, max_col) == (3, 10, 5, 8) + + +def test_sheet_dimensions_user_model(): + # Test with user model API as well + model = ic.create_user_model("model", "en", "UTC") + + # Add a single cell + model.set_user_input(0, 2, 3, "Test") + + # Check dimensions + min_row, max_row, min_col, max_col = model.get_sheet_dimensions(0) + assert (min_row, max_row, min_col, max_col) == (2, 2, 3, 3)