Calc Style Cell
Overview
Applying Cell/Range Styles can be accomplished using the ooodev.format.calc.style.Cell
class.
The StyleCellKind
enum is used to lookup the style to be applied.
Setup
General function used to run these examples.
import uno
from ooodev.format.calc.style import Cell as CellStyle, StyleCellKind
from ooodev.office.calc import Calc
from ooodev.gui import GUI
from ooodev.loader.lo import Lo
def main() -> int:
with Lo.Loader(connector=Lo.ConnectPipe()):
doc = Calc.create_doc()
GUI.set_visible(True, doc)
Lo.delay(500)
Calc.zoom_value(doc, 400)
sheet = Calc.get_active_sheet()
style = CellStyle(name=StyleCellKind.ACCENT_1)
cell_obj = Calc.get_cell_obj("A1")
Calc.set_val(value="Hello", sheet=sheet, cell_obj=cell_obj, styles=[style])
cell = Calc.get_cell(sheet=sheet, cell_obj=cell_obj)
style_obj = CellStyle.from_obj(cell)
assert style_obj.prop_name == str(StyleCellKind.ACCENT_1)
Lo.delay(1_000)
Lo.close_doc(doc)
return 0
if __name__ == "__main__":
SystemExit(main())
Apply Style to a Cell
Apply while setting Value
A cell style can be applied while setting the value of a cell.
# ... other code
cell_obj = Calc.get_cell_obj("A1")
style = CellStyle(name=StyleCellKind.ACCENT_1)
Calc.set_val(value="Hello", sheet=sheet, cell_obj=cell_obj, styles=[style])
Result be seen in Fig. 464.
Apply using Calc.set_style_cell()
The Calc.set_style_cell()
method can be used to apply one or more styles to a cell.
# ... other code
cell_obj = Calc.get_cell_obj("A1")
Calc.set_val(value="Hello", sheet=sheet, cell_obj=cell_obj)
Calc.set_style_cell(sheet=sheet, cell_obj=cell_obj, styles=[style])
Result be seen in Fig. 464.
Apply Style to a XCell
A cell style can be applied while to an existing cell object
by getting the XCell
object and applying the style.
# ... other code
cell_obj = Calc.get_cell_obj("A1")
style = CellStyle(name=StyleCellKind.ACCENT_1)
Calc.set_val(value="Hello", sheet=sheet, cell_obj=cell_obj)
cell = Calc.get_cell(sheet=sheet, cell_obj=cell_obj)
style.apply(cell)
Result be seen in Fig. 464.
Get Style from a Cell
Get Style from a Cell by first getting the XCell
object and then calling CellStyle.from_obj()
passing in the XCell
object.
# ... other code
cell = Calc.get_cell(sheet=sheet, cell_obj=cell_obj)
style_obj = CellStyle.from_obj(cell)
assert style_obj.prop_name == str(StyleCellKind.ACCENT_1)
Apply Style to a Range
Apply while setting Array
A cell style can be applied while setting an array of values.
In this example we will set the values of a range and apply a style.
# ... other code
style = CellStyle(name=StyleCellKind.ACCENT_1)
cell_rng = Calc.get_range_obj(range_name="A1:B1")
Calc.set_array(values=[[101, 22]], sheet=sheet, range_obj=cell_rng, styles=[style])
Result be seen in Fig. 465.
Apply using Calc.set_style_range()
The Calc.set_style_range()
method can be used to apply one or more styles to a range.
# ... other code
cell_rng = Calc.get_range_obj(range_name="A1:B1")
Calc.set_array(values=[[101, 22]], sheet=sheet, range_obj=cell_rng)
Calc.set_style_range(sheet=sheet, range_obj=cell_rng, styles=[style])
Result be seen in Fig. 465.
Apply Style to a XCellRange
In this example we will set the values of a range and apply a style to the range.
# ... other code
cell_rng = Calc.get_range_obj(range_name="A1:B1")
Calc.set_array(values=[[101, 22]], sheet=sheet, range_obj=cell_rng)
rng = Calc.get_cell_range(sheet=sheet, range_obj=cell_rng)
style.apply(rng)
Result be seen in Fig. 465.
Get Style from a Range
Get Style from a Cell by first getting the XCell
object and then calling CellStyle.from_obj()
passing in the XCell
object.
# ... other code
cell_rng = Calc.get_range_obj(range_name="A1:B1")
rng = Calc.get_cell_range(sheet=sheet, range_obj=cell_rng)
style_obj = CellStyle.from_obj(rng)
assert style_obj.prop_name == str(StyleCellKind.ACCENT_1)