diff --git a/docs/source/calc_doc.rst b/docs/source/calc_doc.rst index 208178d..eb46a0d 100644 --- a/docs/source/calc_doc.rst +++ b/docs/source/calc_doc.rst @@ -98,3 +98,25 @@ Select range by user doc = app.active doc.start_range_selection(Controllers) return + +| + +Selection +--------- + +.. code-block:: python + + doc = app.active + selection = doc.selection + app.debug(selection.is_cell) + +| + +Select +------ + +.. code-block:: python + + doc = app.active + cell = doc[0]['A1'] + doc.select(cell) diff --git a/docs/source/calc_sheets.rst b/docs/source/calc_sheets.rst index 39177cf..4e06ac3 100644 --- a/docs/source/calc_sheets.rst +++ b/docs/source/calc_sheets.rst @@ -64,6 +64,17 @@ Count | +Iter +---- + +.. code-block:: python + + doc = app.active + for sheet in doc: + app.debug(sheet) + +| + New ^^^ @@ -380,3 +391,48 @@ Activate # Activate by name doc.activate('Sheet3') + +| + +Events +^^^^^^ + +* See all the events that can be used. + +.. code-block:: python + + sheet = doc.active + event_names = sheet.events.names + app.debug(event_names) + +* Assing some macro to event. + +.. code-block:: python + + def on_select(source): + app.debug(source.AbsoluteName) + return + + def main(): + doc = app.active + sheet = doc.active + + events = sheet.events + if 'OnSelect' in events: + macro = {'library': 'test', 'name': 'on_select'} + events['OnSelect'] = macro + + return + +* Remove + +.. code-block:: python + + events['OnSelect'] = {} + +* Or + +.. code-block:: python + + events.remove('OnSelect') + diff --git a/source/easymacro.py b/source/easymacro.py index 7e5867a..1b87cec 100644 --- a/source/easymacro.py +++ b/source/easymacro.py @@ -3188,6 +3188,9 @@ class LODocMain(): class LODocCalc(LODocument): """Classe for Calc module""" + TYPE_RANGES = ('ScCellObj', 'ScCellRangeObj') + RANGES = 'ScCellRangesObj' + SHAPE = 'com.sun.star.drawing.SvxShapeCollection' _type = 'calc' def __init__(self, obj): @@ -3209,6 +3212,32 @@ class LODocCalc(LODocument): def __contains__(self, item): return item in self._sheets + def __iter__(self): + self._i = 0 + return self + + def __next__(self): + try: + sheet = LOCalcSheet(self._sheets[self._i]) + except Exception as e: + raise StopIteration + self._i += 1 + return sheet + + @property + def selection(self): + sel = self.obj.CurrentSelection + type_obj = sel.ImplementationName + if type_obj in self.TYPE_RANGES: + sel = LOCalcRange(sel) + elif type_obj == self.RANGES: + sel = LOCalcRanges(sel) + elif type_obj == self.SHAPE: + print(len(sel)) + else: + debug(type_obj) + return sel + @property def headers(self): """Get true if is visible columns/rows headers""" @@ -3388,6 +3417,23 @@ class LODocCalc(LODocument): self._cc.removeRangeSelectionListener(self._listener_range_selection) return + def select(self, rango: Any): + obj = rango + if hasattr(rango, 'obj'): + obj = rango.obj + self._cc.select(obj) + return + + @property + def ranges(self): + obj = self.create_instance('com.sun.star.sheet.SheetCellRanges') + return LOCalcRanges(obj) + + def get_ranges(self, address: str): + ranges = self.ranges + ranges.add([sheet[address] for sheet in self]) + return ranges + class LOCalcSheet(object): @@ -3404,7 +3450,7 @@ class LOCalcSheet(object): pass def __str__(self): - return f'easymacro.LOCalcSheet: {self.name}' + return f'Sheet: {self.name}' @property def obj(self): @@ -3442,6 +3488,16 @@ class LOCalcSheet(object): def color(self, value): self._obj.TabColor = Color()(value) + @property + def events(self): + return LOEvents(self.obj.Events) + + @property + def used_area(self): + cursor = self.create_cursor() + cursor.gotoEndOfUsedArea(True) + return self[cursor.AbsoluteName] + @property def is_protected(self): return self._obj.isProtected() @@ -3492,6 +3548,38 @@ class LOCalcSheet(object): self.doc.activate(self.obj) return + def create_cursor(self, rango: Any=None): + if rango is None: + cursor = self.obj.createCursor() + else: + obj = rango + if hasattr(rango, 'obj'): + obj = rango.obj + cursor = self.obj.createCursorByRange(obj) + return cursor + + +class LOCalcRanges(object): + + def __init__(self, obj): + self._obj = obj + + @property + def obj(self): + return self._obj + + @property + def names(self): + return self.obj.ElementNames + + def add(self, rangos): + if isinstance(rangos, LOCalcRange): + rangos = (rangos,) + for r in rangos: + # ~ self._ranges[r.name] = r + self.obj.addRangeAddress(r.range_address, False) + return + class LOCalcRange(object): CELL = 'ScCellObj' @@ -3500,10 +3588,57 @@ class LOCalcRange(object): self._obj = obj self._is_cell = obj.ImplementationName == self.CELL + def __getitem__(self, index): + return LOCalcRange(self.obj[index]) + + def __iter__(self): + self._r = 0 + self._c = 0 + return self + + def __next__(self): + try: + rango = self[self._r, self._c] + except Exception as e: + raise StopIteration + self._c += 1 + if self._c == self.obj.Columns.Count: + self._c = 0 + self._r +=1 + return rango + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_value, traceback): + pass + + def __str__(self): + s = f'Range: {self.name}' + if self.is_cell: + s = f'Cell: {self.name}' + return s + @property def obj(self): return self._obj + @property + def is_cell(self): + return self._is_cell + + @property + def name(self): + return self.obj.AbsoluteName + + @property + def address(self): + return self.obj.CellAddress + + @property + def range_address(self): + return self.obj.RangeAddress + @property def data(self): return self.obj.getDataArray()