Modifying selected cells in Writer table through a macro?

I’ve been looking all over for this and asking here is pretty much my last resort.

I want to make a macro that can modify (specifically the cell background color, or changing the text) of all selected cells, but in a Writer table, not a Calc spreadsheet. That’s it, really, but I can only find information about the later, so I am at a total loss of what to do to modify table cells, or rather, get some way to iterate through the selection. The few approaches I’ve seen only work for Calc spreadsheet cells, or don’t work at all (OpenOffice?).
I’m at a total loss about this, I’m not even sure if it can be done or not.

I don’t really care about the specifics to accomplish this task (using LO Basic or Python or anything, can handle everything except altering LO directly).

I’ve tried snippets such as

Sub ColorizeTable Dim oCurrentSelection As Variant Dim oRows As Variant Const nCellBackColor = 15132415 REM # "Blue gray" Dim i As Long
    oCurrentSelection = ThisComponent.getCurrentSelection()
    If oCurrentSelection.supportsService("com.sun.star.table.CellRange") Then
        oRows = oCurrentSelection.getRows()
        For i = 0 To oRows.getCount()-1 Step 2
            oRows.getByIndex(i).setPropertyValue("BackColor", nCellBackColor)
        Next i
    EndIf
 End Sub

but while it’s the only one that doesn’t throw an error, it doesn’t seem to do anything.

You obviously didn’t test your code in the IDE. Otherwise you would know that - the view showing a cell range selected - ThisComponent.CurrentSelection returns a TextTableCursor, not a CellRange.
Your IF-clause simply jumps to End Sub therefore.

General advice: If possible at all abandon programming for Writer - and in specific for TextTable objects. It’s a field where the API is even more messy than elsewhere. Think allone of the fact that a TextTableCursor cannot even tell you what TextTable it is roaming. You need absurd tricks to get that simple information.
Trying to program for TextTable mostly is just wasting time.
(Also: Trying to inspect objects related to TextTable often causes total crashes.)
(And yes: I wasted a lot of time in this field.)

@Lupp:

heh, I share your frustration about TextTableCursor. Some time ago I thought that I add TextTable property to the cursor (it’s easy) - even wrote about that here (yes, I remember that you wrote me last time about Russian text ;-)). But a workaround was found, and I supposed that this is not very important for people…

Having read your comment, I decided to debug the cursor. I’m amazed how people not file bugs! E.g., since ~forever (at least 2000), there’s code to set background of selected cells using BackColor property; but there’s a bug which no one filed since then! - so the code doesn’t work, and paragraph background is set instead.

Or that crash that you mention. I found the crash looking into SwXSellRange. The same story… (I’ll fix these two.)

@mikekaganski:Thanks a lot! Hoping you would read this I saved the time for reporting bugs :wink:
Next thing: As a co-author of FAF extension you may be fond to know: At the end of Module1.Line83 a closing parenthese is missing. This was not detected by the Basic “compiler” due to a former bug which supposedly was fixed without notion.
BTW Currently there are 21 unfixed bugs I reported (1 unconfirmed). 10 of “my” bugs formally reported over the years were fixed. 4 are burried “notabug”(ok…). One (rightfully) “wontfix”.

which supposedly was fixed without notion

When Legacy Justifies Errors – Mike Kaganski's blog ;-D

Great! Interim solution for 33+ years. Applause for the good idea anyway.
You (that’s me) should never underestimate a LibO developer.

I found the crash looking into SwXSellRange

Fixed in 4056b70e6339102374898fff26f099da455475b1 - core - Gitiles

there’s code to set background of selected cells using BackColor property; but there’s a bug which no one filed since then

Fixed in ecb5130e16898c0d2485e99564c57882b5ef25b0 - core - Gitiles

so that now (in 6.5) this should work:

Sub ColorizeTable
  Dim oSel As Object
  oSel = ThisComponent.CurrentSelection
  If oSel.supportsService("com.sun.star.text.TextTableCursor") Then oSel.BackColor = &H00FF0000
End Sub

In TextTable cursors and ranges are very poor and powerless objects. You cannot compare them with similarly or equally named objects in Calc. In specific a TextTable CellRange doesn’t have some of the fundamental properties you expect.
The basic bad illness concerning TextTable is that no real table concept exists for texts. Once there was decided to implement “complex tables” with “cells” merged and split in any absurd manner. This was done without insisting on a rectangular “background grid” capable of playing a similar role as a spreadsheet. Therefore the concept of rows and columns also is fundamentally messed up. LibreOffice developers probably cannot change that without omitting any compatibility with different brands insofar. (And surely not without killing the functionality of custom code developed already finding ways through the desert.)

By the way: What you asked for in the subject of your question can be done. What your code tells you actually want is impossible for complex tables, and needs usage of very different means for simple tables.

As I said I was just trying random things because I cannot find anything specific about this topic.
All I really want is to color cells quicker than going to table properties>background>etc, since there’s no UI widget for it (it changes text background/highlight, but not selected table cells like Word would do) but it seems it cannot be done in a sensible way…