How can I update LibreOffice Calc cells in real-time from a MySQL database?

5.8k views Asked by At

I'm looking to write a program to update cells in LibreOffice Calc in real-time (or at least at some fixed tick) with data pulled from a MySQL database. Ideally, when the values in the database are updated, the corresponding cells in the spreadsheet would be updated such that any formulas or calculations existing in calc would continue to operate on the new values. So far, I have yet to find a way to dynamically and programatically insert data in this manner. Is it possible?

2

There are 2 answers

0
Lyrl On

The LibreOffice component Base is a database front-end that handles queries, forms, and reports. While by default it uses an embedded version of HyperSQL database to manage the tables, it comes with drivers for any number of other back-end programs, including MySQL.

I think the easiest way to approach this would be to create a Base file with your MySQL database as its back-end (note Base will only be able to see tables and views from MySQL - it won't import queries; although you can save queries in the Base file if you want). Make sure to 'register' the Base file so the rest of LibreOffice can 'see' it. Once the file is registered, any open LibreOffice component can access the data from MySQL (Base file can be closed).

Now you can import any tables or views (from the MySQL component) or queries (from the Base file) into Calc: [Tutorial] Using registered datasources in Calc

Refreshing the imported data can be done through an API call. Here is an example in StarBasic code:

Sub refresh_DBRanges
    Dim oDBRangesEnum
    Dim oNext

    oDBRangesEnum = thisComponent.DatabaseRanges.createEnumeration()
    while oDBRangesEnum.hasMoreElements()
        oNext = oDBRangesEnum.nextElement()
        oNext.refresh()
    wend

End Sub

Note in the second posting of the 'registered data sources' tutorial, it gives the API call to set the import ranges on a refresh timer.

1
davehill On

Just a note that the Registered DataSources Tutorial is updated further down its page. It says the list of registered data sources can be accessed by hitting F4. That was true once but was changed with version 5. It's now Ctrl+Shft+F4.