Development/Create a Hello World LibreOffice extension

From The Document Foundation Wiki
Jump to: navigation, search

Other languages:
dansk • ‎English • ‎italiano

In this tutorial we will create a Hello World extension for LibreOffice Calc using LibreOffice Basic language. It is a simple extension to illustrate some of the basic elements of an extension in LibreOffice. The extension will add a button (next to Export as PDF button) to standard toolbar and when it is clicked it shows a dialog with “Hello, World!”. Source code for this tutorial is on github.

Result.png

Step 1. Creating a folder for our extension

1) Choose a location and create a folder named helloworld.

Create a folder.png

Step 2. Creating a library for our macro

1) Open LibreOffice Calc. From the Menu Bar, choose Tools > Macros > Organize Macros> LibreOffice Basic.

2) Click Organizer button.

Figure2.1.png

3) Choose Libraries tab, and click New button.

Figure2.2.png

4) Name it HelloWorldLibrary, and click OK. We can see our created library in the Library list.

Figure2.3.png


Figure2.4.png

5) Click Close button to exit from LibreOffice Basic Macro Organizer dialog.

Step 3. Creating the macro

1) Choose My Macros > HelloWorldLibrary >Module1, and click Edit button.

Figure3.1.png

2) Copy macro below and paste into the LibreOffice Basic IDE, then save (Ctrl+S).

Sub HelloWorldMacro
   Print "Hello, World!"
End Sub

Figure3.2.png

Step 4. Exporting our library

1) From the LibreOffice Basic IDE Menu bar, choose Tools > Organize Macros > LibreOffice Basic.

2) Click Organizer button.

Figure4.1.png

3) Select Libraries tab, and choose HelloWorldLibrary from the Library list, then click Export button.

Figure4.2.png

4) Select Export as BASIC library and click OK.

Figure4.3.png

5) Save the library inside our helloworld folder, then click Close to exit from LibreOffice Basic macro organizer and LibreOffice Basic macros. As we see below, the library we exported contains 3 files.

Figure4.4.png

Step 5. Adding a description to our extension

1) Go to folder helloworld, then create a subfolder named pkg-description.

Figure5.2.png

2) Inside pkg-description folder, create a file named pkg-description.en.

Figure5.3.png

3) Paste content below into the file pkg-description.en, click save (Ctrl+S) and close the file.

Hello World LibreOffice extension. Copyright (c) your name 2019.

Step 6. Adding a license

1) Inside folder helloworld, create a subfolder named registration.

2) Inside folder registration, create a file named license.txt, and paste into it the content from this link. Then save (Ctrl+S) and close file.

Step 7. Adding manifest file

1) Inside folder helloworld, create a subfolder named META-INF.

2) Inside folder META-INF, create a file named manifest.xml, and paste into it the content below. Then save (Ctrl+S) and close file.

<?xml version="1.0" encoding="UTF-8"?>
<manifest:manifest>
   <manifest:file-entry manifest:full-path="HelloWorldLibrary/" manifest:media-type="application/vnd.sun.star.basic-library"/>
   <manifest:file-entry manifest:full-path="pkg-desc/pkg-description.en" manifest:media-type="application/vnd.sun.star.package-bundle-description;locale=en"/>
   <manifest:file-entry manifest:full-path="pkg-desc/pkg-description.pt-BR" manifest:media-type="application/vnd.sun.star.package-bundle-description;locale=pt-BR"/>
   <manifest:file-entry manifest:full-path="Addons.xcu" manifest:media-type="application/vnd.sun.star.configuration-data"/>
</manifest:manifest>

Step 8. Adding images

1) Inside folder helloworld, create a subfolder named icons.

Figure8.1.png

2) Go to this link and download the 2 images there, save them to the icons folder.

Step 9. Adding the description file

1) Inside folder helloworld, create a file named description.xml.

Figure9.1.png

2) Paste the content below into description.xml file. Then save (Ctrl+S) and close file.

<?xml version='1.0' encoding='UTF-8'?>
<description
 xmlns="http://openoffice.org/extensions/description/2006"
 xmlns:dep="http://openoffice.org/extensions/description/2006"
 xmlns:xlink="http://www.w3.org/1999/xlink">
    <identifier value="org.broffice.helloworldmacro"/>
    <icon>
        <default xlink:href="icons/image_42.png" />
    </icon>
    <version value="1.0"/>
    <registration>
        <simple-license  accept-by="admin" default-license-id="ID0" suppress-on-update="true" >
        <license-text xlink:href="registration/license.txt" lang="en" license-id="ID0" />
        </simple-license>
    </registration>
    <publisher>
        <name xlink:href="mailto:youremail@smt.com">Your name</name>
    </publisher>
    <display-name>
        <name>Hello World</name>
    </display-name>
</description>

Step 10. Adding a button to toolbar of LibreOffice Calc

1) Inside folder helloworld, create a file named Addons.xcu.

2) Copy this code and paste into the Addons.xcu file. Then save (Ctrl+S) and close file.

Step 11. This is our file structure so far

Figure11.1.png

Step 12. Packaging the extension

1) Inside helloworld folder, select all files (Ctrl+A).

2) Right-click, and compress to helloworld.zip.

Figure12.1.png

3) Rename helloworld.zip to helloworld.oxt. Now our extension is ready and in the next step we are going to install in LibreOffice Calc.

Figure12.2.png

Step 13. Installing our extension in LibreOffice Calc

1) Open LibreOffice Calc and choose Tools > Extension Manager.

2) Click Add button, and select our extension helloworld.oxt.

3) Accept the Extension Software License Agreement to install the extension.

Figure13.1.png

4) Click Close button, and restart LibreOffice Calc.

Conclusion

In this tutorial, we have created a Hello World extension for LibreOffice Calc. Complete source code can be found on github.