| View previous topic :: View next topic |
| Author |
Message |
cowmix Newbie

Joined: 11 Sep 2008 Posts: 3
|
Posted: Thu Sep 11, 2008 10:13 am Post subject: Print out a list of 'sheets' in a document (Python). |
|
|
I'm able to open up any Calc document but I'm stuck on how I can extract the names of each 'sheet' in a spreadsheet using Python.
thanks! |
|
| Back to top |
|
 |
vitcaro OOo Advocate


Joined: 20 Feb 2007 Posts: 256 Location: Italy
|
Posted: Thu Sep 11, 2008 10:07 pm Post subject: |
|
|
Here is an example:
| Code: |
import uno
def ListSheets():
"""Prints sheet names"""
ctx = uno.getComponentContext()
smgr = ctx.ServiceManager
desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
oDoc = desktop.loadComponentFromURL( "private:factory/scalc","_blank", 0, () )
oSheets = oDoc.getSheets()
oSheet0 = oSheets.getByIndex(0)
Count = oSheets.getCount()
for i in range(Count):
oSheet = oSheets.getByIndex(i)
Name=oSheet.getName()
oCell = oSheet0.getCellByPosition(0,i)
oCell.setString(str(i)+'='+Name)
return None
|
|
|
| Back to top |
|
 |
Villeroy Super User


Joined: 04 Oct 2004 Posts: 10065 Location: Germany
|
Posted: Fri Sep 12, 2008 7:10 am Post subject: |
|
|
In Basic it can be implemented as a one-line function to be called as an array-function directly in the spreadsheet document:
| Code: |
Function getSheetList()
getSheetList = ThisComponent.Sheets.getElementNames()
End Function
|
=getSheetList() [Ctrl+Shift+Enter] puts a horizontal list of all sheet names.
=TRANSPOSE(getSheetList()) [Ctrl+Shift+Enter] puts a vertical list of all sheet names.
btw: This sub-forum "Code Snippets" is not for answering questions. Seemingly
oooforum.org is going to become a cluttered spam-pit anyway. _________________ Rest in peace, oooforum.org
Get help on http://forum.openoffice.org |
|
| Back to top |
|
 |
|