Skip to content

Commit 6ed4788

Browse files
Add new function: get_sheet_props (#23)
- Update unit tests - Upgrade the dependencies package version
1 parent b3cba35 commit 6ed4788

File tree

8 files changed

+59
-4
lines changed

8 files changed

+59
-4
lines changed

excelize.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2059,6 +2059,21 @@ def get_workbook_props(self) -> WorkbookPropsOptions:
20592059
return c_value_to_py(res.opts, WorkbookPropsOptions())
20602060
raise RuntimeError(err)
20612061

2062+
def get_sheet_props(self, sheet_name: str) -> Optional[SheetPropsOptions]:
2063+
"""
2064+
Get worksheet properties.
2065+
2066+
Returns:
2067+
Optional[SheetPropsOptions]: Return the sheet property options if no
2068+
error occurred, otherwise raise a RuntimeError with the message.
2069+
"""
2070+
lib.GetSheetProps.restype = types_go._GetSheetPropsResult
2071+
res = lib.GetSheetProps(self.file_index, sheet_name.encode(ENCODE))
2072+
err = res.err.decode(ENCODE)
2073+
if not err:
2074+
return c_value_to_py(res.opts, SheetPropsOptions())
2075+
raise RuntimeError(err)
2076+
20622077
def group_sheets(self, sheets: List[str]) -> None:
20632078
"""
20642079
Group worksheets by given worksheets name. Group worksheets must contain

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/xuri/excelize-py
33
go 1.23.0
44

55
require (
6-
github.com/xuri/excelize/v2 v2.9.2-0.20250517173208-bb1105b08996
6+
github.com/xuri/excelize/v2 v2.9.2-0.20250527011656-c46099ee67be
77
golang.org/x/image v0.27.0
88
)
99

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ github.com/tiendc/go-deepcopy v1.6.0 h1:0UtfV/imoCwlLxVsyfUd4hNHnB3drXsfle+wzSCA
1313
github.com/tiendc/go-deepcopy v1.6.0/go.mod h1:toXoeQoUqXOOS/X4sKuiAoSk6elIdqc0pN7MTgOOo2I=
1414
github.com/xuri/efp v0.0.1 h1:fws5Rv3myXyYni8uwj2qKjVaRP30PdjeYe2Y6FDsCL8=
1515
github.com/xuri/efp v0.0.1/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
16-
github.com/xuri/excelize/v2 v2.9.2-0.20250517173208-bb1105b08996 h1:+LFbX4Ds4p6O13TrQJODD8UnG3J/Q1whc26C4M/vo8E=
17-
github.com/xuri/excelize/v2 v2.9.2-0.20250517173208-bb1105b08996/go.mod h1:x7L6pKz2dvo9ejrRuD8Lnl98z4JLt0TGAwjhW+EiP8s=
16+
github.com/xuri/excelize/v2 v2.9.2-0.20250527011656-c46099ee67be h1:hPXzrTtRmS2byp4c5RRKmj4vYahw5Yw0+ZGVfMojI00=
17+
github.com/xuri/excelize/v2 v2.9.2-0.20250527011656-c46099ee67be/go.mod h1:x7L6pKz2dvo9ejrRuD8Lnl98z4JLt0TGAwjhW+EiP8s=
1818
github.com/xuri/nfp v0.0.1 h1:MDamSGatIvp8uOmDP8FnmjuQpu90NzdJxo7242ANR9Q=
1919
github.com/xuri/nfp v0.0.1/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
2020
golang.org/x/crypto v0.38.0 h1:jt+WWG8IZlBnVbomuhg2Mdq0+BBQaHbtqHEFEigjUV8=

main.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ var (
111111
return reflect.ValueOf(C.uint(uint32(goVal.Uint()))), nil
112112
},
113113
reflect.Uint8: func(goVal reflect.Value, kind reflect.Kind) (reflect.Value, error) {
114-
return reflect.ValueOf(C.uchar(int8(goVal.Uint()))), nil
114+
return reflect.ValueOf(C.uint(uint32(goVal.Uint()))), nil
115115
},
116116
reflect.Uint32: func(goVal reflect.Value, kind reflect.Kind) (reflect.Value, error) {
117117
return reflect.ValueOf(C.uint(uint32(goVal.Uint()))), nil
@@ -1388,6 +1388,25 @@ func GetSheetName(idx int, sheetIndex int) C.struct_StringErrorResult {
13881388
return C.struct_StringErrorResult{val: C.CString(f.(*excelize.File).GetSheetName(sheetIndex)), err: C.CString(emptyString)}
13891389
}
13901390

1391+
// GetSheetProps provides a function to get worksheet properties.
1392+
//
1393+
//export GetSheetProps
1394+
func GetSheetProps(idx int, sheet *C.char) C.struct_GetSheetPropsResult {
1395+
f, ok := files.Load(idx)
1396+
if !ok {
1397+
return C.struct_GetSheetPropsResult{err: C.CString(errFilePtr)}
1398+
}
1399+
opts, err := f.(*excelize.File).GetSheetProps(C.GoString(sheet))
1400+
if err != nil {
1401+
return C.struct_GetSheetPropsResult{err: C.CString(err.Error())}
1402+
}
1403+
cVal, err := goValueToC(reflect.ValueOf(opts), reflect.ValueOf(&C.struct_SheetPropsOptions{}))
1404+
if err != nil {
1405+
return C.struct_GetSheetPropsResult{err: C.CString(err.Error())}
1406+
}
1407+
return C.struct_GetSheetPropsResult{opts: cVal.Elem().Interface().(C.struct_SheetPropsOptions), err: C.CString(emptyString)}
1408+
}
1409+
13911410
// GetStyle provides a function to get style definition by given style index.
13921411
//
13931412
//export GetStyle

test_excelize.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,11 @@ def test_add_form_control(self):
833833
self.assertIsNone(
834834
f.set_sheet_props("Sheet1", excelize.SheetPropsOptions(code_name="Sheet1"))
835835
)
836+
props = f.get_sheet_props("Sheet1")
837+
self.assertEqual(props.code_name, "Sheet1")
838+
with self.assertRaises(RuntimeError) as context:
839+
f.get_sheet_props("SheetN")
840+
self.assertEqual(str(context.exception), "sheet SheetN does not exist")
836841
with self.assertRaises(RuntimeError) as context:
837842
f.set_sheet_props("SheetN", excelize.SheetPropsOptions(code_name="Sheet1"))
838843
self.assertEqual(str(context.exception), "sheet SheetN does not exist")

types_c.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ struct Font
152152
int *ColorTheme;
153153
double ColorTint;
154154
char *VertAlign;
155+
int *Charset;
155156
};
156157

157158
// Alignment directly maps the alignment settings of the cells.
@@ -807,6 +808,12 @@ struct GetWorkbookPropsResult
807808
char *err;
808809
};
809810

811+
struct GetSheetPropsResult
812+
{
813+
struct SheetPropsOptions opts;
814+
char *err;
815+
};
816+
810817
struct GetSheetMapResult {
811818
int ArrLen;
812819
struct IntStringResult *Arr;

types_go.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ class _Font(Structure):
127127
("ColorTheme", POINTER(c_int)),
128128
("ColorTint", c_double),
129129
("VertAlign", c_char_p),
130+
("Charset", POINTER(c_int)),
130131
]
131132

132133

@@ -797,6 +798,13 @@ class _GetWorkbookPropsResult(Structure):
797798
]
798799

799800

801+
class _GetSheetPropsResult(Structure):
802+
_fields_ = [
803+
("opts", _SheetPropsOptions),
804+
("err", c_char_p),
805+
]
806+
807+
800808
class _GetSheetMapResult(Structure):
801809
_fields_ = [
802810
("ArrLen", c_int),

types_py.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ class Font:
265265
color_theme: Optional[int] = None
266266
color_tint: float = 0
267267
vert_align: str = ""
268+
charset: Optional[int] = None
268269

269270

270271
@dataclass

0 commit comments

Comments
 (0)