Skip to content

Commit c7199a6

Browse files
committed
Add 4 new functions: get_col_style, get_col_visible, get_default_font and get_row_visible
- Update unit tests and docs for the function
1 parent 3c9a9d2 commit c7199a6

File tree

5 files changed

+179
-0
lines changed

5 files changed

+179
-0
lines changed

excelize.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,87 @@ def get_cell_value(
13441344
err = res.err.decode(ENCODE)
13451345
return res.val.decode(ENCODE), None if err == "" else Exception(err)
13461346

1347+
def get_col_style(self, sheet: str, col: str) -> Tuple[int, Optional[Exception]]:
1348+
"""
1349+
Get column style ID by given worksheet name and column name.
1350+
1351+
Args:
1352+
sheet (str): The worksheet name
1353+
col (str): The column name
1354+
1355+
Returns:
1356+
Tuple[int, Optional[Exception]]: A tuple containing the column style
1357+
ID and an exception if an error occurred, otherwise None.
1358+
"""
1359+
lib.GetColStyle.restype = types_go._IntErrorResult
1360+
res = lib.GetColStyle(self.file_index, sheet.encode(ENCODE), col.encode(ENCODE))
1361+
err = res.err.decode(ENCODE)
1362+
return res.val, None if err == "" else Exception(err)
1363+
1364+
def get_col_visible(self, sheet: str, col: str) -> Tuple[bool, Optional[Exception]]:
1365+
"""
1366+
Get visible of a single column by given worksheet name and column name.
1367+
1368+
Args:
1369+
sheet (str): The worksheet name
1370+
col (str): The column name
1371+
1372+
Returns:
1373+
Tuple[bool, Optional[Exception]]: A tuple containing the column
1374+
visible and an exception if an error occurred, otherwise None.
1375+
1376+
Example:
1377+
For example, get visible state of column D in Sheet1:
1378+
1379+
.. code-block:: python
1380+
1381+
visible, err = f.get_col_visible("Sheet1", "D")
1382+
"""
1383+
lib.GetColVisible.restype = types_go._BoolErrorResult
1384+
res = lib.GetColVisible(
1385+
self.file_index, sheet.encode(ENCODE), col.encode(ENCODE)
1386+
)
1387+
err = res.err.decode(ENCODE)
1388+
return res.val, None if err == "" else Exception(err)
1389+
1390+
def get_default_font(self) -> Tuple[str, Optional[Exception]]:
1391+
"""
1392+
Get the default font name currently set in the workbook. The spreadsheet
1393+
generated by excelize default font is Calibri.
1394+
1395+
Returns:
1396+
Tuple[str, Optional[Exception]]: A tuple containing the font name as
1397+
a string and an exception if an error occurred, otherwise None.
1398+
"""
1399+
lib.GetDefaultFont.restype = types_go._StringErrorResult
1400+
res = lib.GetDefaultFont(self.file_index)
1401+
err = res.err.decode(ENCODE)
1402+
return res.val.decode(ENCODE), None if err == "" else Exception(err)
1403+
1404+
def get_row_visible(self, sheet: str, row: int) -> Tuple[bool, Optional[Exception]]:
1405+
"""
1406+
Get visible of a single row by given worksheet name and Excel row number.
1407+
1408+
Args:
1409+
sheet (str): The worksheet name
1410+
col (str): The column name
1411+
1412+
Returns:
1413+
Tuple[bool, Optional[Exception]]: A tuple containing the column
1414+
visible and an exception if an error occurred, otherwise None.
1415+
1416+
Example:
1417+
For example, get visible state of row 2 in Sheet1:
1418+
1419+
.. code-block:: python
1420+
1421+
visible, err = f.get_row_visible("Sheet1", 2)
1422+
"""
1423+
lib.GetRowVisible.restype = types_go._BoolErrorResult
1424+
res = lib.GetRowVisible(self.file_index, sheet.encode(ENCODE), c_int(row))
1425+
err = res.err.decode(ENCODE)
1426+
return res.val, None if err == "" else Exception(err)
1427+
13471428
def get_rows(
13481429
self, sheet: str, *opts: Options
13491430
) -> Tuple[List[List[str]], Optional[Exception]]:

main.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,70 @@ func GetCellValue(idx int, sheet, cell *C.char, opts *C.struct_Options) C.struct
10981098
return C.struct_StringErrorResult{val: C.CString(val), err: C.CString(emptyString)}
10991099
}
11001100

