592 lines
8.3 KiB
ReStructuredText
592 lines
8.3 KiB
ReStructuredText
|
|
Calc
|
|
----
|
|
|
|
Remember, always import library.
|
|
|
|
.. code-block:: python
|
|
|
|
import easymacro as app
|
|
|
|
|
|
Document
|
|
~~~~~~~~
|
|
|
|
Current doc
|
|
^^^^^^^^^^^
|
|
|
|
.. code-block:: python
|
|
|
|
doc = app.active
|
|
app.msgbox(doc.type)
|
|
|
|
|
|
Selection
|
|
^^^^^^^^^
|
|
|
|
* If selection is range get LOCalcRange, if selection is shape get LOShape, other selection get original pyuno object.
|
|
|
|
.. code-block:: python
|
|
|
|
doc = app.active
|
|
selection = doc.selection
|
|
app.msgbox(type(selection))
|
|
|
|
|
|
Headers
|
|
^^^^^^^
|
|
|
|
* Hide or show columns and rows headers.
|
|
|
|
.. code-block:: python
|
|
|
|
doc = app.active
|
|
app.msgbox(doc.headers)
|
|
doc.headers = not doc.headers
|
|
app.msgbox(doc.headers)
|
|
doc.headers = not doc.headers
|
|
|
|
|
|
Tabs
|
|
^^^^
|
|
|
|
* Hide or show tab sheets.
|
|
|
|
.. code-block:: python
|
|
|
|
doc = app.active
|
|
app.msgbox(doc.tabs)
|
|
doc.tabs = not doc.tabs
|
|
app.msgbox(doc.tabs)
|
|
doc.tabs = not doc.tabs
|
|
|
|
|
|
Sheets
|
|
~~~~~~
|
|
|
|
Active sheet
|
|
^^^^^^^^^^^^
|
|
|
|
.. code-block:: python
|
|
|
|
sheet = app.active_sheet
|
|
app.msgbox(sheet.name)
|
|
|
|
# or
|
|
doc = app.active
|
|
sheet = doc.active
|
|
|
|
|
|
Get by index
|
|
^^^^^^^^^^^^
|
|
|
|
.. code-block:: python
|
|
|
|
doc = app.active
|
|
sheet = doc[0]
|
|
app.msgbox(sheet.name)
|
|
|
|
|
|
Get by name
|
|
^^^^^^^^^^^
|
|
|
|
.. code-block:: python
|
|
|
|
doc = app.active
|
|
sheet = doc['Sheet1']
|
|
app.msgbox(sheet.name)
|
|
|
|
|
|
Contains
|
|
^^^^^^^^
|
|
|
|
.. code-block:: python
|
|
|
|
doc = app.active
|
|
app.msgbox('Sheet1' in doc)
|
|
|
|
|
|
Get tuple with all names
|
|
^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
.. code-block:: python
|
|
|
|
doc = app.active
|
|
app.msgbox(doc.names)
|
|
|
|
|
|
Count
|
|
^^^^^
|
|
|
|
.. code-block:: python
|
|
|
|
doc = app.active
|
|
app.msgbox(len(doc))
|
|
|
|
New
|
|
^^^
|
|
|
|
* Always validate if new name not exists.
|
|
|
|
.. code-block:: python
|
|
|
|
doc = app.active
|
|
|
|
sheet = doc.new_sheet()
|
|
# CAUTION: If 'NewSheet' exists, reset it to clean sheet.
|
|
doc['NewSheet'] = sheet
|
|
|
|
# ~ or
|
|
|
|
sheet = doc.insert('NewSheet2')
|
|
|
|
* Insert multiple, get last insert.
|
|
|
|
.. code-block:: python
|
|
|
|
names = ('One', 'Two', 'Three')
|
|
sheet = doc.insert(names)
|
|
|
|
|
|
Move
|
|
^^^^
|
|
|
|
* Move by object to last position.
|
|
|
|
.. code-block:: python
|
|
|
|
sheet = doc[0]
|
|
doc.move(sheet)
|
|
|
|
* Move by name to last position.
|
|
|
|
.. code-block:: python
|
|
|
|
doc.move('Sheet1')
|
|
|
|
* Move to position.
|
|
|
|
.. code-block:: python
|
|
|
|
sheet = doc[0]
|
|
doc.move(sheet, 2)
|
|
|
|
* Move from sheet
|
|
|
|
.. code-block:: python
|
|
|
|
sheet = app.active_sheet
|
|
sheet.move()
|
|
|
|
* Move to position.
|
|
|
|
.. code-block:: python
|
|
|
|
sheet = app.active_sheet
|
|
sheet.move(2)
|
|
|
|
|
|
Remove
|
|
^^^^^^
|
|
|
|
* Remove by object.
|
|
|
|
.. code-block:: python
|
|
|
|
sheet = doc[0]
|
|
doc.remove(sheet)
|
|
|
|
* Remove by name.
|
|
|
|
.. code-block:: python
|
|
|
|
doc.remove('One')
|
|
|
|
* Remove from sheet.
|
|
|
|
.. code-block:: python
|
|
|
|
sheet = app.active_sheet
|
|
sheet.remove()
|
|
|
|
|
|
Copy
|
|
^^^^
|
|
|
|
* Copy inside the same spreadsheet.
|
|
|
|
* By object
|
|
|
|
.. code-block:: python
|
|
|
|
sheet = doc[0]
|
|
doc.copy(sheet, 'OtherSheet')
|
|
|
|
* By name
|
|
|
|
.. code-block:: python
|
|
|
|
doc.copy('Sheet1', 'Sheet2')
|
|
|
|
* From sheet
|
|
|
|
.. code-block:: python
|
|
|
|
sheet = app.active_sheet
|
|
sheet.copy(sheet.name + '_2')
|
|
|
|
|
|
Copy from
|
|
^^^^^^^^^
|
|
|
|
* Copy sheet from one spreadsheet to other.
|
|
|
|
.. code-block:: python
|
|
|
|
doc = app.active
|
|
doc_source = app.docs['Contacts.ods']
|
|
name_source = 'Names'
|
|
name_target = 'Names'
|
|
position = 0
|
|
doc.copy_from(doc_source, name_source, name_target, position)
|
|
|
|
|
|
Copy to
|
|
^^^^^^^
|
|
|
|
* Copy from sheet with the same name
|
|
|
|
.. code-block:: python
|
|
|
|
doc = app.docs.new()
|
|
sheet = app.active_sheet
|
|
sheet.copy_to(doc)
|
|
|
|
* Used new name
|
|
|
|
.. code-block:: python
|
|
|
|
doc = app.docs.new()
|
|
sheet = app.active_sheet
|
|
sheet.copy_to(doc, 'NewName')
|
|
|
|
|
|
Sort
|
|
^^^^
|
|
|
|
* Sort sheets by names.
|
|
|
|
.. code-block:: python
|
|
|
|
doc = app.active
|
|
doc.sort()
|
|
|
|
Name
|
|
^^^^
|
|
|
|
* Name visible by the user.
|
|
|
|
.. code-block:: python
|
|
|
|
sheet = app.active_sheet
|
|
app.msgbox(sheet.name)
|
|
sheet.name = 'NewName'
|
|
app.msgbox(sheet.name)
|
|
|
|
|
|
Code name
|
|
^^^^^^^^^
|
|
|
|
* Name only accessible by code.
|
|
|
|
.. code-block:: python
|
|
|
|
sheet = app.active_sheet
|
|
app.msgbox(sheet.code_name)
|
|
sheet.code_name = 'my_name'
|
|
app.msgbox(sheet.code_name)
|
|
|
|
|
|
Visible
|
|
^^^^^^^
|
|
|
|
* Apply only with spreadsheet with two or more sheets.
|
|
|
|
.. code-block:: python
|
|
|
|
sheet = app.active_sheet
|
|
app.msgbox(sheet.visible)
|
|
sheet.visible = not sheet.visible
|
|
app.msgbox(sheet.visible)
|
|
sheet.visible = not sheet.visible
|
|
|
|
|
|
Is protected
|
|
^^^^^^^^^^^^
|
|
|
|
* If sheet is protected with password.
|
|
|
|
.. code-block:: python
|
|
|
|
sheet = app.active_sheet
|
|
app.msgbox(sheet.is_protected)
|
|
|
|
|
|
Set password
|
|
^^^^^^^^^^^^
|
|
|
|
.. code-block:: python
|
|
|
|
sheet = app.active_sheet
|
|
sheet.password = 'letmein'
|
|
app.msgbox(sheet.is_protected)
|
|
|
|
|
|
Remove password
|
|
^^^^^^^^^^^^^^^
|
|
|
|
.. code-block:: python
|
|
|
|
sheet = app.active_sheet
|
|
sheet.password = 'letmein'
|
|
app.msgbox(sheet.is_protected)
|
|
|
|
sheet.unprotect('letmein')
|
|
app.msgbox(sheet.is_protected)
|
|
|
|
|
|
Tab color
|
|
^^^^^^^^^
|
|
|
|
.. code-block:: python
|
|
|
|
sheet = app.active_sheet
|
|
app.msgbox(sheet.color)
|
|
|
|
sheet.color = 'red'
|
|
app.msgbox(sheet.color)
|
|
|
|
# RGB
|
|
sheet.color = (125, 200, 10)
|
|
app.msgbox(sheet.color)
|
|
|
|
|
|
Document parent
|
|
^^^^^^^^^^^^^^^
|
|
|
|
.. code-block:: python
|
|
|
|
doc = sheet.doc
|
|
|
|
|
|
Activate
|
|
^^^^^^^^
|
|
|
|
.. code-block:: python
|
|
|
|
doc = app.active
|
|
# Get last sheet
|
|
sheet = doc[-1]
|
|
|
|
# Activate from doc
|
|
doc.activate(sheet)
|
|
|
|
# Activate from sheet
|
|
sheet.activate()
|
|
|
|
|
|
Ranges
|
|
~~~~~~
|
|
|
|
Cells
|
|
^^^^^
|
|
|
|
* By name
|
|
|
|
.. code-block:: python
|
|
|
|
sheet = app.active_sheet
|
|
cell = sheet['A1']
|
|
app.msgbox(cell.name)
|
|
|
|
|
|
* By position [row, column]
|
|
|
|
.. code-block:: python
|
|
|
|
cell = sheet[1, 4]
|
|
app.msgbox(cell.name)
|
|
|
|
|
|
Ranges
|
|
^^^^^^
|
|
|
|
* By name
|
|
|
|
.. code-block:: python
|
|
|
|
sheet = app.active_sheet
|
|
rango = sheet['B2:D5']
|
|
app.msgbox(rango.name)
|
|
|
|
* By position [start_row:end_row, start_column:end_column]
|
|
|
|
.. code-block:: python
|
|
|
|
rango = sheet[1:5,1:4]
|
|
app.msgbox(rango.name)
|
|
|
|
* Group ranges
|
|
|
|
.. code-block:: python
|
|
|
|
doc = app.active
|
|
sheet = doc.active
|
|
last = doc[-1]
|
|
with doc.ranges as r:
|
|
# Add one range
|
|
r.add(sheet['A1:B2'])
|
|
r2 = sheet['C4:D5']
|
|
r3 = last['E7:D8']
|
|
r4 = last['E10:F12']
|
|
# Add multiple ranges
|
|
r.add((r2, r3, r4))
|
|
app.debug(r.names)
|
|
|
|
* Get the same range in all sheets
|
|
|
|
.. code-block:: python
|
|
|
|
ranges = doc.get_ranges('A1:B1')
|
|
app.debug(ranges.names)
|
|
|
|
* Get columns by name.
|
|
|
|
.. code-block:: python
|
|
|
|
sheet = app.active_sheet
|
|
rango = sheet['B:B']
|
|
app.debug(rango.name)
|
|
|
|
rango = sheet['D:F']
|
|
app.debug(rango.name)
|
|
|
|
* Get columns by position.
|
|
|
|
.. code-block:: python
|
|
|
|
sheet = app.active_sheet
|
|
# Column B
|
|
rango = sheet[0:,1]
|
|
app.debug(rango.name)
|
|
|
|
# Columnas D:F
|
|
rango = sheet[0:,3:6]
|
|
app.debug(rango.name)
|
|
|
|
|
|
* Get rows
|
|
|
|
.. code-block:: python
|
|
|
|
sheet = app.active_sheet
|
|
# One row
|
|
row = sheet[1]
|
|
app.debug(row.name)
|
|
|
|
# Range rows
|
|
row = sheet[3:10,0:]
|
|
app.debug(row.name)
|
|
|
|
|
|
Info ranges
|
|
^^^^^^^^^^^
|
|
|
|
* Get absolute name
|
|
|
|
.. code-block:: python
|
|
|
|
sheet = app.active_sheet
|
|
rango = sheet['A1:E10']
|
|
app.msgbox(rango.name)
|
|
|
|
* Get address
|
|
|
|
.. code-block:: python
|
|
|
|
sheet = app.active_sheet
|
|
rango = sheet['A1']
|
|
a = rango.address
|
|
data = f"""Cell Address
|
|
Row: {a.Row}
|
|
Column: {a.Column}
|
|
"""
|
|
app.msgbox(data)
|
|
|
|
rango = sheet['A1:E10']
|
|
ra = rango.range_address
|
|
data = (
|
|
f'Range Address:\n\n'
|
|
f'Star Row: {ra.StartRow}\n'
|
|
f'End Row: {ra.EndRow}\n'
|
|
f'Star Column: {ra.StartColumn}\n'
|
|
f'End Column: {ra.EndColumn}\n'
|
|
)
|
|
app.msgbox(data)
|
|
|
|
|
|
Special ranges
|
|
^^^^^^^^^^^^^^
|
|
|
|
* Get used area
|
|
|
|
.. code-block:: python
|
|
|
|
sheet = app.active_sheet
|
|
rango = sheet.used_area
|
|
app.msgbox(rango.name)
|
|
|
|
* Get current region
|
|
|
|
.. code-block:: python
|
|
|
|
rango = sheet['A1'].current_region
|
|
app.msgbox(rango.name)
|
|
|
|
* Get next free cell
|
|
|
|
.. code-block:: python
|
|
|
|
cell = sheet['A1'].next_cell
|
|
app.msgbox(cell.name)
|
|
|
|
* Get merged area
|
|
|
|
.. code-block:: python
|
|
|
|
sheet = app.active_sheet
|
|
rango = sheet['A1'].merged_area
|
|
app.msgbox(rango.name)
|
|
|
|
* Get visible cells
|
|
|
|
.. code-block:: python
|
|
|
|
sheet = app.active_sheet
|
|
rangos = sheet['A1:E10'].visible
|
|
for r in rangos:
|
|
app.debug(r.name)
|
|
|
|
* Get empty cells
|
|
|
|
.. code-block:: python
|
|
|
|
sheet = app.active_sheet
|
|
rangos = sheet['A1:E10'].empty
|
|
for r in rangos:
|
|
app.debug(r.name)
|
|
|
|
|
|
Manipulate ranges
|
|
~~~~~~~~~~~~~~~~~
|