Calc Modify Cell Protection

Overview

The ooodev.format.calc.modify.cell.cell_protection.CellProtection class is used to programmatically set styles protection properties as seen dialog shown in Fig. 419.

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 Style Protection

Fig. 419 Calc Format Cell dialog Cell Style Protection

Setup

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


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

        style = CellProtection(
            hide_all=False,
            hide_formula=True,
            protected=True,
            hide_print=True,
            style_name=StyleCellKind.DEFAULT,
        )
        style.apply(doc)

        style_obj = CellProtection.from_style(doc=doc, style_name=StyleCellKind.DEFAULT)
        assert style_obj.prop_style_name == str(StyleCellKind.DEFAULT)

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


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

Setting the style protection

style = CellProtection(
    hide_all=False,
    hide_formula=True,
    protected=True,
    hide_print=True,
    style_name=StyleCellKind.DEFAULT,
)
style.apply(doc)

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

Calc Format Cell dialog Cell Style Protection set

Fig. 420 Calc Format Cell dialog Cell Style Protection set

Getting cell protection from a style

# ... other code

style_obj = CellProtection.from_style(doc=doc, style_name=StyleCellKind.DEFAULT)
assert style_obj.prop_style_name == str(StyleCellKind.DEFAULT)