1
sheets
Mauricio Baeza edited this page 2019-10-09 16:50:42 -05:00
Table of Contents
Sheets
All sheets
- Get all sheet names
doc = app.get_document()
app.msgbox(doc.names)
- Iter in sheets
doc = app.get_document()
for sheet in doc:
app.msgbox(sheet.name)
Get
- Active sheet
doc = app.get_document()
sheet = doc.active
app.msgbox(sheet.name)
- Get by index
sheet = doc[0]
app.msgbox(sheet.name)
# Last
sheet = doc[-1]
app.msgbox(sheet.name)
- Get by (code) name
IMPORTANT: This name it's a code name, internal name of sheets. You can change, but it's better not. If user change the name to sheet, this name not change, this is good for code.
sheet = doc['Sheet2']
app.msgbox(sheet.name)
- If you get by name with the name in UI, used.
sheet = doc.sheets['Data']
app.msgbox(sheet.name)
Activate
- By name
doc = app.get_document()
doc.activate('Sheet3')
- By object
doc = app.get_document()
sheet = doc['Sheet1']
doc.activate(sheet)
- By method in sheet
doc = app.get_document()
sheet = doc['Sheet2']
sheet.activate()
Name
- Change name visible by user
doc = app.get_document()
sheet = doc.active
sheet.name = 'New'
- Name visible only for code
CAUTION: This change is valid only in current session. It's better not change.
doc = app.get_document()
sheet = doc.active
sheet.code_name = 'data'
Set visible
doc = app.get_document()
sheet = doc.active
sheet.visible = False
app.msgbox(sheet.name)
sheet.visible = True
Password
doc = app.get_document()
sheet = doc.active
password = 'letmein'
sheet.password = password
app.msgbox(sheet.name)
if not sheet.unprotect('abrete'):
app.msgbox('Incorrect password')
sheet.unprotect(password)
Exists
doc = app.get_document()
app.msgbox('Sheet2' in doc)
Insert
doc = app.get_document()
doc['Data'] = doc.create()
doc.insert('NewSheet')
names = ('New1', 'New2', 'New3')
doc.insert(names)
Move
- By default move to last position.
doc = app.get_document()
doc.move('Sheet1')
doc.move(0)
doc.move(-1, 0)
Remove
doc = app.get_document()
doc.insert('New')
app.msgbox(doc[-1].name)
doc.remove('New')
doc.insert('Other')
app.msgbox(doc[-1].name)
doc.remove(-1)
Copy
- Into the same document, make sure that the not exists new name.
doc = app.get_document()
doc.copy('Sheet1', 'NewName')
# Set new position
doc.copy('NewName', 'OtherName', 0)
- Copy several sheets, make sure the source and names tuples must be the same size.
source = ('Sheet1', 'Sheet2', 'Sheet3')
names = ('New1', 'New2', 'New3')
doc.copy(source, names)
Copy from other document
- By name
doc = app.get_document()
source = app.get_document('Source.ods')
names = 'Data1'
doc.copy_from(source, names)
- Several names
doc = app.get_document()
source = app.get_document('Source.ods')
names = ('Data1', 'Data2')
doc.copy_from(source, names)
- All sheets
doc = app.get_document()
source = app.get_document('Source.ods')
doc.copy_from(source)
- Set names for copies, make sure the booth tuples must be the same size.
names = ('Data1', 'Data2')
new_names = ('New1', 'New2')
doc.copy_from(source, names, new_names)
Sort
- Sort sheets by name
doc = app.get_document()
names = ('Z', 'L', 'M', 'B', 'A')
doc.insert(names)
doc.sort()
app.msgbox('Sorted...')
# Reversed sort
doc.sort(True)