Skip to content

Commit 7264f79

Browse files
Add new function: get_sheet_visible (#25)
- Update unit tests
1 parent c2bcaeb commit 7264f79

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

excelize.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ def add_chart(self, sheet: str, cell: str, chart: Chart, **combo: Chart) -> None
863863
864864
Set properties of the chart title. The properties that can be set are:
865865
866-
title
866+
title
867867
868868
title: Set the name (title) for the chart. The name is displayed above
869869
the chart. The name can also be a formula such as Sheet1!$A$1 or a list
@@ -2961,6 +2961,34 @@ def get_sheet_name(self, sheet: int) -> str:
29612961
return res.val.decode(ENCODE)
29622962
raise RuntimeError(err)
29632963

2964+
def get_sheet_visible(self, sheet: str) -> bool:
2965+
"""
2966+
Get visible state of the sheet by given sheet name.
2967+
2968+
Args:
2969+
sheet (str): The sheet name.
2970+
2971+
Returns:
2972+
bool: Return the sheet visible if no error occurred, otherwise raise
2973+
a RuntimeError with the message.
2974+
2975+
Example:
2976+
For example, get the visible state of "Sheet1":
2977+
2978+
```python
2979+
try:
2980+
visible = f.get_sheet_visible("Sheet1")
2981+
except RuntimeError as err:
2982+
print(err)
2983+
```
2984+
"""
2985+
lib.GetSheetVisible.restype = types_go._BoolErrorResult
2986+
res = lib.GetSheetVisible(self.file_index, sheet.encode(ENCODE))
2987+
err = res.err.decode(ENCODE)
2988+
if not err:
2989+
return res.val
2990+
raise RuntimeError(err)
2991+
29642992
def get_style(self, style_id: int) -> Optional[Style]:
29652993
"""
29662994
Get style definition by given style ID.

main.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,6 +1439,22 @@ func GetSheetProps(idx int, sheet *C.char) C.struct_GetSheetPropsResult {
14391439
return C.struct_GetSheetPropsResult{opts: cVal.Elem().Interface().(C.struct_SheetPropsOptions), err: C.CString(emptyString)}
14401440
}
14411441

1442+
// GetSheetVisible provides a function to get worksheet visible by given
1443+
// worksheet name.
1444+
//
1445+
//export GetSheetVisible
1446+
func GetSheetVisible(idx int, sheet *C.char) C.struct_BoolErrorResult {
1447+
f, ok := files.Load(idx)
1448+
if !ok {
1449+
return C.struct_BoolErrorResult{val: C._Bool(false), err: C.CString(errFilePtr)}
1450+
}
1451+
visible, err := f.(*excelize.File).GetSheetVisible(C.GoString(sheet))
1452+
if err != nil {
1453+
return C.struct_BoolErrorResult{val: C._Bool(visible), err: C.CString(err.Error())}
1454+
}
1455+
return C.struct_BoolErrorResult{val: C._Bool(visible), err: C.CString(emptyString)}
1456+
}
1457+
14421458
// GetStyle provides a function to get style definition by given style index.
14431459
//
14441460
//export GetStyle

test_excelize.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,6 +1571,14 @@ def test_sheet_visible(self):
15711571
str(context.exception),
15721572
"the sheet can not contain any of the characters :\/?*[or]",
15731573
)
1574+
self.assertTrue(f.get_sheet_visible("Sheet1"))
1575+
self.assertFalse(f.get_sheet_visible("Sheet2"))
1576+
with self.assertRaises(RuntimeError) as context:
1577+
f.get_sheet_visible("Sheet:1")
1578+
self.assertEqual(
1579+
str(context.exception),
1580+
"the sheet can not contain any of the characters :\/?*[or]",
1581+
)
15741582
self.assertIsNone(f.save_as(os.path.join("test", "TestSheetVisible.xlsx")))
15751583
self.assertIsNone(f.close())
15761584

0 commit comments

Comments
 (0)