@@ -1001,7 +1001,7 @@ def calc_cell_value(
1001
1001
result as a string and an exception if an error occurred, otherwise
1002
1002
None.
1003
1003
"""
1004
- lib .CalcCellValue .restype = types_go ._CalcCellValueResult
1004
+ lib .CalcCellValue .restype = types_go ._StringErrorResult
1005
1005
options = (
1006
1006
byref (py_value_to_c (opts [0 ], types_go ._Options ()))
1007
1007
if opts
@@ -1222,7 +1222,7 @@ def get_cell_formula(
1222
1222
Tuple[str, Optional[Exception]]: A tuple containing the cell formula
1223
1223
string and an exception if an error occurred, otherwise None.
1224
1224
"""
1225
- lib .GetCellFormula .restype = types_go ._GetCellFormulaResult
1225
+ lib .GetCellFormula .restype = types_go ._StringErrorResult
1226
1226
res = lib .GetCellFormula (
1227
1227
self .file_index , sheet .encode (ENCODE ), cell .encode (ENCODE )
1228
1228
)
@@ -1285,7 +1285,7 @@ def get_cell_value(
1285
1285
Tuple[str, Optional[Exception]]: A tuple containing the cell value
1286
1286
as a string and an exception if an error occurred, otherwise None.
1287
1287
"""
1288
- lib .GetCellValue .restype = types_go ._GetCellValueResult
1288
+ lib .GetCellValue .restype = types_go ._StringErrorResult
1289
1289
options = (
1290
1290
byref (py_value_to_c (opts [0 ], types_go ._Options ()))
1291
1291
if opts
@@ -1336,6 +1336,41 @@ def get_rows(
1336
1336
1337
1337
return rows , None if err == "" else Exception (err )
1338
1338
1339
+ def get_sheet_dimension (self , sheet : str ) -> Tuple [str , Optional [Exception ]]:
1340
+ """
1341
+ Get style definition by given style index.
1342
+
1343
+ Args:
1344
+ sheet (str): The worksheet name
1345
+
1346
+ Returns:
1347
+ Tuple[str, Optional[Exception]]: A tuple containing the sheet
1348
+ dimension, and an Exception object if an error occurred, otherwise
1349
+ None.
1350
+ """
1351
+ lib .GetSheetDimension .restype = types_go ._StringErrorResult
1352
+ res = lib .GetSheetDimension (self .file_index , sheet .encode (ENCODE ))
1353
+ err = res .err .decode (ENCODE )
1354
+ return res .val .decode (ENCODE ), None if err == "" else Exception (err )
1355
+
1356
+ def get_sheet_index (self , sheet : str ) -> Tuple [int , Optional [Exception ]]:
1357
+ """
1358
+ Get a sheet index of the workbook by the given sheet name. If the given
1359
+ sheet name is invalid or sheet doesn't exist, it will return an integer
1360
+ type value -1.
1361
+
1362
+ Args:
1363
+ sheet (str): The worksheet name
1364
+
1365
+ Returns:
1366
+ Tuple[int, Optional[Exception]]: A tuple containing the sheet index,
1367
+ and an Exception object if an error occurred, otherwise None.
1368
+ """
1369
+ lib .GetSheetIndex .restype = types_go ._IntErrorResult
1370
+ res = lib .GetSheetIndex (self .file_index , sheet .encode (ENCODE ))
1371
+ err = res .err .decode (ENCODE )
1372
+ return res .val , None if err == "" else Exception (err )
1373
+
1339
1374
def get_style (self , style_id : int ) -> Tuple [Optional [Style ], Optional [Exception ]]:
1340
1375
"""
1341
1376
Get style definition by given style index.
@@ -1369,9 +1404,42 @@ def get_tables(self, sheet: str) -> Tuple[List[Table], Optional[Exception]]:
1369
1404
lib .GetTables .restype = types_go ._GetTablesResult
1370
1405
res = lib .GetTables (self .file_index , sheet .encode (ENCODE ))
1371
1406
tables = c_value_to_py (res , GetTablesResult ()).tables
1372
- err = res .err .decode (ENCODE )
1407
+ err = res .Err .decode (ENCODE )
1373
1408
return tables , None if err == "" else Exception (err )
1374
1409
1410
+ def insert_cols (self , sheet : str , col : str , n : int ) -> Optional [Exception ]:
1411
+ """
1412
+ Insert new columns before the given column name and number of columns.
1413
+ Use this method with caution, which will affect changes in references
1414
+ such as formulas, charts, and so on. If there is any referenced value of
1415
+ the worksheet, it will cause a file error when you open it. The excelize
1416
+ only partially updates these references currently.
1417
+
1418
+ Args:
1419
+ sheet (str): The worksheet name
1420
+ col (str): The column name
1421
+ n (int): The columns
1422
+
1423
+ Returns:
1424
+ Optional[Exception]: Returns None if no error occurred,
1425
+ otherwise returns an Exception with the message.
1426
+
1427
+ Example:
1428
+ For example, create two columns before column C in Sheet1:
1429
+
1430
+ .. code-block:: python
1431
+
1432
+ err = f.insert_cols("Sheet1", "C", 2)
1433
+ """
1434
+ lib .InsertCols .restype = c_char_p
1435
+ err = lib .InsertCols (
1436
+ self .file_index ,
1437
+ sheet .encode (ENCODE ),
1438
+ col .encode (ENCODE ),
1439
+ c_int (n ),
1440
+ ).decode (ENCODE )
1441
+ return None if err == "" else Exception (err )
1442
+
1375
1443
def merge_cell (
1376
1444
self , sheet : str , top_left_cell : str , bottom_right_cell : str
1377
1445
) -> Optional [Exception ]:
@@ -1440,11 +1508,11 @@ def new_conditional_style(self, style: Style) -> Tuple[int, Optional[Exception]]
1440
1508
Tuple[int, Optional[Exception]]: A tuple containing the style index
1441
1509
and an exception if any error occurs.
1442
1510
"""
1443
- lib .NewConditionalStyle .restype = types_go ._NewStyleResult
1511
+ lib .NewConditionalStyle .restype = types_go ._IntErrorResult
1444
1512
options = py_value_to_c (style , types_go ._Style ())
1445
1513
res = lib .NewConditionalStyle (self .file_index , byref (options ))
1446
1514
err = res .err .decode (ENCODE )
1447
- return res .style , None if err == "" else Exception (err )
1515
+ return res .val , None if err == "" else Exception (err )
1448
1516
1449
1517
def new_sheet (self , sheet : str ) -> Tuple [int , Optional [Exception ]]:
1450
1518
"""
@@ -1459,10 +1527,10 @@ def new_sheet(self, sheet: str) -> Tuple[int, Optional[Exception]]:
1459
1527
Tuple[int, Optional[Exception]]: A tuple containing the index of the
1460
1528
new sheet and an Exception if an error occurred, otherwise None.
1461
1529
"""
1462
- lib .NewSheet .restype = types_go ._NewSheetResult
1530
+ lib .NewSheet .restype = types_go ._IntErrorResult
1463
1531
res = lib .NewSheet (self .file_index , sheet .encode (ENCODE ))
1464
1532
err = res .err .decode (ENCODE )
1465
- return res .idx , None if err == "" else Exception (err )
1533
+ return res .val , None if err == "" else Exception (err )
1466
1534
1467
1535
def new_style (self , style : Style ) -> Tuple [int , Optional [Exception ]]:
1468
1536
"""
@@ -1478,11 +1546,11 @@ def new_style(self, style: Style) -> Tuple[int, Optional[Exception]]:
1478
1546
Tuple[int, Optional[Exception]]: A tuple containing the style index
1479
1547
and an exception if any error occurs.
1480
1548
"""
1481
- lib .NewStyle .restype = types_go ._NewStyleResult
1549
+ lib .NewStyle .restype = types_go ._IntErrorResult
1482
1550
options = py_value_to_c (style , types_go ._Style ())
1483
1551
res = lib .NewStyle (self .file_index , byref (options ))
1484
1552
err = res .err .decode (ENCODE )
1485
- return res .style , None if err == "" else Exception (err )
1553
+ return res .val , None if err == "" else Exception (err )
1486
1554
1487
1555
def protect_sheet (
1488
1556
self , sheet : str , opts : SheetProtectionOptions
@@ -2767,10 +2835,10 @@ def column_name_to_number(name: str) -> Tuple[int, Optional[Exception]]:
2767
2835
Tuple[int, Optional[Exception]]: A tuple containing the column number
2768
2836
and an Exception if an error occurred, otherwise None.
2769
2837
"""
2770
- lib .ColumnNameToNumber .restype = types_go ._ColumnNameToNumberResult
2838
+ lib .ColumnNameToNumber .restype = types_go ._IntErrorResult
2771
2839
res = lib .ColumnNameToNumber (name .encode (ENCODE ))
2772
2840
err = res .err .decode (ENCODE )
2773
- return res .col , None if err == "" else Exception (err )
2841
+ return res .val , None if err == "" else Exception (err )
2774
2842
2775
2843
2776
2844
def column_number_to_name (num : int ) -> Tuple [str , Optional [Exception ]]:
@@ -2784,10 +2852,10 @@ def column_number_to_name(num: int) -> Tuple[str, Optional[Exception]]:
2784
2852
Tuple[str, Optional[Exception]]: A tuple containing the column name and
2785
2853
an Exception if an error occurred, otherwise None.
2786
2854
"""
2787
- lib .ColumnNumberToName .restype = types_go ._ColumnNumberToNameResult
2855
+ lib .ColumnNumberToName .restype = types_go ._StringErrorResult
2788
2856
res = lib .ColumnNumberToName (c_int (num ))
2789
2857
err = res .err .decode (ENCODE )
2790
- return res .col .decode (ENCODE ), None if err == "" else Exception (err )
2858
+ return res .val .decode (ENCODE ), None if err == "" else Exception (err )
2791
2859
2792
2860
2793
2861
def coordinates_to_cell_name (
@@ -2807,13 +2875,13 @@ def coordinates_to_cell_name(
2807
2875
Tuple[str, Optional[Exception]]: A tuple containing the cell name as a
2808
2876
string and an Exception if an error occurred, otherwise None.
2809
2877
"""
2810
- lib .CoordinatesToCellName .restype = types_go ._CoordinatesToCellNameResult
2878
+ lib .CoordinatesToCellName .restype = types_go ._StringErrorResult
2811
2879
options = False
2812
2880
if len (abs ) > 0 :
2813
2881
options = abs [0 ]
2814
2882
res = lib .CoordinatesToCellName (col , row , options )
2815
2883
err = res .err .decode (ENCODE )
2816
- return res .cell .decode (ENCODE ), None if err == "" else Exception (err )
2884
+ return res .val .decode (ENCODE ), None if err == "" else Exception (err )
2817
2885
2818
2886
2819
2887
def new_file () -> File :
@@ -2841,13 +2909,13 @@ def open_file(
2841
2909
Tuple[Optional[File], Optional[Exception]]: A tuple containing a File
2842
2910
object if successful, or None and an Exception if an error occurred.
2843
2911
"""
2844
- lib .OpenFile .restype , options = types_go ._OptionsResult , None
2912
+ lib .OpenFile .restype , options = types_go ._IntErrorResult , None
2845
2913
if len (opts ) > 0 :
2846
2914
options = byref (py_value_to_c (opts [0 ], types_go ._Options ()))
2847
2915
res = lib .OpenFile (filename .encode (ENCODE ), options )
2848
2916
err = res .err .decode (ENCODE )
2849
2917
if err == "" :
2850
- return File (res .idx ), None
2918
+ return File (res .val ), None
2851
2919
return None , Exception (err )
2852
2920
2853
2921
@@ -2865,11 +2933,11 @@ def open_reader(
2865
2933
Tuple[Optional[File], Optional[Exception]]: A tuple containing a File
2866
2934
object if successful, or None and an Exception if an error occurred.
2867
2935
"""
2868
- lib .OpenReader .restype , options = types_go ._OptionsResult , None
2936
+ lib .OpenReader .restype , options = types_go ._IntErrorResult , None
2869
2937
if len (opts ) > 0 :
2870
2938
options = byref (py_value_to_c (opts [0 ], types_go ._Options ()))
2871
2939
res = lib .OpenReader (cast (buffer , POINTER (c_ubyte )), len (buffer ), options )
2872
2940
err = res .err .decode (ENCODE )
2873
2941
if err == "" :
2874
- return File (res .idx ), None
2942
+ return File (res .val ), None
2875
2943
return None , Exception (err )
0 commit comments