Function to return Calc Document containing a Spreadsheet

Shared Libraries
Forum rules
For sharing working examples of macros / scripts. These can be in any script language supported by OpenOffice.org [Basic, Python, Netbean] or as source code files in Java or C# even - but requires the actual source code listing. This section is not for asking questions about writing your own macros.
Post Reply
User avatar
gurfle
Posts: 67
Joined: Wed Dec 02, 2009 5:40 am

Function to return Calc Document containing a Spreadsheet

Post by gurfle »

EDIT: Based on this, here seems to be THE way to do it:

Code: Select all

function WorkBookof(oWS as Object) as Object
  WorkBookof=oWS.getDrawPage().getForms().getParent()
End Function
and forget the kluge shown below ;) .

After a little discussion on this topic here I came up with this somewhat awkward and limited solution to what appears to be, in the words of B Marcelly, "A missing facility of the API"

I am not versed enough at searching the OOo Feature/Bug tracking system to tell if adding a feature like this is something that is already an issue there. Does anyone here know?

If not, what do folks here think about proposing such a feature. In the work I do, I know it would be very handy to add the Calc Document containing a Spreadsheet as a property of ScTableSheetObj.

Code: Select all

function WorkBookof(oWS as Object) as Object
  ' Until the OOo API includes a feature to discover the Document (Calc "Workbook") containing a Spreadsheet
  ' This Function, inspired by JohnV's contribution to the discussion in
  ' http://user.services.openoffice.org/en/forum/viewtopic.php?f=45&t=33177#p152538
  ' could be used with the following
  ' Limitations:  Assumes that the Cell Z100 is modifyable
  Dim sDocURL as String, oDoc as Object, oFrame as Object, oDispatcher as Object
  oWS.getCellRangeByName("Z100").setformula("=CELL(""filename"";$A$1)")
  sDocURL=oWS.getCellRangeByName("Z100").getstring
  sDocURL=mid(sDocURL,2,instr(sDocURL,"#")-3)
  oDoc=stardesktop.loadComponentFromURL(sDocURL,"_default",0,Array())
  oFrame=oDoc.CurrentController.Frame
  oDispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
  oDispatcher.executeDispatch(oFrame, ".uno:Undo", "", 0, Array())
  WorkBookof=oDoc
End Function
Nicholas Dreyer
AMD sempron 3.4GHz, 1G RAM, nForce3-250Gb motherboard

AntiX M11
LibreOffice 3.4.3

Posts prior to Aug. 18, 2011 referenced
Linux Debian (lenny-backports or squeeze)
OpenOffice 3.2.1
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Function to return Calc Document containing a Spreadshee

Post by Villeroy »

No need to use a cell

Code: Select all

srv = createUnoService("com.sun.star.sheet.FunctionAccess")
sDocURL = srv.callFunction("CELL", Array("filename"))
But what if the file has not been stored yet?

Just another alternative I read on oooforum.org:

Code: Select all

Sub Snippet(Optional oInitialTarget As Object)
  Dim oSpreadsheet As Object
  Dim oDrawPage As Object
  Dim oForms As Object
  Dim oParent As Object

  oSpreadsheet = oInitialTarget.getSpreadsheet()
  oDrawPage = oSpreadsheet.getDrawPage()
  oForms = oDrawPage.getForms()
  
  oParent = oForms.getParent()
End Sub
Above Basic code has been generated by the MRI extension starting at the current cell as oInitialTarget. The form's parent gives the document model.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Post Reply