Skip to content

Commit 0e11565

Browse files
committed
Add 3 new functions: get_sheet_dimension, get_sheet_index and insert_cols
- Update unit tests and docs for the function - Combine structures for fields with the same data types - Fix get tables error - Update GitHub action config, run tests on Linux
1 parent c37440a commit 0e11565

File tree

7 files changed

+306
-277
lines changed

7 files changed

+306
-277
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ jobs:
4646
- name: Test on Linux
4747
env:
4848
CGO_ENABLED: 1
49-
if: matrix.os == 'ubuntu-latest'
49+
if: matrix.os == 'ubuntu-24.04'
5050
run: go build -buildmode=c-shared -o libexcelize.amd64.linux.so main.go && coverage run -m unittest
5151

5252
- name: Test on macOS

excelize.py

Lines changed: 88 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ def calc_cell_value(
10011001
result as a string and an exception if an error occurred, otherwise
10021002
None.
10031003
"""
1004-
lib.CalcCellValue.restype = types_go._CalcCellValueResult
1004+
lib.CalcCellValue.restype = types_go._StringErrorResult
10051005
options = (
10061006
byref(py_value_to_c(opts[0], types_go._Options()))
10071007
if opts
@@ -1222,7 +1222,7 @@ def get_cell_formula(
12221222
Tuple[str, Optional[Exception]]: A tuple containing the cell formula
12231223
string and an exception if an error occurred, otherwise None.
12241224
"""
1225-
lib.GetCellFormula.restype = types_go._GetCellFormulaResult
1225+
lib.GetCellFormula.restype = types_go._StringErrorResult
12261226
res = lib.GetCellFormula(
12271227
self.file_index, sheet.encode(ENCODE), cell.encode(ENCODE)
12281228
)
@@ -1285,7 +1285,7 @@ def get_cell_value(
12851285
Tuple[str, Optional[Exception]]: A tuple containing the cell value
12861286
as a string and an exception if an error occurred, otherwise None.
12871287
"""
1288-
lib.GetCellValue.restype = types_go._GetCellValueResult
1288+
lib.GetCellValue.restype = types_go._StringErrorResult
12891289
options = (
12901290
byref(py_value_to_c(opts[0], types_go._Options()))
12911291
if opts
@@ -1336,6 +1336,41 @@ def get_rows(
13361336

13371337
return rows, None if err == "" else Exception(err)
13381338

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+
13391374
def get_style(self, style_id: int) -> Tuple[Optional[Style], Optional[Exception]]:
13401375
"""
13411376
Get style definition by given style index.
@@ -1369,9 +1404,42 @@ def get_tables(self, sheet: str) -> Tuple[List[Table], Optional[Exception]]:
13691404
lib.GetTables.restype = types_go._GetTablesResult
13701405
res = lib.GetTables(self.file_index, sheet.encode(ENCODE))
13711406
tables = c_value_to_py(res, GetTablesResult()).tables
1372-
err = res.err.decode(ENCODE)
1407+
err = res.Err.decode(ENCODE)
13731408
return tables, None if err == "" else Exception(err)
13741409

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+
13751443
def merge_cell(
13761444
self, sheet: str, top_left_cell: str, bottom_right_cell: str
13771445
) -> Optional[Exception]:
@@ -1440,11 +1508,11 @@ def new_conditional_style(self, style: Style) -> Tuple[int, Optional[Exception]]
14401508
Tuple[int, Optional[Exception]]: A tuple containing the style index
14411509
and an exception if any error occurs.
14421510
"""
1443-
lib.NewConditionalStyle.restype = types_go._NewStyleResult
1511+
lib.NewConditionalStyle.restype = types_go._IntErrorResult
14441512
options = py_value_to_c(style, types_go._Style())
14451513
res = lib.NewConditionalStyle(self.file_index, byref(options))
14461514
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)
14481516

14491517
def new_sheet(self, sheet: str) -> Tuple[int, Optional[Exception]]:
14501518
"""
@@ -1459,10 +1527,10 @@ def new_sheet(self, sheet: str) -> Tuple[int, Optional[Exception]]:
14591527
Tuple[int, Optional[Exception]]: A tuple containing the index of the
14601528
new sheet and an Exception if an error occurred, otherwise None.
14611529
"""
1462-
lib.NewSheet.restype = types_go._NewSheetResult
1530+
lib.NewSheet.restype = types_go._IntErrorResult
14631531
res = lib.NewSheet(self.file_index, sheet.encode(ENCODE))
14641532
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)
14661534

14671535
def new_style(self, style: Style) -> Tuple[int, Optional[Exception]]:
14681536
"""
@@ -1478,11 +1546,11 @@ def new_style(self, style: Style) -> Tuple[int, Optional[Exception]]:
14781546
Tuple[int, Optional[Exception]]: A tuple containing the style index
14791547
and an exception if any error occurs.
14801548
"""
1481-
lib.NewStyle.restype = types_go._NewStyleResult
1549+
lib.NewStyle.restype = types_go._IntErrorResult
14821550
options = py_value_to_c(style, types_go._Style())
14831551
res = lib.NewStyle(self.file_index, byref(options))
14841552
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)
14861554

14871555
def protect_sheet(
14881556
self, sheet: str, opts: SheetProtectionOptions
@@ -2767,10 +2835,10 @@ def column_name_to_number(name: str) -> Tuple[int, Optional[Exception]]:
27672835
Tuple[int, Optional[Exception]]: A tuple containing the column number
27682836
and an Exception if an error occurred, otherwise None.
27692837
"""
2770-
lib.ColumnNameToNumber.restype = types_go._ColumnNameToNumberResult
2838+
lib.ColumnNameToNumber.restype = types_go._IntErrorResult
27712839
res = lib.ColumnNameToNumber(name.encode(ENCODE))
27722840
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)
27742842

27752843

27762844
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]]:
27842852
Tuple[str, Optional[Exception]]: A tuple containing the column name and
27852853
an Exception if an error occurred, otherwise None.
27862854
"""
2787-
lib.ColumnNumberToName.restype = types_go._ColumnNumberToNameResult
2855+
lib.ColumnNumberToName.restype = types_go._StringErrorResult
27882856
res = lib.ColumnNumberToName(c_int(num))
27892857
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)
27912859

27922860

27932861
def coordinates_to_cell_name(
@@ -2807,13 +2875,13 @@ def coordinates_to_cell_name(
28072875
Tuple[str, Optional[Exception]]: A tuple containing the cell name as a
28082876
string and an Exception if an error occurred, otherwise None.
28092877
"""
2810-
lib.CoordinatesToCellName.restype = types_go._CoordinatesToCellNameResult
2878+
lib.CoordinatesToCellName.restype = types_go._StringErrorResult
28112879
options = False
28122880
if len(abs) > 0:
28132881
options = abs[0]
28142882
res = lib.CoordinatesToCellName(col, row, options)
28152883
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)
28172885

