From 4381c2cc5bd916c992f779c28c4bf5f96830c8ac Mon Sep 17 00:00:00 2001 From: Fushiqihefrog <164356364+Fushiqihefrog@users.noreply.github.com> Date: Sun, 9 Feb 2025 11:42:52 +0000 Subject: [PATCH 1/2] Add 2 new functions: get_workbook_props and insert_rows --- excelize.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 35 ++++++++++++++++++++++++++++++++++- test_excelize.py | 4 ++++ types_c.h | 6 ++++++ types_go.py | 7 +++++++ 5 files changed, 99 insertions(+), 1 deletion(-) diff --git a/excelize.py b/excelize.py index 4211a46..cc839b5 100644 --- a/excelize.py +++ b/excelize.py @@ -1727,6 +1727,21 @@ def get_tables(self, sheet: str) -> Tuple[List[Table], Optional[Exception]]: err = res.Err.decode(ENCODE) return tables if tables else [], None if err == "" else Exception(err) + def get_workbook_props(self) -> Tuple[WorkbookPropsOptions, Optional[Exception]]: + """ + Get the properties of the workbook. + + Returns: + Tuple[WorkbookPropsOptions, Optional[Exception]]: Returns None if no error occurred, + otherwise returns an Exception with the message. + """ + lib.GetWorkbookProps.restype = types_go._GetWorkbookPropsResult + res = lib.GetWorkbookProps(self.file_index) + err = res.err.decode(ENCODE) + return c_value_to_py(res.opts, WorkbookPropsOptions()) if err == "" else None, ( + None if err == "" else Exception(err) + ) + def insert_cols(self, sheet: str, col: str, n: int) -> Optional[Exception]: """ Insert new columns before the given column name and number of columns. @@ -1759,6 +1774,39 @@ def insert_cols(self, sheet: str, col: str, n: int) -> Optional[Exception]: c_int(n), ).decode(ENCODE) return None if err == "" else Exception(err) + + def insert_rows(self, sheet: str, row: int, n: int) -> Optional[Exception]: + """ + Insert new rows before the specified row number in a given worksheet. + Use this method with caution, which will affect changes in references + such as formulas, charts, and so on. If there is any referenced value of + the worksheet, it will cause a file error when you open it. The excelize + only partially updates these references currently. + + Args: + sheet (str): The worksheet name + row (int): The row number before which new rows will be inserted + n (int): The number of rows to insert + + Returns: + Optional[Exception]: Returns None if no error occurred, + otherwise returns an Exception with the message. + + Example: + For example, create two rows before row 3 in Sheet1: + + .. code-block:: python + + err = f.insert_row("Sheet1", 3, 2) + """ + lib.InsertRows.restype = c_char_p + err = lib.InsertRows( + self.file_index, + sheet.encode(ENCODE), + c_int(row), + c_int(n), + ).decode(ENCODE) + return None if err == "" else Exception(err) def merge_cell( self, sheet: str, top_left_cell: str, bottom_right_cell: str diff --git a/main.go b/main.go index ae1ca56..73ec783 100644 --- a/main.go +++ b/main.go @@ -1315,6 +1315,25 @@ func GetTables(idx int, sheet *C.char) C.struct_GetTablesResult { return C.struct_GetTablesResult{TablesLen: C.int(len(tables)), Tables: (*C.struct_Table)(cArray), Err: C.CString(emptyString)} } +// GetWorkbookPropsprovides a function to retrieve the properties of a workbook. +// +//export GetWorkbookProps +func GetWorkbookProps(idx int) C.struct_GetWorkbookPropsResult { + f, ok := files.Load(idx) + if !ok { + return C.struct_GetWorkbookPropsResult{err: C.CString(errFilePtr)} + } + opts, err := f.(*excelize.File).GetWorkbookProps() + if err != nil { + return C.struct_GetWorkbookPropsResult{err: C.CString(err.Error())} + } + cVal, err := goValueToC(reflect.ValueOf(opts), reflect.ValueOf(&C.struct_WorkbookPropsOptions{})) + if err != nil { + return C.struct_GetWorkbookPropsResult{err: C.CString(err.Error())} + } + return C.struct_GetWorkbookPropsResult{opts: cVal.Elem().Interface().(C.struct_WorkbookPropsOptions), err: C.CString(emptyString)} +} + // InsertCols provides a function to insert new columns before the given column // name and number of columns. // @@ -1330,6 +1349,21 @@ func InsertCols(idx int, sheet, col *C.char, n int) *C.char { return C.CString(emptyString) } +// InsertRows provides a function to insert new rows before the specified row number +// in a given worksheet. +// +//export InsertRows +func InsertRows(idx int, sheet *C.char, row, n int) *C.char { + f, ok := files.Load(idx) + if !ok { + return C.CString(emptyString) + } + if err := f.(*excelize.File).InsertRows(C.GoString(sheet), row, n); err != nil { + return C.CString(err.Error()) + } + return C.CString(emptyString) +} + // MergeCell provides a function to merge cells by given range reference and // sheet name. Merging cells only keeps the upper-left cell value, and // discards the other values. @@ -1800,7 +1834,6 @@ func SaveAs(idx int, name *C.char, opts *C.struct_Options) *C.char { return C.CString(emptyString) } - // SearchSheet provides a function to get cell reference by given worksheet name, // cell value, and regular expression. The function doesn't support searching // on the calculated result, formatted numbers and conditional lookup diff --git a/test_excelize.py b/test_excelize.py index cbea28b..2ec696f 100644 --- a/test_excelize.py +++ b/test_excelize.py @@ -275,6 +275,7 @@ def test_style(self): self.assertIsNone(f.duplicate_row("Sheet1", 20)) self.assertIsNone(f.duplicate_row_to("Sheet1", 20, 20)) self.assertIsNone(f.insert_cols("Sheet1", "C", 2)) + self.assertIsNone(f.insert_rows("Sheet1", 20, 2)) self.assertIsNone(f.merge_cell("Sheet1", "A1", "B2")) self.assertIsNone(f.unmerge_cell("Sheet1", "A1", "B2")) @@ -1219,6 +1220,9 @@ def test_workbook_props(self): date1904=True, filter_privacy=True, code_name="code" ) self.assertIsNone(f.set_workbook_props(expected)) + opts, err = f.get_workbook_props() + self.assertEqual(opts, expected) + self.assertIsNone(err) self.assertIsNone(f.save_as(os.path.join("test", "TestWorkbookProps.xlsx"))) self.assertIsNone(f.close()) diff --git a/types_c.h b/types_c.h index a96642c..07adef3 100644 --- a/types_c.h +++ b/types_c.h @@ -787,3 +787,9 @@ struct GetTablesResult struct Table *Tables; char *Err; }; + +struct GetWorkbookPropsResult +{ + struct WorkbookPropsOptions opts; + char *err; +}; diff --git a/types_go.py b/types_go.py index 98862d7..b76d8ab 100644 --- a/types_go.py +++ b/types_go.py @@ -770,3 +770,10 @@ class _GetTablesResult(Structure): ("Tables", POINTER(_Table)), ("Err", c_char_p), ] + + +class _GetWorkbookPropsResult(Structure): + _fields_ = [ + ("opts", _WorkbookPropsOptions), + ("err", c_char_p), + ] From 06f22e57107280a36168600862262b3a197d135f Mon Sep 17 00:00:00 2001 From: xuri Date: Sun, 9 Feb 2025 19:50:39 +0800 Subject: [PATCH 2/2] Format code and fix typo --- excelize.py | 31 ++++++++++++++++--------------- main.go | 6 +++--- types_go.py | 2 ++ 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/excelize.py b/excelize.py index cc839b5..e2bdcd4 100644 --- a/excelize.py +++ b/excelize.py @@ -1729,11 +1729,12 @@ def get_tables(self, sheet: str) -> Tuple[List[Table], Optional[Exception]]: def get_workbook_props(self) -> Tuple[WorkbookPropsOptions, Optional[Exception]]: """ - Get the properties of the workbook. + Get all tables in a worksheet by given worksheet name. Returns: - Tuple[WorkbookPropsOptions, Optional[Exception]]: Returns None if no error occurred, - otherwise returns an Exception with the message. + Tuple[List[WorkbookPropsOptions], Optional[Exception]]: A tuple + containing the workbook property options and an exception if an + error occurred, otherwise None. """ lib.GetWorkbookProps.restype = types_go._GetWorkbookPropsResult res = lib.GetWorkbookProps(self.file_index) @@ -1741,7 +1742,7 @@ def get_workbook_props(self) -> Tuple[WorkbookPropsOptions, Optional[Exception]] return c_value_to_py(res.opts, WorkbookPropsOptions()) if err == "" else None, ( None if err == "" else Exception(err) ) - + def insert_cols(self, sheet: str, col: str, n: int) -> Optional[Exception]: """ Insert new columns before the given column name and number of columns. @@ -1774,30 +1775,30 @@ def insert_cols(self, sheet: str, col: str, n: int) -> Optional[Exception]: c_int(n), ).decode(ENCODE) return None if err == "" else Exception(err) - + def insert_rows(self, sheet: str, row: int, n: int) -> Optional[Exception]: """ - Insert new rows before the specified row number in a given worksheet. - Use this method with caution, which will affect changes in references - such as formulas, charts, and so on. If there is any referenced value of - the worksheet, it will cause a file error when you open it. The excelize - only partially updates these references currently. + Insert new rows after the given Excel row number starting from 1 and + number of rows. Use this method with caution, which will affect changes + in references such as formulas, charts, and so on. If there is any + referenced value of the worksheet, it will cause a file error when you + open it. The excelize only partially updates these references currently. Args: sheet (str): The worksheet name - row (int): The row number before which new rows will be inserted - n (int): The number of rows to insert + row (int): The row number + n (int): The rows Returns: Optional[Exception]: Returns None if no error occurred, otherwise returns an Exception with the message. Example: - For example, create two rows before row 3 in Sheet1: + For example, create two rows before row 3 in Sheet1: .. code-block:: python - err = f.insert_row("Sheet1", 3, 2) + err = f.insert_rows("Sheet1", 3, 2) """ lib.InsertRows.restype = c_char_p err = lib.InsertRows( @@ -3292,7 +3293,7 @@ def set_workbook_props(self, opts: WorkbookPropsOptions) -> Optional[Exception]: Sets workbook properties. Args: - opts (WorkbookPropsOptions): TThe workbook property options + opts (WorkbookPropsOptions): The workbook property options Returns: Optional[Exception]: Returns None if no error occurred, diff --git a/main.go b/main.go index 73ec783..0beb429 100644 --- a/main.go +++ b/main.go @@ -1315,7 +1315,7 @@ func GetTables(idx int, sheet *C.char) C.struct_GetTablesResult { return C.struct_GetTablesResult{TablesLen: C.int(len(tables)), Tables: (*C.struct_Table)(cArray), Err: C.CString(emptyString)} } -// GetWorkbookPropsprovides a function to retrieve the properties of a workbook. +// GetWorkbookProps provides a function to gets workbook properties. // //export GetWorkbookProps func GetWorkbookProps(idx int) C.struct_GetWorkbookPropsResult { @@ -1349,8 +1349,8 @@ func InsertCols(idx int, sheet, col *C.char, n int) *C.char { return C.CString(emptyString) } -// InsertRows provides a function to insert new rows before the specified row number -// in a given worksheet. +// InsertRows provides a function to insert new rows after the given Excel row +// number starting from 1 and number of rows. // //export InsertRows func InsertRows(idx int, sheet *C.char, row, n int) *C.char { diff --git a/types_go.py b/types_go.py index b76d8ab..0f9c1c1 100644 --- a/types_go.py +++ b/types_go.py @@ -704,6 +704,7 @@ class _BoolErrorResult(Structure): ("err", c_char_p), ] + class _StringArrayErrorResult(Structure): _fields_ = [ ("ArrLen", c_int), @@ -711,6 +712,7 @@ class _StringArrayErrorResult(Structure): ("Err", c_char_p), ] + class _CellNameToCoordinatesResult(Structure): _fields_ = [ ("col", c_int),