From 0589659bf68c22f72f4d3d0f02fc00603d02bba6 Mon Sep 17 00:00:00 2001 From: hm3248 Date: Sun, 30 Mar 2025 09:03:50 -0400 Subject: [PATCH] Add new function: get_col_width --- excelize.py | 31 +++++++++++++++++++++++++++++++ main.go | 16 ++++++++++++++++ test_excelize.py | 7 ++++++- types_c.h | 6 ++++++ types_go.py | 5 +++++ 5 files changed, 64 insertions(+), 1 deletion(-) diff --git a/excelize.py b/excelize.py index 8d554b6..7b1e100 100644 --- a/excelize.py +++ b/excelize.py @@ -1669,6 +1669,37 @@ def get_col_visible(self, sheet: str, col: str) -> bool: return res.val raise RuntimeError(err) + def get_col_width(self, sheet: str, col: str) -> float: + """ + Get column width by given worksheet name and column name. + + Args: + sheet (str): The worksheet name + col (str): The column name + + Returns: + float: Return the column width if no error occurred, otherwise + raise a RuntimeError with the message. + + Example: + For example, get width of column D in Sheet1: + + ```python + try: + width = f.get_col_width("Sheet1", "D") + except RuntimeError as err: + print(err) + ``` + """ + lib.GetColWidth.restype = types_go._Float64ErrorResult + res = lib.GetColWidth( + self.file_index, sheet.encode(ENCODE), col.encode(ENCODE) + ) + err = res.err.decode(ENCODE) + if not err: + return res.val + raise RuntimeError(err) + def get_default_font(self) -> str: """ Get the default font name currently set in the workbook. The spreadsheet diff --git a/main.go b/main.go index 0bf9353..7c81e9e 100644 --- a/main.go +++ b/main.go @@ -1173,6 +1173,22 @@ func GetColVisible(idx int, sheet, col *C.char) C.struct_BoolErrorResult { return C.struct_BoolErrorResult{val: C._Bool(val), err: C.CString(emptyString)} } +// GetColWidth provides a function to get column width by given worksheet name +// and column name. This function is concurrency safe. +// +//export GetColWidth +func GetColWidth(idx int, sheet, col *C.char) C.struct_Float64ErrorResult { + f, ok := files.Load(idx) + if !ok { + return C.struct_Float64ErrorResult{val: C.double(0), err: C.CString(errFilePtr)} + } + val, err := f.(*excelize.File).GetColWidth(C.GoString(sheet), C.GoString(col)) + if err != nil { + return C.struct_Float64ErrorResult{val: C.double(val), err: C.CString(err.Error())} + } + return C.struct_Float64ErrorResult{val: C.double(val), err: C.CString(emptyString)} +} + // GetDefaultFont provides the default font name currently set in the // workbook. The spreadsheet generated by excelize default font is Calibri. // diff --git a/test_excelize.py b/test_excelize.py index 104e782..3737af7 100644 --- a/test_excelize.py +++ b/test_excelize.py @@ -1176,10 +1176,15 @@ def test_cell_rich_text(self): f.set_row_height("Sheet1", 0, 35) self.assertEqual(str(context.exception), "invalid row number 0") - self.assertIsNone(f.set_col_width("Sheet1", "A", "A", 44)) + self.assertIsNone(f.set_col_width("Sheet1", "A", "A", 44.5)) with self.assertRaises(RuntimeError) as context: f.set_col_width("SheetN", "A", "A", 44) self.assertEqual(str(context.exception), "sheet SheetN does not exist") + self.assertEqual(f.get_col_width("Sheet1", "A"), 44.5) + with self.assertRaises(RuntimeError) as context: + f.get_col_width("SheetN", "A") + self.assertEqual(str(context.exception), "sheet SheetN does not exist") + expected = [ excelize.RichTextRun( text="bold", diff --git a/types_c.h b/types_c.h index 07adef3..463ac9c 100644 --- a/types_c.h +++ b/types_c.h @@ -728,6 +728,12 @@ struct BoolErrorResult char *err; }; +struct Float64ErrorResult +{ + double val; + char *err; +}; + struct StringArrayErrorResult { int ArrLen; diff --git a/types_go.py b/types_go.py index 0f9c1c1..7ece669 100644 --- a/types_go.py +++ b/types_go.py @@ -704,6 +704,11 @@ class _BoolErrorResult(Structure): ("err", c_char_p), ] +class _Float64ErrorResult(Structure): + _fields_ = [ + ("val", c_double), + ("err", c_char_p), + ] class _StringArrayErrorResult(Structure): _fields_ = [