From f40f0bad97200eb82cb39bb5130e92b5250e8f49 Mon Sep 17 00:00:00 2001 From: El Mau Date: Sat, 9 Apr 2022 22:23:13 -0500 Subject: [PATCH] Add cell styles --- docs/source/calc_cells.rst | 143 ++++++++++++++++++++++++++++++++++++- source/easymacro.py | 138 +++++++++++++++++++++++++++++++---- 2 files changed, 266 insertions(+), 15 deletions(-) diff --git a/docs/source/calc_cells.rst b/docs/source/calc_cells.rst index 9b58b0a..cc55e7e 100644 --- a/docs/source/calc_cells.rst +++ b/docs/source/calc_cells.rst @@ -1,5 +1,5 @@ -Cells and Range ---------------- +Cells and Ranges +---------------- By selection ^^^^^^^^^^^^ @@ -10,3 +10,142 @@ By selection selection = doc.selection app.debug(selection) + +By name +^^^^^^^ + +.. code-block:: python + + doc = app.active + sheet = doc.active + cell = sheet['A1'] + rango = sheet['C10:D15'] + + app.debug(cell) + app.debug(rango) + + +By position +^^^^^^^^^^^ + +For cells: `sheet[row,column]` + +For ranges: `sheet[start_row:end_row, start_column:end_column]` + +.. code-block:: python + + sheet = app.active.active + + 'Cell A10 + cell = sheet[9,0] + + 'Range A1:C10 + rango = sheet[0:10,0:3] + + +Iteration cells +^^^^^^^^^^^^^^^ + +.. code-block:: python + + rango = sheet['B10:C15'] + for cell in rango: + app.debug(cell) + + +Properties +^^^^^^^^^^ + +is_cell +~~~~~~~ + +.. code-block:: python + + cell = sheet['A1'] + app.debug(cell.is_cell) + + rango = sheet['A1:C5'] + app.debug(rango.is_cell) + +name +~~~~ + +Return `AbsoluteName` + +.. code-block:: python + + cell = sheet['A1'] + app.debug(cell.name) + + rango = sheet['A1:C5'] + app.debug(rango.name) + +address +~~~~~~~ + +Return struct `com.sun.star.table.CellAddress` + +.. code-block:: python + + cell = sheet['A1'] + if cell.is_cell: + app.debug(cell.address) + +range_address +~~~~~~~~~~~~~ + +Return struct `com.sun.star.table.CellRangeAddress` + +.. code-block:: python + + rango = sheet['A1:C5'] + if not rango.is_cell: + app.debug(rango.range_address) + +sheet +~~~~~ + +Get parent sheet, return LOCalcSheet class + +.. code-block:: python + + rango = sheet['A1:C5'] + sheet = rango.sheet + app.debug(sheet) + +doc +~~~ + +Get parent document, return LODocCalc class + +.. code-block:: python + + rango = sheet['A1:C5'] + doc = rango.doc + app.debug(doc) + +cursor +~~~~~~ + +Get cursor by self range, return ScCellCursorObj instance. + +.. code-block:: python + + rango = sheet['A1:C5'] + cursor = rango.cursor + app.debug(cursor.ImplementationName) + +style +~~~~~ + +Get or set cell style. Style must exists. + +.. code-block:: python + + rango = sheet['A1:C5'] + app.debug(rango.style) + rango.style = 'Good' + + +Methods +^^^^^^^ diff --git a/source/easymacro.py b/source/easymacro.py index 843cc0d..fc42b9c 100644 --- a/source/easymacro.py +++ b/source/easymacro.py @@ -2268,6 +2268,8 @@ class LOShape(object): @property def properties(self): + # ~ properties = self.obj.PropertySetInfo.Properties + # ~ data = {p.Name: getattr(self.obj, p.Name) for p in properties} data = self.obj.PropertySetInfo.Properties keys = [p.Name for p in data] values = self.obj.getPropertyValues(keys) @@ -3339,6 +3341,62 @@ class LODocMain(): return self._type +class LOCellStyle(LOBaseObject): + + def __init__(self, obj): + super().__init__(obj) + + @property + def name(self): + return self.obj.Name + + @property + def properties(self): + properties = self.obj.PropertySetInfo.Properties + data = {p.Name: getattr(self.obj, p.Name) for p in properties} + return data + @properties.setter + def properties(self, values): + _set_properties(self.obj, values) + + +class LOCellStyles(object): + + def __init__(self, obj, doc): + self._obj = obj + self._doc = doc + + def __len__(self): + return len(self.obj) + + def __getitem__(self, index): + return LOCellStyle(self.obj[index]) + + def __setitem__(self, key, value): + self.obj[key] = value + + def __delitem__(self, key): + if not isinstance(key, str): + key = key.Name + del self.obj[key] + + def __contains__(self, item): + return item in self.obj + + @property + def obj(self): + return self._obj + + @property + def names(self): + return self.obj.ElementNames + + def new(self, name: str): + obj = self._doc.create_instance('com.sun.star.style.CellStyle') + self.obj[name] = obj + return LOCellStyle(obj) + + class LODocCalc(LODocument): """Classe for Calc module""" TYPE_RANGES = ('ScCellObj', 'ScCellRangeObj') @@ -3377,6 +3435,9 @@ class LODocCalc(LODocument): self._i += 1 return sheet + def __str__(self): + return f'Calc: {self.title}' + @property def selection(self): sel = self.obj.CurrentSelection @@ -3432,6 +3493,14 @@ class LODocCalc(LODocument): def events(self): return LOEvents(self.obj.Events) + @property + def cs(self): + return self.cell_styles + @property + def cell_styles(self): + obj = self.obj.StyleFamilies['CellStyles'] + return LOCellStyles(obj, self) + def activate(self, sheet: Any): """Activate sheet @@ -3853,20 +3922,20 @@ class LOCalcRange(object): def range_address(self): return self.obj.RangeAddress - @property - def data(self): - return self.obj.getDataArray() - @data.setter - def data(self, values): - if self._is_cell: - self.to_size(len(values[0]), len(values)).data = values - else: - self.obj.setDataArray(values) - @property def sheet(self): return LOCalcSheet(self.obj.Spreadsheet) + @property + def doc(self): + doc = self.obj.Spreadsheet.DrawPage.Forms.Parent + return LODocCalc(doc) + + @property + def cursor(self): + cursor = self.obj.Spreadsheet.createCursorByRange(self.obj) + return cursor + @property def style(self): return self.obj.CellStyle @@ -3875,9 +3944,14 @@ class LOCalcRange(object): self.obj.CellStyle = value @property - def cursor(self): - cursor = self.obj.Spreadsheet.createCursorByRange(self.obj) - return cursor + def data(self): + return self.obj.getDataArray() + @data.setter + def data(self, values): + if self._is_cell: + self.to_size(len(values[0]), len(values)).data = values + else: + self.obj.setDataArray(values) def to_size(self, cols: int, rows: int): cursor = self.cursor @@ -3886,13 +3960,51 @@ class LOCalcRange(object): return LOCalcRange(rango) +class LOWriterTextRange(object): + + def __init__(self, obj, doc): + self._obj = obj + self._doc = doc + + @property + def obj(self): + return self._obj + + @property + def text(self): + return self.obj.Text + + @property + def cursor(self): + return self.text.createTextCursorByRange(self.obj) + + def insert_comment(self, content: str, author: str='', dt: Any=None): + # ~ range.Text.insertTextContent(cursor, comment, False) + comment = self._doc._create_instance('com.sun.star.text.textfield.Annotation') + comment.Content = content + comment.Author = author + comment.attach(self.cursor.End) + return + + class LODocWriter(LODocument): _type = 'writer' + TEXT_RANGES = 'SwXTextRanges' def __init__(self, obj): super().__init__(obj) self._view_settings = self._cc.ViewSettings + @property + def selection(self): + sel = self.obj.CurrentSelection + type_obj = sel.ImplementationName + if type_obj == self.TEXT_RANGES: + if len(sel) == 1: + sel = LOWriterTextRange(sel[0], self) + + return sel + @property def zoom(self): return self._view_settings.ZoomValue