Add cell styles
This commit is contained in:
parent
1a35cdfd8a
commit
f40f0bad97
|
@ -1,5 +1,5 @@
|
||||||
Cells and Range
|
Cells and Ranges
|
||||||
---------------
|
----------------
|
||||||
|
|
||||||
By selection
|
By selection
|
||||||
^^^^^^^^^^^^
|
^^^^^^^^^^^^
|
||||||
|
@ -10,3 +10,142 @@ By selection
|
||||||
selection = doc.selection
|
selection = doc.selection
|
||||||
app.debug(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
|
||||||
|
^^^^^^^
|
||||||
|
|
|
@ -2268,6 +2268,8 @@ class LOShape(object):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def properties(self):
|
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
|
data = self.obj.PropertySetInfo.Properties
|
||||||
keys = [p.Name for p in data]
|
keys = [p.Name for p in data]
|
||||||
values = self.obj.getPropertyValues(keys)
|
values = self.obj.getPropertyValues(keys)
|
||||||
|
@ -3339,6 +3341,62 @@ class LODocMain():
|
||||||
return self._type
|
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):
|
class LODocCalc(LODocument):
|
||||||
"""Classe for Calc module"""
|
"""Classe for Calc module"""
|
||||||
TYPE_RANGES = ('ScCellObj', 'ScCellRangeObj')
|
TYPE_RANGES = ('ScCellObj', 'ScCellRangeObj')
|
||||||
|
@ -3377,6 +3435,9 @@ class LODocCalc(LODocument):
|
||||||
self._i += 1
|
self._i += 1
|
||||||
return sheet
|
return sheet
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f'Calc: {self.title}'
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def selection(self):
|
def selection(self):
|
||||||
sel = self.obj.CurrentSelection
|
sel = self.obj.CurrentSelection
|
||||||
|
@ -3432,6 +3493,14 @@ class LODocCalc(LODocument):
|
||||||
def events(self):
|
def events(self):
|
||||||
return LOEvents(self.obj.Events)
|
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):
|
def activate(self, sheet: Any):
|
||||||
"""Activate sheet
|
"""Activate sheet
|
||||||
|
|
||||||
|
@ -3853,20 +3922,20 @@ class LOCalcRange(object):
|
||||||
def range_address(self):
|
def range_address(self):
|
||||||
return self.obj.RangeAddress
|
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
|
@property
|
||||||
def sheet(self):
|
def sheet(self):
|
||||||
return LOCalcSheet(self.obj.Spreadsheet)
|
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
|
@property
|
||||||
def style(self):
|
def style(self):
|
||||||
return self.obj.CellStyle
|
return self.obj.CellStyle
|
||||||
|
@ -3875,9 +3944,14 @@ class LOCalcRange(object):
|
||||||
self.obj.CellStyle = value
|
self.obj.CellStyle = value
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def cursor(self):
|
def data(self):
|
||||||
cursor = self.obj.Spreadsheet.createCursorByRange(self.obj)
|
return self.obj.getDataArray()
|
||||||
return cursor
|
@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):
|
def to_size(self, cols: int, rows: int):
|
||||||
cursor = self.cursor
|
cursor = self.cursor
|
||||||
|
@ -3886,13 +3960,51 @@ class LOCalcRange(object):
|
||||||
return LOCalcRange(rango)
|
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):
|
class LODocWriter(LODocument):
|
||||||
_type = 'writer'
|
_type = 'writer'
|
||||||
|
TEXT_RANGES = 'SwXTextRanges'
|
||||||
|
|
||||||
def __init__(self, obj):
|
def __init__(self, obj):
|
||||||
super().__init__(obj)
|
super().__init__(obj)
|
||||||
self._view_settings = self._cc.ViewSettings
|
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
|
@property
|
||||||
def zoom(self):
|
def zoom(self):
|
||||||
return self._view_settings.ZoomValue
|
return self._view_settings.ZoomValue
|
||||||
|
|
Loading…
Reference in New Issue