Skip to content

Commit

Permalink
Add 4 new functions: get_col_style, get_col_visible, get_default_font…
Browse files Browse the repository at this point in the history
… and get_row_visible

- Update unit tests and docs for the function
  • Loading branch information
xuri committed Jan 6, 2025
1 parent 3c9a9d2 commit c7199a6
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 0 deletions.
81 changes: 81 additions & 0 deletions excelize.py
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,87 @@ def get_cell_value(
err = res.err.decode(ENCODE)
return res.val.decode(ENCODE), None if err == "" else Exception(err)

def get_col_style(self, sheet: str, col: str) -> Tuple[int, Optional[Exception]]:
"""
Get column style ID by given worksheet name and column name.
Args:
sheet (str): The worksheet name
col (str): The column name
Returns:
Tuple[int, Optional[Exception]]: A tuple containing the column style
ID and an exception if an error occurred, otherwise None.
"""
lib.GetColStyle.restype = types_go._IntErrorResult
res = lib.GetColStyle(self.file_index, sheet.encode(ENCODE), col.encode(ENCODE))
err = res.err.decode(ENCODE)
return res.val, None if err == "" else Exception(err)

def get_col_visible(self, sheet: str, col: str) -> Tuple[bool, Optional[Exception]]:
"""
Get visible of a single column by given worksheet name and column name.
Args:
sheet (str): The worksheet name
col (str): The column name
Returns:
Tuple[bool, Optional[Exception]]: A tuple containing the column
visible and an exception if an error occurred, otherwise None.
Example:
For example, get visible state of column D in Sheet1:
.. code-block:: python
visible, err = f.get_col_visible("Sheet1", "D")
"""
lib.GetColVisible.restype = types_go._BoolErrorResult
res = lib.GetColVisible(
self.file_index, sheet.encode(ENCODE), col.encode(ENCODE)
)
err = res.err.decode(ENCODE)
return res.val, None if err == "" else Exception(err)

def get_default_font(self) -> Tuple[str, Optional[Exception]]:
"""
Get the default font name currently set in the workbook. The spreadsheet
generated by excelize default font is Calibri.
Returns:
Tuple[str, Optional[Exception]]: A tuple containing the font name as
a string and an exception if an error occurred, otherwise None.
"""
lib.GetDefaultFont.restype = types_go._StringErrorResult
res = lib.GetDefaultFont(self.file_index)
err = res.err.decode(ENCODE)
return res.val.decode(ENCODE), None if err == "" else Exception(err)

def get_row_visible(self, sheet: str, row: int) -> Tuple[bool, Optional[Exception]]:
"""
Get visible of a single row by given worksheet name and Excel row number.
Args:
sheet (str): The worksheet name
col (str): The column name
Returns:
Tuple[bool, Optional[Exception]]: A tuple containing the column
visible and an exception if an error occurred, otherwise None.
Example:
For example, get visible state of row 2 in Sheet1:
.. code-block:: python
visible, err = f.get_row_visible("Sheet1", 2)
"""
lib.GetRowVisible.restype = types_go._BoolErrorResult
res = lib.GetRowVisible(self.file_index, sheet.encode(ENCODE), c_int(row))
err = res.err.decode(ENCODE)
return res.val, None if err == "" else Exception(err)

def get_rows(
self, sheet: str, *opts: Options
) -> Tuple[List[List[str]], Optional[Exception]]:
Expand Down
64 changes: 64 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,70 @@ func GetCellValue(idx int, sheet, cell *C.char, opts *C.struct_Options) C.struct
return C.struct_StringErrorResult{val: C.CString(val), err: C.CString(emptyString)}
}

// GetColStyle provides a function to get column style ID by given worksheet
// name and column name. This function is concurrency safe.
//
//export GetColStyle
func GetColStyle(idx int, sheet, col *C.char) C.struct_IntErrorResult {
f, ok := files.Load(idx)
if !ok {
return C.struct_IntErrorResult{val: C.int(0), err: C.CString(errFilePtr)}
}
val, err := f.(*excelize.File).GetColStyle(C.GoString(sheet), C.GoString(col))
if err != nil {
return C.struct_IntErrorResult{val: C.int(val), err: C.CString(err.Error())}
}
return C.struct_IntErrorResult{val: C.int(val), err: C.CString(emptyString)}
}

