Pythonガイド - シート操作
Appearance
TDF LibreOffice Document Liberation Project Community Blogs Weblate Nextcloud Redmine Ask LibreOffice Donate
CAUTION: Copy or import function msgbox from Useful functions
Names
- Get all sheet names
doc = XSCRIPTCONTEXT.getDocument()
names = doc.Sheets.ElementNames
msgbox(names)
Count
doc = XSCRIPTCONTEXT.getDocument()
count = doc.Sheets.Count
msgbox(count)
Iteration in all sheets
doc = XSCRIPTCONTEXT.getDocument()
for sheet in doc.Sheets:
msgbox(sheet.Name)
Get
- Get active sheet from current document.
doc = XSCRIPTCONTEXT.getDocument()
sheet = doc.getCurrentController().getActiveSheet()
msgbox(sheet.Name)
- Get by index
sheet = doc.Sheets[0]
msgbox(sheet.Name)
- Get by name
sheet = doc.Sheets['data']
msgbox(sheet.Name)
Activate
- Send focus to any sheet
doc = XSCRIPTCONTEXT.getDocument()
sheet = doc.Sheets[0]
doc.getCurrentController().setActiveSheet(sheet)
Change name
- Name visible for user
sheet = doc.Sheets[0]
msgbox(sheet.Name)
sheet.Name = 'OtherName'
msgbox(sheet.Name)
- Name visible only for code
CAUTION: This change is valid only in current session. It’s better not change this property and used default names.
sheet = doc.Sheets[0]
msgbox(sheet.CodeName)
sheet.CodeName = 'Data'
msgbox(sheet.CodeName)
Set visible
- Set
Falsein propertyIsVisiblefor hidden and setTruefor show.
sheet = doc.Sheets[0]
msgbox(sheet.IsVisible)
sheet.IsVisible = False
msgbox(sheet.IsVisible)
sheet.IsVisible = True
Password
- Set password
sheet = doc.Sheets[0]
contra = 'letmein'
sheet.protect(contra)
- Remove password
sheet = doc.Sheets[0]
contra = 'letmein'
sheet.unprotect(contra)
Exists
doc = XSCRIPTCONTEXT.getDocument()
exists = 'data' in doc.Sheets
msgbox(exists)
Insert
insertNewByName(NAME, POSITION)
doc = XSCRIPTCONTEXT.getDocument()
doc.Sheets.insertNewByName('new', 0)
insertByName(SHEET, POSITION)
doc = XSCRIPTCONTEXT.getDocument()
sheet = doc.createInstance('com.sun.star.sheet.Spreadsheet')
doc.Sheets.insertByName(sheet, 0)
Move
moveByName(NAME, NEW_POSITION)
doc = XSCRIPTCONTEXT.getDocument()
doc.Sheets.moveByName('new', 3)
Remove
removeByName(NAME)
doc = XSCRIPTCONTEXT.getDocument()
doc.Sheets.removeByName('new')
Replace
replaceByName(NAME, SHEET)
CAUTION: Replace sheet NAME with a clean sheet.
doc = XSCRIPTCONTEXT.getDocument()
sheet = doc.createInstance('com.sun.star.sheet.Spreadsheet')
doc.Sheets.replaceByName('data', sheet)
Copy
copyByName(SOURCE_NAME, NEW_NAME, POSITION)
CAUTION: If new name exists, copy sheet with name SOURCE_NAME_2, but not get any error.
- Copy into the same document
doc = XSCRIPTCONTEXT.getDocument()
doc.Sheets.copyByName('data', 'NewData', 10)
- Copy from other document
importSheet(SOURCE, SOURCE_NAME, POSITION)
source = XSCRIPTCONTEXT.getDocument()
desktop = app.create_instance('com.sun.star.frame.Desktop', True)
path = 'private:factory/scalc'
doc = desktop.loadComponentFromURL(path, '_default', 0, ())
doc.Sheets.importSheet(source, 'Services', 0)