Calc

Remember, always import library.

import easymacro as app

Document

Current doc

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.

doc = app.active
selection = doc.selection
app.msgbox(type(selection))

Headers

  • Hide or show columns and rows headers.

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.

doc = app.active
app.msgbox(doc.tabs)
doc.tabs = not doc.tabs
app.msgbox(doc.tabs)
doc.tabs = not doc.tabs

Sheets

Active sheet

sheet = app.active_sheet
app.msgbox(sheet.name)

# or
doc = app.active
sheet = doc.active

Get by index

doc = app.active
sheet = doc[0]
app.msgbox(sheet.name)

Get by name

doc = app.active
sheet = doc['Sheet1']
app.msgbox(sheet.name)

Contains

doc = app.active
app.msgbox('Sheet1' in doc)

Get tuple with all names

doc = app.active
app.msgbox(doc.names)

Count

doc = app.active
app.msgbox(len(doc))

New

  • Always validate if new name not exists.

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.

names = ('One', 'Two', 'Three')
sheet = doc.insert(names)

Move

  • Move by object to last position.

sheet = doc[0]
doc.move(sheet)
  • Move by name to last position.

doc.move('Sheet1')
  • Move to position.

sheet = doc[0]
doc.move(sheet, 2)
  • Move from sheet

sheet = app.active_sheet
sheet.move()
  • Move to position.

sheet = app.active_sheet
sheet.move(2)

Remove

  • Remove by object.

sheet = doc[0]
doc.remove(sheet)
  • Remove by name.

doc.remove('One')
  • Remove from sheet.

sheet = app.active_sheet
sheet.remove()

Copy

  • Copy inside the same spreadsheet.

  • By object

sheet = doc[0]
doc.copy(sheet, 'OtherSheet')
  • By name

doc.copy('Sheet1', 'Sheet2')
  • From sheet

sheet = app.active_sheet
sheet.copy(sheet.name + '_2')

Copy from

  • Copy sheet from one spreadsheet to other.

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

doc = app.docs.new()
sheet = app.active_sheet
sheet.copy_to(doc)
  • Used new name

doc = app.docs.new()
sheet = app.active_sheet
sheet.copy_to(doc, 'NewName')

Sort

  • Sort sheets by names.

doc = app.active
doc.sort()

Name

  • Name visible by the user.

sheet = app.active_sheet
app.msgbox(sheet.name)
sheet.name = 'NewName'
app.msgbox(sheet.name)

Code name

  • Name only accessible by code.

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.

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.

sheet = app.active_sheet
app.msgbox(sheet.is_protected)

Set password

sheet = app.active_sheet
sheet.password = 'letmein'
app.msgbox(sheet.is_protected)

Remove password

sheet = app.active_sheet
sheet.password = 'letmein'
app.msgbox(sheet.is_protected)

sheet.unprotect('letmein')
app.msgbox(sheet.is_protected)

Tab color

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

doc = sheet.doc

Activate

doc = app.active
# Get last sheet
sheet = doc[-1]

# Activate from doc
doc.activate(sheet)

# Activate from sheet
sheet.activate()

Ranges

Cells

  • By name

sheet = app.active_sheet
cell = sheet['A1']
app.msgbox(cell.name)
  • By position [row, column]

cell = sheet[1, 4]
app.msgbox(cell.name)

Ranges

  • By name

sheet = app.active_sheet
rango = sheet['B2:D5']
app.msgbox(rango.name)
  • By position [start_row:end_row, start_column:end_column]

rango = sheet[1:5,1:4]
app.msgbox(rango.name)
  • Group ranges

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

ranges = doc.get_ranges('A1:B1')
app.debug(ranges.names)
  • Get columns by name.

sheet = app.active_sheet
rango = sheet['B:B']
app.debug(rango.name)

rango = sheet['D:F']
app.debug(rango.name)
  • Get columns by position.

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

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

sheet = app.active_sheet
rango = sheet['A1:E10']
app.msgbox(rango.name)
  • Get address

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

sheet = app.active_sheet
rango = sheet.used_area
app.msgbox(rango.name)
  • Get current region

rango = sheet['A1'].current_region
app.msgbox(rango.name)
  • Get next free cell

cell = sheet['A1'].next_cell
app.msgbox(cell.name)
  • Get merged area

sheet = app.active_sheet
rango = sheet['A1'].merged_area
app.msgbox(rango.name)
  • Get visible cells

sheet = app.active_sheet
rangos = sheet['A1:E10'].visible
for r in rangos:
    app.debug(r.name)
  • Get empty cells

sheet = app.active_sheet
rangos = sheet['A1:E10'].empty
for r in rangos:
    app.debug(r.name)

Manipulate ranges