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(sheet, 'OtherSheet')
  • By name

doc.copy_sheet('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()