Search code examples
linuxmacrosopenoffice-writeropenoffice-basic

OpenOffice Macro to Access Contents of a Table


I wrote a Macro, which should take two Dates (dd.mm.yyyy) as a String from a table in an OpenOffice Document (Writer, not Calc). These two Dates should be merged to this: ddmmyyyy-ddmmyyyy. This should be used as the filename then.

The Table has only one row and 6 columns, the first Date being in table2:D1:D1 and the second one in table2:F1:F1. I "translated" this to table2(1, 4) and table2(1, 6)

This German site is doing what I want to do, but with spreadsheets in a OOCalc Document and not OOWriter.

Enough talk, here is my code:

sub save
  oDoc=thisComponent

  sStartDate = oDoc.Table2(1, 4)
  sEndDate = oDoc.Table2(1, 6))

  sFilename = sStartDate.String & sEndDate.String

  sURL = ConvertToURL("file:///home/cp/Documents/" & sFilename & ".odt")

  msgbox sURL
  ' oDoc.StoreAsURL(sURL, Array())

end sub

Yes, I run linux, so the path should be correct. When I try to run this script it says:

Property or Method not found table2

I of course tried google but somehow I could not find a solution. A hint in the right direction could be enough, I also guessed I have to write "more":

sStartDate = oDoc.getString(table2(1, 4))

or something similar. Didnt work either. Another thing I tried was using (0, 3) instead of (1, 4).

Well I would appreciate it, if someone could help me a bit! :) And I hope I have done everything right how I posted here.

Vaelor

EDIT: I have now modified the script to this, according to the PDF found HERE in chapter 14.9.

It now looks like this,

  sub save

  oDoc=thisComponent
  Dim oTable
  Dim sTableName As String  

  sTableName = "Table2"

  oTable = oDoc.getTextTables().getByName(sTableName)
  ' oTable = oTables.getByName(sTableName)


 sStartDate = oTable.getCellByPosition(0, 3)
 sEndDate = oTable.getCellByPosition(0, 5)

 sFilename = sStartDate.String & sEndDate.String

 sURL = ConvertToURL("file:///home/cp/Documents/" & sFilename & ".odt")

 msgbox sURL
 ' oDoc.StoreAsURL(sURL, Array())

 end sub

But, still not working. Now I get this exception IndexOutOfBoundsException. (I wanted to link it, but it says, I cant post more than 2 links :-( )

My first thought was I had to change the cels to 0, 3 and 0, 5. After changing that, the error still occurs. :-(

Edit2: Since I got no response, I think I will try this in Python, maybe it yields better results.


Solution

  • This code demonstrates how to locate a text table with the given name and how to access individual cells.

    function get_table_by_name(name as string) as object
        dim oenum as object
        dim oelem as object
    
        oenum = thisComponent.text.createEnumeration
        while oenum.hasMoreelements
            oelem = oenum.nextElement
            if oelem.supportsService("com.sun.star.text.TextTable") then
                if oelem.Name = name then
                    get_table_by_name = oelem
                    exit function
                end if
            end if
        wend
    end function
    
    Sub Main
        dim table as object
    
        table = get_table_by_name("Table1")
        if not isNull(table) then
            msgbox "Got " & table.Name & " " & table.getRows().getCount() & "x" & table.getColumns().getCount()
            msgbox "Cell[0,0] is " & table.getCellByPosition(0, 0).getString()
        end if
    End Sub