diff --git a/excelize.py b/excelize.py index 4211a46..e2bdcd4 100644 --- a/excelize.py +++ b/excelize.py @@ -1727,6 +1727,22 @@ 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 all tables in a worksheet by given worksheet name. + + Returns: + 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) + 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. @@ -1760,6 +1776,39 @@ def insert_cols(self, sheet: str, col: str, n: int) -> Optional[Exception]: ).decode(ENCODE) return None if err == "" else Exception(err) + def insert_rows(self, sheet: str, row: int, n: int) -> Optional[Exception]: + """ + 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 + 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: + + .. code-block:: python + + err = f.insert_rows("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 ) -> Optional[Exception]: @@ -3244,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 ae1ca56..0beb429 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)} } +// GetWorkbookProps provides a function to gets workbook properties. +// +//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 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 { + 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..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), @@ -770,3 +772,10 @@ class _GetTablesResult(Structure): ("Tables", POINTER(_Table)), ("Err", c_char_p), ] + + +class _GetWorkbookPropsResult(Structure): + _fields_ = [ + ("opts", _WorkbookPropsOptions), + ("err", c_char_p), + ]