OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Print out a list of 'sheets' in a document (Python).

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
cowmix
Newbie
Newbie


Joined: 11 Sep 2008
Posts: 3

PostPosted: Thu Sep 11, 2008 10:13 am    Post subject: Print out a list of 'sheets' in a document (Python). Reply with quote

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
View user's profile Send private message
vitcaro
OOo Advocate
OOo Advocate


Joined: 20 Feb 2007
Posts: 256
Location: Italy

PostPosted: Thu Sep 11, 2008 10:07 pm    Post subject: Reply with quote

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
View user's profile Send private message
Villeroy
Super User
Super User


Joined: 04 Oct 2004
Posts: 10106
Location: Germany

PostPosted: Fri Sep 12, 2008 7:10 am    Post subject: Reply with quote

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 https://forum.openoffice.org
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group