Skip to content

Commit f64ad99

Browse files
Add 2 new functions: get_workbook_props and insert_rows (#11)
- Updated unit tests
1 parent 38ad3b2 commit f64ad99

File tree

5 files changed

+103
-2
lines changed

5 files changed

+103
-2
lines changed

excelize.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1756,6 +1756,22 @@ def get_tables(self, sheet: str) -> Tuple[List[Table], Optional[Exception]]:
17561756
err = res.Err.decode(ENCODE)
17571757
return tables if tables else [], None if err == "" else Exception(err)
17581758

1759+
def get_workbook_props(self) -> Tuple[WorkbookPropsOptions, Optional[Exception]]:
1760+
"""
1761+
Get all tables in a worksheet by given worksheet name.
1762+
1763+
Returns:
1764+
Tuple[List[WorkbookPropsOptions], Optional[Exception]]: A tuple
1765+
containing the workbook property options and an exception if an
1766+
error occurred, otherwise None.
1767+
"""
1768+
lib.GetWorkbookProps.restype = types_go._GetWorkbookPropsResult
1769+
res = lib.GetWorkbookProps(self.file_index)
1770+
err = res.err.decode(ENCODE)
1771+
return c_value_to_py(res.opts, WorkbookPropsOptions()) if err == "" else None, (
1772+
None if err == "" else Exception(err)
1773+
)
1774+
17591775
def insert_cols(self, sheet: str, col: str, n: int) -> Optional[Exception]:
17601776
"""
17611777
Insert new columns before the given column name and number of columns.
@@ -1789,6 +1805,39 @@ def insert_cols(self, sheet: str, col: str, n: int) -> Optional[Exception]:
17891805
).decode(ENCODE)
17901806
return None if err == "" else Exception(err)
17911807

1808+
def insert_rows(self, sheet: str, row: int, n: int) -> Optional[Exception]:
1809+
"""
1810+
Insert new rows after the given Excel row number starting from 1 and
1811+
number of rows. Use this method with caution, which will affect changes
1812+
in references such as formulas, charts, and so on. If there is any
1813+
referenced value of the worksheet, it will cause a file error when you
1814+
open it. The excelize only partially updates these references currently.
1815+
1816+
Args:
1817+
sheet (str): The worksheet name
1818+
row (int): The row number
1819+
n (int): The rows
1820+
1821+
Returns:
1822+
Optional[Exception]: Returns None if no error occurred,
1823+
otherwise returns an Exception with the message.
1824+
1825+
Example:
1826+
For example, create two rows before row 3 in Sheet1:
1827+
1828+
.. code-block:: python
1829+
1830+
err = f.insert_rows("Sheet1", 3, 2)
1831+
"""
1832+
lib.InsertRows.restype = c_char_p
1833+
err = lib.InsertRows(
1834+
self.file_index,
1835+
sheet.encode(ENCODE),
1836+
c_int(row),
1837+
c_int(n),
1838+
).decode(ENCODE)
1839+
return None if err == "" else Exception(err)
1840+
17921841
def merge_cell(
17931842
self, sheet: str, top_left_cell: str, bottom_right_cell: str
17941843
) -> Optional[Exception]:
@@ -3273,7 +3322,7 @@ def set_workbook_props(self, opts: WorkbookPropsOptions) -> Optional[Exception]:
32733322
Sets workbook properties.
32743323
32753324
Args:
3276-
opts (WorkbookPropsOptions): TThe workbook property options
3325+
opts (WorkbookPropsOptions): The workbook property options
32773326
32783327
Returns:
32793328
Optional[Exception]: Returns None if no error occurred,

main.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,25 @@ func GetTables(idx int, sheet *C.char) C.struct_GetTablesResult {
13311331
return C.struct_GetTablesResult{TablesLen: C.int(len(tables)), Tables: (*C.struct_Table)(cArray), Err: C.CString(emptyString)}
13321332
}
13331333

1334+
// GetWorkbookProps provides a function to gets workbook properties.
1335+
//
1336+
//export GetWorkbookProps
1337+
func GetWorkbookProps(idx int) C.struct_GetWorkbookPropsResult {
1338+
f, ok := files.Load(idx)
1339+
if !ok {
1340+
return C.struct_GetWorkbookPropsResult{err: C.CString(errFilePtr)}
1341+
}
1342+
opts, err := f.(*excelize.File).GetWorkbookProps()
1343+
if err != nil {
1344+
return C.struct_GetWorkbookPropsResult{err: C.CString(err.Error())}
1345+
}
1346+
cVal, err := goValueToC(reflect.ValueOf(opts), reflect.ValueOf(&C.struct_WorkbookPropsOptions{}))
1347+
if err != nil {
1348+
return C.struct_GetWorkbookPropsResult{err: C.CString(err.Error())}
1349+
}
1350+
return C.struct_GetWorkbookPropsResult{opts: cVal.Elem().Interface().(C.struct_WorkbookPropsOptions), err: C.CString(emptyString)}
1351+
}
1352+
13341353
// InsertCols provides a function to insert new columns before the given column
13351354
// name and number of columns.
13361355
//
@@ -1346,6 +1365,21 @@ func InsertCols(idx int, sheet, col *C.char, n int) *C.char {
13461365
return C.CString(emptyString)
13471366
}
13481367

1368+
// InsertRows provides a function to insert new rows after the given Excel row
1369+
// number starting from 1 and number of rows.
1370+
//
1371+
//export InsertRows
1372+
func InsertRows(idx int, sheet *C.char, row, n int) *C.char {
1373+
f, ok := files.Load(idx)
1374+
if !ok {
1375+
return C.CString(emptyString)
1376+
}
1377+
if err := f.(*excelize.File).InsertRows(C.GoString(sheet), row, n); err != nil {
1378+
return C.CString(err.Error())
1379+
}
1380+
return C.CString(emptyString)
1381+
}
1382+
13491383
// MergeCell provides a function to merge cells by given range reference and
13501384
// sheet name. Merging cells only keeps the upper-left cell value, and
13511385
// discards the other values.
@@ -1816,7 +1850,6 @@ func SaveAs(idx int, name *C.char, opts *C.struct_Options) *C.char {
18161850
return C.CString(emptyString)
18171851
}
18181852

1819-
18201853
// SearchSheet provides a function to get cell reference by given worksheet name,
18211854
// cell value, and regular expression. The function doesn't support searching
18221855
// on the calculated result, formatted numbers and conditional lookup

test_excelize.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ def test_style(self):
275275
self.assertIsNone(f.duplicate_row("Sheet1", 20))
276276
self.assertIsNone(f.duplicate_row_to("Sheet1", 20, 20))
277277
self.assertIsNone(f.insert_cols("Sheet1", "C", 2))
278+
self.assertIsNone(f.insert_rows("Sheet1", 20, 2))
278279
self.assertIsNone(f.merge_cell("Sheet1", "A1", "B2"))
279280
self.assertIsNone(f.unmerge_cell("Sheet1", "A1", "B2"))
280281

@@ -1222,6 +1223,9 @@ def test_workbook_props(self):
12221223
date1904=True, filter_privacy=True, code_name="code"
12231224
)
12241225
self.assertIsNone(f.set_workbook_props(expected))
1226+
opts, err = f.get_workbook_props()
1227+
self.assertEqual(opts, expected)
1228+
self.assertIsNone(err)
12251229
self.assertIsNone(f.save_as(os.path.join("test", "TestWorkbookProps.xlsx")))
12261230
self.assertIsNone(f.close())
12271231

types_c.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,3 +787,9 @@ struct GetTablesResult
787787
struct Table *Tables;
788788
char *Err;
789789
};
790+
791+
struct GetWorkbookPropsResult
792+
{
793+
struct WorkbookPropsOptions opts;
794+
char *err;
795+
};

types_go.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,13 +704,15 @@ class _BoolErrorResult(Structure):
704704
("err", c_char_p),
705705
]
706706

707+
707708
class _StringArrayErrorResult(Structure):
708709
_fields_ = [
709710
("ArrLen", c_int),
710711
("Arr", POINTER(POINTER(c_char))),
711712
("Err", c_char_p),
712713
]
713714

715+
714716
class _CellNameToCoordinatesResult(Structure):
715717
_fields_ = [
716718
("col", c_int),
@@ -770,3 +772,10 @@ class _GetTablesResult(Structure):
770772
("Tables", POINTER(_Table)),
771773
("Err", c_char_p),
772774
]
775+
776+
777+
class _GetWorkbookPropsResult(Structure):
778+
_fields_ = [
779+
("opts", _WorkbookPropsOptions),
780+
("err", c_char_p),
781+
]

0 commit comments

Comments
 (0)