File tree Expand file tree Collapse file tree 5 files changed +78
-10
lines changed Expand file tree Collapse file tree 5 files changed +78
-10
lines changed Original file line number Diff line number Diff line change @@ -2915,6 +2915,37 @@ def get_default_font(self) -> str:
29152915 return res .val .decode (ENCODE )
29162916 raise RuntimeError (err )
29172917
2918+ def get_defined_name (self ) -> List [DefinedName ]:
2919+ """
2920+ Get the defined names of the workbook or worksheet.
2921+
2922+ Returns:
2923+ List[DefinedName]: Return the defined names list if no error
2924+ occurred, otherwise raise a RuntimeError with the message.
2925+
2926+ Example:
2927+ For example:
2928+
2929+ ```python
2930+ try:
2931+ names = f.get_defined_name()
2932+ for dn in names:
2933+ print(dn.name, dn.refers_to, dn.scope, dn.comment)
2934+ except (RuntimeError, TypeError) as err:
2935+ print(err)
2936+ ```
2937+ """
2938+ lib .GetDefinedName .restype = types_go ._GetDefinedNameResult
2939+ res = lib .GetDefinedName (self .file_index )
2940+ err = res .Err .decode (ENCODE )
2941+ if err == "" :
2942+ arr = []
2943+ if res .DefinedNames :
2944+ for i in range (res .DefinedNamesLen ):
2945+ arr .append (c_value_to_py (res .DefinedNames [i ], DefinedName ()))
2946+ return arr
2947+ raise RuntimeError (err )
2948+
29182949 def get_row_height (self , sheet : str , row : int ) -> float :
29192950 """
29202951 Get row height by given worksheet name and row number.
Original file line number Diff line number Diff line change @@ -1229,6 +1229,27 @@ func GetDefaultFont(idx int) C.struct_StringErrorResult {
12291229 return C.struct_StringErrorResult {val : C .CString (val ), err : C .CString (emptyString )}
12301230}
12311231
1232+ // GetDefinedName provides a function to get the defined names of the workbook
1233+ // or worksheet.
1234+ //
1235+ //export GetDefinedName
1236+ func GetDefinedName (idx int ) C.struct_GetDefinedNameResult {
1237+ f , ok := files .Load (idx )
1238+ if ! ok {
1239+ return C.struct_GetDefinedNameResult {Err : C .CString (errFilePtr )}
1240+ }
1241+ definedNames := f .(* excelize.File ).GetDefinedName ()
1242+ cArray := C .malloc (C .size_t (len (definedNames )) * C .size_t (unsafe .Sizeof (C.struct_DefinedName {})))
1243+ for i , dn := range definedNames {
1244+ cVal , err := goValueToC (reflect .ValueOf (dn ), reflect .ValueOf (& C.struct_DefinedName {}))
1245+ if err != nil {
1246+ return C.struct_GetDefinedNameResult {Err : C .CString (err .Error ())}
1247+ }
1248+ * (* C .struct_DefinedName )(unsafe .Pointer (uintptr (unsafe .Pointer (cArray )) + uintptr (i )* unsafe .Sizeof (C.struct_DefinedName {}))) = cVal .Elem ().Interface ().(C.struct_DefinedName )
1249+ }
1250+ return C.struct_GetDefinedNameResult {DefinedNamesLen : C .int (len (definedNames )), DefinedNames : (* C .struct_DefinedName )(cArray ), Err : C .CString (emptyString )}
1251+ }
1252+
12321253// GetRowHeight provides a function to get row height by given worksheet name
12331254// and row number.
12341255//
Original file line number Diff line number Diff line change @@ -837,6 +837,9 @@ def test_none_file_pointer(self):
837837 with self .assertRaises (RuntimeError ) as context :
838838 f .get_default_font ()
839839 self .assertEqual (str (context .exception ), expected )
840+ with self .assertRaises (RuntimeError ) as context :
841+ f .get_defined_name ()
842+ self .assertEqual (str (context .exception ), expected )
840843 with self .assertRaises (RuntimeError ) as context :
841844 f .get_sheet_name (0 )
842845 self .assertEqual (str (context .exception ), expected )
@@ -2082,16 +2085,14 @@ def test_add_picture(self):
20822085
20832086 def test_defined_name (self ):
20842087 f = excelize .new_file ()
2085- self .assertIsNone (
2086- f .set_defined_name (
2087- excelize .DefinedName (
2088- name = "Amount" ,
2089- refers_to = "Sheet1!$A$2:$D$5" ,
2090- comment = "defined name comment" ,
2091- scope = "Sheet1" ,
2092- )
2093- )
2094- )
2088+ df = excelize .DefinedName (
2089+ name = "Amount" ,
2090+ refers_to = "Sheet1!$A$2:$D$5" ,
2091+ comment = "defined name comment" ,
2092+ scope = "Sheet1" ,
2093+ )
2094+ self .assertIsNone (f .set_defined_name (df ))
2095+ self .assertEqual (f .get_defined_name (), [df ])
20952096 self .assertIsNone (
20962097 f .delete_defined_name (
20972098 excelize .DefinedName (
Original file line number Diff line number Diff line change @@ -848,3 +848,10 @@ struct GetCommentsResult
848848 struct Comment * Comments ;
849849 char * Err ;
850850};
851+
852+ struct GetDefinedNameResult
853+ {
854+ int DefinedNamesLen ;
855+ struct DefinedName * DefinedNames ;
856+ char * Err ;
857+ };
Original file line number Diff line number Diff line change @@ -835,3 +835,11 @@ class _GetCommentsResult(Structure):
835835 ("Comments" , POINTER (_Comment )),
836836 ("Err" , c_char_p ),
837837 ]
838+
839+
840+ class _GetDefinedNameResult (Structure ):
841+ _fields_ = [
842+ ("DefinedNamesLen" , c_int ),
843+ ("DefinedNames" , POINTER (_DefinedName )),
844+ ("Err" , c_char_p ),
845+ ]
You can’t perform that action at this time.
0 commit comments