Calc Direct Cell Background (Static)

Color

The ooodev.format.calc.direct.cell.background.Color class is used to set the background color of a cell.

Apply the background color to a cell

Setup

import uno
from ooodev.office.calc import Calc
from ooodev.gui.gui import GUI
from ooodev.loader.lo import Lo
from ooodev.format.calc.direct.cell.background import Color as BgColor
from ooodev.utils.color import StandardColor

def main() -> int:
    with Lo.Loader(connector=Lo.ConnectSocket()):
        doc = Calc.create_doc()
        sheet = Calc.get_sheet()
        GUI.set_visible(True, doc)
        Lo.delay(500)
        Calc.zoom_value(doc, 400)

        cell = Calc.get_cell(sheet=sheet, cell_name="A1")
        style = BgColor(StandardColor.BLUE_LIGHT2)
        Calc.set_val(value="Hello", cell=cell, styles=[style])

        f_style = BgColor.from_obj(cell)
        assert f_style.prop_color == StandardColor.BLUE_LIGHT2

        Lo.delay(1_000)
        Lo.close_doc(doc)
    return 0


if __name__ == "__main__":
    SystemExit(main())

Setting the color

style = BgColor(StandardColor.BLUE_LIGHT2)
Calc.set_val(value="Hello", cell=cell, styles=[style])

Running the above code will produce the following output in Fig. 370 and Fig. 371.

Calc Cell Background Color set

Fig. 370 Calc Cell Background Color set

Calc Format Cell dialog Background Color set

Fig. 371 Calc Format Cell dialog Background Color set

Getting the color from a cell

# ... other code

f_style = BgColor.from_obj(cell)
assert f_style.prop_color == StandardColor.BLUE_LIGHT2

Apply the background color to a range

Setup

import uno
from ooodev.office.calc import Calc
from ooodev.gui.gui import GUI
from ooodev.loader.lo import Lo
from ooodev.format.calc.direct.cell.background import Color as BgColor
from ooodev.utils.color import StandardColor


def main() -> int:
    with Lo.Loader(connector=Lo.ConnectSocket()):
        doc = Calc.create_doc()
        sheet = Calc.get_sheet()
        GUI.set_visible(True, doc)
        Lo.delay(500)
        Calc.zoom_value(doc, 400)

        Calc.set_val(value="Hello", sheet=sheet, cell_name="A1")
        Calc.set_val(value="World", sheet=sheet, cell_name="B1")
        rng = Calc.get_cell_range(sheet=sheet, range_name="A1:B1")

        style = BgColor(StandardColor.BLUE_LIGHT2)
        style.apply(rng)

        f_style = BgColor.from_obj(rng)
        assert f_style.prop_color == StandardColor.BLUE_LIGHT2

        Lo.delay(1_000)
        Lo.close_doc(doc)
    return 0


if __name__ == "__main__":
    SystemExit(main())

Setting the color

style = BgColor(StandardColor.BLUE_LIGHT2)
style.apply(rng)

Running the above code will produce the following output in Fig. 371 and Fig. 372.

Calc Cell Background Color set

Fig. 372 Calc Cell Background Color set

Getting the color from a range

# ... other code

f_style = BgColor.from_obj(rng)
assert f_style.prop_color == StandardColor.BLUE_LIGHT2