Re: how to convert VB scripts to Gnumeric python



I agree, for a python user the Gnumeric interface feels pretty alien, some kind of wrapper library would be a godsend. The python interpreter plugin had elements of a basic API in it, though as noted it's not really usable. As your example shows you really don't need that much in order to do useful things.

With my application these ended up being the primitives functions used:

    - Open file
    - Save file (including saving new files )
- Get workbook object (by workbook_get_by_index or via workbook_get_by_name)
    - Get sheet object (by index or sheet_by_name)
- Get cell content (using sheet.cell_get(col, rol) and sheet.cell_fetch(col, row) ) - Get cell content and style (using cell.get_rendered_text() and cell.get_style() ) - Set cell content and style (using cell.set_text() and sheet.style_set_pos() ) - as noted setting style caused gnm_style_unref errors - Some odds and ends related to sheet styling (sheet.style_get(col, row).get_border(5).line_type and sheet.row_is_hidden(row) sheet.col_is_hidden(col) ) that was specific to my application

With those pretty much everything can be done in python to construct more advanced operations (like copying and pasting a block.) This is slight off-topic, but the python interpreter hack allowed me to add some functionality to menus to extend python, having a new plugin to do that would be very handy but obviously a side project. One of the features Excel had that I ended up having to emulate in Python was cut/paste visible cells only, for example.

I played around a lot with using the sheet view interface initially (as I was trying to copy VB macros), primarily to do cut and paste. In the end it wasn't as reliable as I liked and I decided you could implement copy and paste using the get/set cell primitives instead. I continue to have issues with style formatting, though, and multi-cell formatting (like text wrapped across cells) was difficult enough that I skipped coding for it.

I don't have notes as to why but I know I had to avoid using the get/set value for some reason, it may be as simple as not knowing to use the Gnm.Value.new_int() object like you did in your example. Most of my application involves extracting and reformating existing data in sheets to new workbooks, so it may have simply been my dealing with existing data.

I don't think it would be too hard to put together a basic example application akin to a hello_world that does something simple like open a workbook and place data from an RSS feed into it and save it out.

What would be the best venue to share the initial patches and code without clogging the list with very long messages? Github?


On 04/12/2018 06:00 AM, Morten Welinder wrote:
Some progress.  I have something like this working now:

    import gi
    gi.require_version('Gnm', '1.12')
    from gi.repository import Gnm
    Gnm.init()

    wb = Gnm.Workbook.new_with_sheets(1)
    sheet = wb.sheet_by_index(0)
    sheet.cell_set_value(0,0,Gnm.Value.new_int(10))
    sheet.cell_set_value(0,1,Gnm.Value.new_float(1.0/3.0))
    sheet.cell_set_text(0,2,"=A1+A2")
    wb.recalc()

    for i in range(3):
        print sheet.cell_get_value(0,i).peek_string()

It's not a great API. In fact, in it shows clear signs of being a bit foreign to Python.

A gotcha: all versions of introspection until about 2 months ago misunderstood "char const *" as "char *" causing problems with functions like value_peek_string. Such functions will need to have their return value marked "transfer none". ("const char *" worked fine, on the other hand.)

M.





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]