Skip to content

Commit

Permalink
Add 3 new functions: set_col_style, set_col_visible and set_col_width
Browse files Browse the repository at this point in the history
- Update unit tests and docs for the function
  • Loading branch information
xuri committed Dec 24, 2024
1 parent 1ac3c8f commit e49e57c
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
94 changes: 94 additions & 0 deletions excelize.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
byref,
c_char_p,
c_char,
c_double,
c_int,
c_ubyte,
cast,
Expand Down Expand Up @@ -1855,6 +1856,99 @@ def set_col_outline_level(
).decode(ENCODE)
return None if err == "" else Exception(err)

def set_col_style(
self, sheet: str, columns: str, style_id: int
) -> Optional[Exception]:
"""
Set style of columns by given worksheet name, columns range and style
ID. This function is concurrency safe. Note that this will overwrite the
existing styles for the columns, it won't append or merge style with
existing styles.
Args:
sheet (str): The worksheet name
columns (str): The columns range
style_id (int): The style ID
Returns:
Optional[Exception]: Returns None if no error occurred,
otherwise returns an Exception with the message.
Example:
For example set style of column H on Sheet1:
.. code-block:: python
err = f.set_col_style("Sheet1", "H", style)
"""
lib.SetColStyle.restype = c_char_p
err = lib.SetColStyle(
self.file_index, sheet.encode(ENCODE), columns.encode(ENCODE), style_id
).decode(ENCODE)
return None if err == "" else Exception(err)

def set_col_visible(
self, sheet: str, columns: str, visible: bool
) -> Optional[Exception]:
"""
Set visible columns by given worksheet name, columns range and
visibility.
Args:
sheet (str): The worksheet name
columns (str): The columns range
visible (bool): The column visible
Returns:
Optional[Exception]: Returns None if no error occurred,
otherwise returns an Exception with the message.
Example:
For example hide column D on Sheet1:
.. code-block:: python
err = f.set_col_visible("Sheet1", "D", False)
"""
lib.SetColVisible.restype = c_char_p
err = lib.SetColVisible(
self.file_index, sheet.encode(ENCODE), columns.encode(ENCODE), visible
).decode(ENCODE)
return None if err == "" else Exception(err)

def set_col_width(
self, sheet: str, start_col: str, end_col: str, width: float
) -> Optional[Exception]:
"""
Set the width of a single column or multiple columns.
Args:
sheet (str): The worksheet name
start_col (str): The start column name
end_col (bool): The end column name
width (float): The column width
Returns:
Optional[Exception]: Returns None if no error occurred,
otherwise returns an Exception with the message.
Example:
For example set column width for column A to H on Sheet1:
.. code-block:: python
err = f.set_col_width("Sheet1", "A", "H", 20)
"""
lib.SetColWidth.restype = c_char_p
err = lib.SetColWidth(
self.file_index,
sheet.encode(ENCODE),
start_col.encode(ENCODE),
end_col.encode(ENCODE),
c_double(width),
).decode(ENCODE)
return None if err == "" else Exception(err)

def set_defined_name(self, defined_name: DefinedName) -> Optional[Exception]:
"""
Set the defined names of the workbook or worksheet. If not specified
Expand Down
47 changes: 47 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1497,6 +1497,53 @@ func SetColOutlineLevel(idx int, sheet, col *C.char, level int) *C.char {
return C.CString(errNil)
}

// SetColStyle provides a function to set style of columns by given worksheet
// name, columns range and style ID. This function is concurrency safe. Note
// that this will overwrite the existing styles for the columns, it won't
// append or merge style with existing styles.
//
//export SetColStyle
func SetColStyle(idx int, sheet, columns *C.char, styleID int) *C.char {
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
if err := f.(*excelize.File).SetColStyle(C.GoString(sheet), C.GoString(columns), styleID); err != nil {
return C.CString(err.Error())
}
return C.CString(errNil)
}

// SetColVisible provides a function to set visible columns by given worksheet
// name, columns range and visibility. This function is concurrency safe.
//
//export SetColVisible
func SetColVisible(idx int, sheet, columns *C.char, visible bool) *C.char {
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
if err := f.(*excelize.File).SetColVisible(C.GoString(sheet), C.GoString(columns), visible); err != nil {
return C.CString(err.Error())
}
return C.CString(errNil)
}

// SetColWidth provides a function to set the width of a single column or
// multiple columns. This function is concurrency safe.
//
//export SetColWidth
func SetColWidth(idx int, sheet, startCol, endCol *C.char, width float64) *C.char {
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
if err := f.(*excelize.File).SetColWidth(C.GoString(sheet), C.GoString(startCol), C.GoString(endCol), width); err != nil {
return C.CString(err.Error())
}
return C.CString(errNil)
}

// SetDefinedName provides a function to set the defined names of the workbook
// or worksheet. If not specified scope, the default scope is workbook.
//
Expand Down
3 changes: 3 additions & 0 deletions test_excelize.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ def test_style(self):
str(f.set_cell_style("SheetN", "A1", "B2", style_id)),
"sheet SheetN does not exist",
)
self.assertIsNone(f.set_col_style("Sheet1", "H", style_id))
self.assertIsNone(f.set_col_visible("Sheet1", "D:F", False))

style, err = f.get_style(2)
self.assertEqual("invalid style ID 2", str(err))
Expand Down Expand Up @@ -712,6 +714,7 @@ def test_cell_hyperlink(self):

def test_cell_rich_text(self):
f = excelize.new_file()
self.assertIsNone(f.set_col_width("Sheet1", "A", "A", 44))
self.assertIsNone(
f.set_cell_rich_text(
"Sheet1",
Expand Down

0 comments on commit e49e57c

Please sign in to comment.