Calc Direct Cell Protection (Static)

Overview

The Calc class has several methods related to cell protection and sheet protection.

The ooodev.format.calc.direct.cell.cell_protection.CellProtection class is used to set the cell protection properties.

The CellProtection class gives you the same options as Calc’s Cell Protection Dialog tab, but without the dialog. as seen in Fig. 397.

Warning

Note that cell protection is not the same as sheet protection and cell protection is only enabled with sheet protection is enabled. See LibreOffice Help - Cell Protection for more information.

Calc Format Cell dialog Cell Protection

Fig. 397 Calc Format Cell dialog Cell Protection

Apply cell protection to a cell

Setup

import uno
from ooodev.office.calc import Calc
from ooodev.gui import GUI
from ooodev.loader.lo import Lo
from ooodev.format.calc.direct.cell.cell_protection import CellProtection

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 = CellProtection(hide_all=False, hide_formula=True, protected=True, hide_print=True)
        Calc.set_val(value="Hello", cell=cell, styles=[style])

        f_style = CellProtection.from_obj(cell)
        assert f_style is not None

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

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

Setting the cell protection

style = CellProtection(hide_all=False, hide_formula=True, protected=True, hide_print=True)
Calc.set_val(value="Hello", cell=cell, styles=[style])

Running the above code will produce the following output in Fig. 398.

Calc Format Cell dialog Cell Protection set

Fig. 398 Calc Format Cell dialog Cell Protection set

Getting cell protection from a cell

# ... other code

f_style = CellProtection.from_obj(cell)
assert f_style is not None

Apply cell protection to a range

Setup

import uno
from ooodev.office.calc import Calc
from ooodev.gui import GUI
from ooodev.loader.lo import Lo
from ooodev.format.calc.direct.cell.cell_protection import CellProtection

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 = CellProtection(hide_all=False, hide_formula=True, protected=True, hide_print=True)
        style.apply(rng)

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


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

Setting the range protection

style = CellProtection(hide_all=False, hide_formula=True, protected=True, hide_print=True)
style.apply(rng)

Running the above code will produce the following output in Fig. 398.

Getting cell protection from a range

It is not recommended to get and instance of CellProtection from a range. This is because a range can have multiple cells with different cell protection settings and the CellProtection may not properly represent the range.