Lodahl's blog: Macro
Showing posts with label Macro. Show all posts
Showing posts with label Macro. Show all posts

03 December 2013

LibreOffice now has a built in XML-parser

LibreOffice is using a XML based document format so of cause there is a built in XML parser. But until now it has been quite cumbersome to deal with XML in macros. You need to manually traverse through the entire XML structure like this example (thanks to Andrew Pitonyak):

Function CreateDocumentHandler()
oDocHandler = CreateUnoListener( "DocHandler_", "com.sun.star.xml.sax.XDocumentHandler" )
glLocatorSet = False
CreateDocumentHandler() = oDocHandler
End Function

'==================================================
' Methods of our document handler call these
' global functions.
' These methods look strangely similar to
' a SAX event handler. ;-)
' These global routines are called by the Sax parser
' as it reads in an XML document.
' These subroutines must be named with a prefix that is
' followed by the event name of the com.sun.star.xml.sax.XDocumentHandler interface.
'==================================================

Sub DocHandler_characters( cChars As String )

if xNode = "lipsum" then
oWrite=1
cChars= Left(cChars,len(cChars)-1)
if len(cChars)>1 then
cChars= cChars+ Chr$(13)
else
cChars=cChars
endif
WriteLoremipsum (cChars, oWrite)
Else oWrite=0
Endif
End Sub

Sub DocHandler_ignorableWhitespace( cWhitespace As String )
End Sub

Sub DocHandler_processingInstruction( cTarget As String, cData As String )
End Sub

Sub DocHandler_startDocument()
End Sub

Sub DocHandler_endDocument()
End Sub

Sub DocHandler_startElement( cName As String, oAttributes As com.sun.star.xml.sax.XAttributeList )
xNode = cName
End Sub

Sub DocHandler_endElement( cName As String )
End Sub

Sub DocHandler_setDocumentLocator( oLocator As com.sun.star.xml.sax.XLocator )
' Save the locator object in a global variable.
' The locator object has valuable methods that we can
' call to determine
goLocator = oLocator
glLocatorSet = True
End Sub

This example above is from the extension Lorem Ipsum generator that you can download from here: http://extensions.libreoffice.org/extension-center/magenta-lorem-ipsum-generator

But now its much easier as LibreOffice 4.2 comes with two new spreadsheet functions called WEBSERVICE and FILTERXML. In a macro it is possible to call and use such built in spreadsheet functions even when you are working with text documents.

The example below does pretty much the same as the one above

Sub Main
svc = createUnoService( "com.sun.star.sheet.FunctionAccess" ) 'Create a service to use Calc functions
XML_String = svc.callFunction("WEBSERVICE",array("http://www.lipsum.com/feed/xml?amount=2&what=paras&start=Yes"))
Lipsum = svc.callFunction("FILTERXML", array(XML_String, "/feed/lipsum" ))
Print Lipsum
End Sub

I'm really looking forward play around with these nifty little features in Calc.

15 March 2011

Macroprogramming with Python

I've been doing some investigations on how to do macroprograming with Python. I was so lucky to find a instant word counter from https://bitbucket.org/yawaramin/oo.o-live-word-count/overview and started to hack on it. Hacking other peoples code is a very good way to get into new areas of coding.

I succeeded to improve the macro by adding a graphical progress bar.

The code will of cause be added to upstream ;-)

03 October 2007

Extending OpenOffice.org

Sorry, I have been away from my blog for a few days.

This summer I wrote a Danish manual about extensions and how to create and maintain extensions( http://doc.oooforum.dk/Extensions.pdf ). When I wrote the manual, I used both the wiki on http://www.OpenOffice.org : http://wiki.services.openoffice.org/wiki/Extensions . I also used two articles by Dimitri Popov (http://www.linuxjournal.com/article/7802 and http://www.linuxjournal.com/article/9412).

Dimitri is using an example in his article, where he is creating dummy text with Lorem Ipsum. I remember, that it was annoying that the example was masde as a mock up as example and didn't actually create Lorem Ipsum text. I know that this was not the purpose of the article, but I was still thinking: "Why not do it ?".

I have never made a macro in OpenOffice.org before, so I took this as a challenge. I had to find out everything from the beginning and I has some difficulties extracting data from the XML stream. The solution is a little clumsy, I will admit that. If any of you know a better and more elegant way, please feel free to make a new macro.

There is still a few other issues , but I expect to get them solved over the next few weeks.

I have uploaded the solution as extension to the repository, but it's actually not published yet. You can find the odt-file here: http://extensions.services.openoffice.org/download/288

When you have installed the extension, you will see a new tool bar with a button. This button will call http://www.lipsum.com/ give you some dummy text. Not rocket science, but anyway a helpfull feature.

Thanks to DannyB for this description of how to parse an XML stream http://www.oooforum.org/forum/viewtopic.phtml?t=4907 and to Andrew Pitonyak for his dokumentation "Useful Macro Information".