1101+
// GetColStyle provides a function to get column style ID by given worksheet
1102+
// name and column name. This function is concurrency safe.
1103+
//
1104+
//export GetColStyle
1105+
func GetColStyle(idx int, sheet, col *C.char) C.struct_IntErrorResult {
1106+
f, ok := files.Load(idx)
1107+
if !ok {
1108+
return C.struct_IntErrorResult{val: C.int(0), err: C.CString(errFilePtr)}
1109+
}
1110+
val, err := f.(*excelize.File).GetColStyle(C.GoString(sheet), C.GoString(col))
1111+
if err != nil {
1112+
return C.struct_IntErrorResult{val: C.int(val), err: C.CString(err.Error())}
1113+
}
1114+
return C.struct_IntErrorResult{val: C.int(val), err: C.CString(emptyString)}
1115+
}
1116+
1117+
// GetColVisible provides a function to get visible of a single column by given
1118+
// worksheet name and column name. This function is concurrency safe.
1119+
//
1120+
//export GetColVisible
1121+
func GetColVisible(idx int, sheet, col *C.char) C.struct_BoolErrorResult {
1122+
f, ok := files.Load(idx)
1123+
if !ok {
1124+
return C.struct_BoolErrorResult{val: C._Bool(false), err: C.CString(errFilePtr)}
1125+
}
1126+
val, err := f.(*excelize.File).GetColVisible(C.GoString(sheet), C.GoString(col))
1127+
if err != nil {
1128+
return C.struct_BoolErrorResult{val: C._Bool(val), err: C.CString(err.Error())}
1129+
}
1130+
return C.struct_BoolErrorResult{val: C._Bool(val), err: C.CString(emptyString)}
1131+
}
1132+
1133+
// GetDefaultFont provides the default font name currently set in the
1134+
// workbook. The spreadsheet generated by excelize default font is Calibri.
1135+
//
1136+
//export GetDefaultFont
1137+
func GetDefaultFont(idx int) C.struct_StringErrorResult {
1138+
f, ok := files.Load(idx)
1139+
if !ok {
1140+
return C.struct_StringErrorResult{val: C.CString(emptyString), err: C.CString(errFilePtr)}
1141+
}
1142+
val, err := f.(*excelize.File).GetDefaultFont()
1143+
if err != nil {
1144+
return C.struct_StringErrorResult{val: C.CString(val), err: C.CString(err.Error())}
1145+
}
1146+
return C.struct_StringErrorResult{val: C.CString(val), err: C.CString(emptyString)}
1147+
}
1148+
1149+
// GetRowVisible provides a function to get visible of a single row by given
1150+
// worksheet name and Excel row number.
1151+
//
1152+
//export GetRowVisible
1153+
func GetRowVisible(idx int, sheet *C.char, row int) C.struct_BoolErrorResult {
1154+
f, ok := files.Load(idx)
1155+
if !ok {
1156+
return C.struct_BoolErrorResult{val: C._Bool(false), err: C.CString(errFilePtr)}
1157+
}
1158+
val, err := f.(*excelize.File).GetRowVisible(C.GoString(sheet), row)
1159+
if err != nil {
1160+
return C.struct_BoolErrorResult{val: C._Bool(val), err: C.CString(err.Error())}
1161+
}
1162+
return C.struct_BoolErrorResult{val: C._Bool(val), err: C.CString(emptyString)}
1163+
}
1164+
11011165
// GetRows return all the rows in a sheet by given worksheet name, returned as
11021166
// a two-dimensional array, where the value of the cell is converted to the
11031167
// string type. If the cell format can be applied to the value of the cell,

test_excelize.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ def test_default_font(self):
7373
f = excelize.new_file()
7474
font_name = "Arial"
7575
self.assertIsNone(f.set_default_font(font_name))
76+
val, err = f.get_default_font()
77+
self.assertEqual(val, font_name)
78+
self.assertIsNone(err)
7679

7780
def test_style(self):
7881
f = excelize.new_file()
@@ -127,9 +130,27 @@ def test_style(self):
127130
"sheet SheetN does not exist",
128131
)
129132
self.assertIsNone(f.set_col_style("Sheet1", "H", style_id))
133+
col_style, err = f.get_col_style("Sheet1", "H")
134+
self.assertEqual(col_style, style_id)
135+
self.assertIsNone(err)
136+
130137
self.assertIsNone(f.set_col_visible("Sheet1", "D:F", False))
138+
col_visible, err = f.get_col_visible("Sheet1", "E")
139+
self.assertFalse(col_visible)
140+
self.assertIsNone(err)
141+
col_visible, err = f.get_col_visible("Sheet1", "G")
142+
self.assertTrue(col_visible)
143+
self.assertIsNone(err)
144+
131145
self.assertIsNone(f.set_row_style("Sheet1", 1, 1, style_id))
146+
132147
self.assertIsNone(f.set_row_visible("Sheet1", 1, False))
148+
row_visible, err = f.get_row_visible("Sheet1", 1)
149+
self.assertFalse(row_visible)
150+
self.assertIsNone(err)
151+
row_visible, err = f.get_row_visible("Sheet1", 2)
152+
self.assertTrue(row_visible)
153+
self.assertIsNone(err)
133154

134155
style, err = f.get_style(2)
135156
self.assertEqual("invalid style ID 2", str(err))

types_c.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,12 @@ struct IntErrorResult
684684
char *err;
685685
};
686686

687+
struct BoolErrorResult
688+
{
689+
bool val;
690+
char *err;
691+
};
692+
687693
struct GetCellHyperLinkResult
688694
{
689695
bool link;

types_go.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,13 @@ class _IntErrorResult(Structure):
662662
]
663663

664664

665+
class _BoolErrorResult(Structure):
666+
_fields_ = [
667+
("val", c_bool),
668+
("err", c_char_p),
669+
]
670+
671+
665672
class _CellNameToCoordinatesResult(Structure):
666673
_fields_ = [
667674
("col", c_int),

0 commit comments

Comments
 (0)