// GetColVisible provides a function to get visible of a single column by given
// worksheet name and column name. This function is concurrency safe.
//
//export GetColVisible
func GetColVisible(idx int, sheet, col *C.char) C.struct_BoolErrorResult {
f, ok := files.Load(idx)
if !ok {
return C.struct_BoolErrorResult{val: C._Bool(false), err: C.CString(errFilePtr)}
}
val, err := f.(*excelize.File).GetColVisible(C.GoString(sheet), C.GoString(col))
if err != nil {
return C.struct_BoolErrorResult{val: C._Bool(val), err: C.CString(err.Error())}
}
return C.struct_BoolErrorResult{val: C._Bool(val), err: C.CString(emptyString)}
}

// GetDefaultFont provides the default font name currently set in the
// workbook. The spreadsheet generated by excelize default font is Calibri.
//
//export GetDefaultFont
func GetDefaultFont(idx int) C.struct_StringErrorResult {
f, ok := files.Load(idx)
if !ok {
return C.struct_StringErrorResult{val: C.CString(emptyString), err: C.CString(errFilePtr)}
}
val, err := f.(*excelize.File).GetDefaultFont()
if err != nil {
return C.struct_StringErrorResult{val: C.CString(val), err: C.CString(err.Error())}
}
return C.struct_StringErrorResult{val: C.CString(val), err: C.CString(emptyString)}
}

// GetRowVisible provides a function to get visible of a single row by given
// worksheet name and Excel row number.
//
//export GetRowVisible
func GetRowVisible(idx int, sheet *C.char, row int) C.struct_BoolErrorResult {
f, ok := files.Load(idx)
if !ok {
return C.struct_BoolErrorResult{val: C._Bool(false), err: C.CString(errFilePtr)}
}
val, err := f.(*excelize.File).GetRowVisible(C.GoString(sheet), row)
if err != nil {
return C.struct_BoolErrorResult{val: C._Bool(val), err: C.CString(err.Error())}
}
return C.struct_BoolErrorResult{val: C._Bool(val), err: C.CString(emptyString)}
}

// GetRows return all the rows in a sheet by given worksheet name, returned as
// a two-dimensional array, where the value of the cell is converted to the
// string type. If the cell format can be applied to the value of the cell,
Expand Down
21 changes: 21 additions & 0 deletions test_excelize.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ def test_default_font(self):
f = excelize.new_file()
font_name = "Arial"
self.assertIsNone(f.set_default_font(font_name))
val, err = f.get_default_font()
self.assertEqual(val, font_name)
self.assertIsNone(err)

def test_style(self):
f = excelize.new_file()
Expand Down Expand Up @@ -127,9 +130,27 @@ def test_style(self):
"sheet SheetN does not exist",
)
self.assertIsNone(f.set_col_style("Sheet1", "H", style_id))
col_style, err = f.get_col_style("Sheet1", "H")
self.assertEqual(col_style, style_id)
self.assertIsNone(err)

self.assertIsNone(f.set_col_visible("Sheet1", "D:F", False))
col_visible, err = f.get_col_visible("Sheet1", "E")
self.assertFalse(col_visible)
self.assertIsNone(err)
col_visible, err = f.get_col_visible("Sheet1", "G")
self.assertTrue(col_visible)
self.assertIsNone(err)

self.assertIsNone(f.set_row_style("Sheet1", 1, 1, style_id))

self.assertIsNone(f.set_row_visible("Sheet1", 1, False))
row_visible, err = f.get_row_visible("Sheet1", 1)
self.assertFalse(row_visible)
self.assertIsNone(err)
row_visible, err = f.get_row_visible("Sheet1", 2)
self.assertTrue(row_visible)
self.assertIsNone(err)

style, err = f.get_style(2)
self.assertEqual("invalid style ID 2", str(err))
Expand Down
6 changes: 6 additions & 0 deletions types_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,12 @@ struct IntErrorResult
char *err;
};

struct BoolErrorResult
{
bool val;
char *err;
};

struct GetCellHyperLinkResult
{
bool link;
Expand Down
7 changes: 7 additions & 0 deletions types_go.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,13 @@ class _IntErrorResult(Structure):
]


class _BoolErrorResult(Structure):
_fields_ = [
("val", c_bool),
("err", c_char_p),
]


class _CellNameToCoordinatesResult(Structure):
_fields_ = [
("col", c_int),
Expand Down

0 comments on commit c7199a6

Please sign in to comment.