28182886

28192887
def new_file() -> File:
@@ -2841,13 +2909,13 @@ def open_file(
28412909
Tuple[Optional[File], Optional[Exception]]: A tuple containing a File
28422910
object if successful, or None and an Exception if an error occurred.
28432911
"""
2844-
lib.OpenFile.restype, options = types_go._OptionsResult, None
2912+
lib.OpenFile.restype, options = types_go._IntErrorResult, None
28452913
if len(opts) > 0:
28462914
options = byref(py_value_to_c(opts[0], types_go._Options()))
28472915
res = lib.OpenFile(filename.encode(ENCODE), options)
28482916
err = res.err.decode(ENCODE)
28492917
if err == "":
2850-
return File(res.idx), None
2918+
return File(res.val), None
28512919
return None, Exception(err)
28522920

28532921

@@ -2865,11 +2933,11 @@ def open_reader(
28652933
Tuple[Optional[File], Optional[Exception]]: A tuple containing a File
28662934
object if successful, or None and an Exception if an error occurred.
28672935
"""
2868-
lib.OpenReader.restype, options = types_go._OptionsResult, None
2936+
lib.OpenReader.restype, options = types_go._IntErrorResult, None
28692937
if len(opts) > 0:
28702938
options = byref(py_value_to_c(opts[0], types_go._Options()))
28712939
res = lib.OpenReader(cast(buffer, POINTER(c_ubyte)), len(buffer), options)
28722940
err = res.err.decode(ENCODE)
28732941
if err == "":
2874-
return File(res.idx), None
2942+
return File(res.val), None
28752943
return None, Exception(err)

0 commit comments

Comments
 (0)