Skip to content

Commit 336f5ed

Browse files
authored
Add new function: get_sheet_name (#13)
1 parent df5dc0e commit 336f5ed

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
on: [push, pull_request]
1+
on: [push, pull_request, release]
22
name: build
33

44
jobs:

excelize.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,6 +1740,23 @@ def get_sheet_index(self, sheet: str) -> Tuple[int, Optional[Exception]]:
17401740
err = res.err.decode(ENCODE)
17411741
return res.val, None if err == "" else Exception(err)
17421742

1743+
def get_sheet_name(self, sheet: int) -> str:
1744+
"""
1745+
Get the sheet name of the workbook by the given sheet index.
1746+
If the given sheet index is invalid or the sheet doesn't exist,
1747+
it will return an empty string.
1748+
1749+
Args:
1750+
sheet (int): The worksheet index
1751+
1752+
Returns:
1753+
str: The sheet name if the index is valid, otherwise an empty string.
1754+
"""
1755+
lib.GetSheetName.restype = types_go._StringErrorResult
1756+
res = lib.GetSheetName(self.file_index, c_int(sheet))
1757+
err = res.err.decode(ENCODE)
1758+
return res.val.decode(ENCODE) if err == "" else ""
1759+
17431760
def get_style(self, style_id: int) -> Tuple[Optional[Style], Optional[Exception]]:
17441761
"""
17451762
Get style definition by given style index.

main.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,18 @@ func GetSheetIndex(idx int, sheet *C.char) C.struct_IntErrorResult {
12871287
return C.struct_IntErrorResult{val: C.int(idx), err: C.CString(emptyString)}
12881288
}
12891289

1290+
// GetSheetName provides a function to get the sheet name by the given worksheet index.
1291+
// If the given worksheet index is invalid, it will return an error.
1292+
//
1293+
//export GetSheetName
1294+
func GetSheetName(idx int, sheetIndex int) C.struct_StringErrorResult {
1295+
f, ok := files.Load(idx)
1296+
if !ok {
1297+
return C.struct_StringErrorResult{val: C.CString(emptyString), err: C.CString(errFilePtr)}
1298+
}
1299+
return C.struct_StringErrorResult{val: C.CString(f.(*excelize.File).GetSheetName(sheetIndex)), err: C.CString(emptyString)}
1300+
}
1301+
12901302
// GetStyle provides a function to get style definition by given style index.
12911303
//
12921304
//export GetStyle

test_excelize.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ def test_style(self):
293293
index, err = f.get_sheet_index("Sheet2")
294294
self.assertEqual(idx, index)
295295
self.assertIsNone(err)
296+
self.assertEqual(f.get_sheet_name(index), "Sheet2")
296297

297298
self.assertIsNone(f.set_col_outline_level("Sheet1", "D", 2))
298299
level, err = f.get_col_outline_level("Sheet1", "D")

0 commit comments

Comments
 (0)