[LibreOffice Writer] Table with alternative row color

Hi,

I'm looking for a simple and automatic way how to tell to my tables in a writer document, to be formatted with alternative rows color
for example even rows will be blue in the background and odd rows will have a light blue as background color.

I did not find any easy way to do it.
Is there someone who has already solved this topic ?
thx

menu:Table>AutoFormat...

Try the Color2Rows extension, available at http://extensions/openoffice.org/en/project/color2rows.

jonathon

Not me.

o Once you have set the background colour for one row, you can use the Format Paintbrush to paint the same colour on other rows. If you double-click the button, you can drag the "paint bucket" across alternate rows to achieve one colour and then repeat the process for the other colour.

o For large numbers of rows, you can copy an entire existing table and paste it immediately following - and you can repeat that process. You'll end up with multiple tables, but that may suffice, providing you delete the empty paragraph between them. If you need to, you can put the cursor into the second table and go to Table | Merge Tables (or right-click | Merge Tables) to merge the two tables into one.

o It is somewhat easier to set up such background colouring in a spreadsheet than in a Writer table. If you colour two rows you can easily copy them to two more. Then you can copy four rows to create eight in total. With this binary multiplication, you can very quickly achieve the desired size. If you then copy an appropriately-sized section of the spreadsheet and paste it into your text document using Paste Special... and "HTML (HyperText Markup Language)" you will get a table with the same background formatting. The table properties can be adjusted as necessary.

o The automatic way to colour spreadsheet rows alternately is to use Conditional Formatting and create a formula using the ROW() function to determine the oddness or evenness of the row number and control the background colouring using cell styles. But unfortunately - and as you might expect - the effect of such formatting is lost if you paste a copy into a text document as a table. So that's a dead duck.

o It's probably much easier to set up formatting such as this before you insert your data. If necessary, you should be able to copy any existing material and paste it into a new, formatted table.

I trust this helps.

Brian Barker

postmaster@btinternet.com)
row color
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit

Alain,

I don't know whether you consider this to be "simple and automatic" but what I've found is this:

Use LibreOffice to create a spreadsheet.

Select the table space.

Use Format=>Conditional Formatting=>Condition

Condition 1:
  Formula is ISEVEN(ROW())
  select or create your style for even rows

Add

Condition 2:
  Formula is ISODD(ROW())
  select or create your style for odd rows

OK

Copy the selected cells and paste them into your writer document.

This is the best solution I have found. Maybe it will tickle someone else's creativity to come up with a better solution.

It's not clear whether you appreciate that this is the method that I described as a dead duck! Could you come clean about exactly how you are making this work, please?

Depending on how I paste the cell range from a spreadsheet, I can create:
o a striped graphic with no row or column structure - just a picture,
o an embedded spreadsheet section,
o a blank picture,
o a stack of paragraphs containing only sequences of tab characters, or
o the required table - but without the background formatting!

In particular, freezing the effect of the conditional formatting in the spreadsheet before copying the range of cells to the text document might be expected to work, but I didn't see any way to do this.

A striped panel is not going to provide the alternately coloured table rows that the questioner wanted. Do please explain how you have managed to paste the spreadsheet range to create a *formatted table* in the text document.

Brian Barker

postmaster@btinternet.com)
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit

I performed a simple paste. I guess that technically it's an image (or if you like, a picture) but the contents can be editted.

I don't generally use Writer but I do use Calc a lot. I knew that I could generate the alternating color bands in Calc by means of formulas so I first looked for that functionality in Writer to no avail. Then I tried the technique I posted and editted the data successfully so I posted it.

Maybe I'm being over sensitive but I sensed hostlity in your response to me. I thought this list was one where everyone (expert or novice) shared for the benefit of all.

Maybe I'm being over sensitive but I sensed hostility in your response to me.

Oh, no: not at all! Sorry you got that impression. Indeed, I was surprised that you hadn't explicitly suggested that my rejection of the method as a dead duck in my earlier message was wrong. And I replied as I assumed that, unlike me, you had made this method work and I was genuinely interested to know how.

I thought this list was one where everyone (expert or novice) shared for the benefit of all.

Of course it is! Let's not change that.

Brian Barker

postmaster@btinternet.com)
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit

Thanks Brian. It looks like Paste in this case is the same as Paste Special Calc8. They both retain the colors, some formulas, and spreadsheet context (Row & Column identities).

I wrote a macro some years back and I still use it today. You probably have no interest in the part that sets the borders...

Sub FormatTable(Optional oUseTable)
   Dim oTable
   Dim oCell
   Dim nRow As Long
   Dim nCol As Long

   If IsMissing(oUseTable) Then
     oTable = ThisComponent.CurrentController.getViewCursor().TextTable
   Else
     oTable = oUseTable
   End If
   If IsNull(oTable) OR IsEmpty(oTable) Then
     Print "FormatTable: No table specified"
     Exit Sub
   End If

   Dim v
   Dim x
   v = oTable.TableBorder
   x = v.TopLine
   x.OuterLineWidth = 2
   v.TopLine = x

   x = v.LeftLine
   x.OuterLineWidth = 2
   v.LeftLine = x

   x = v.RightLine
   x.OuterLineWidth = 2
   v.RightLine = x

   x = v.TopLine
   x.OuterLineWidth = 2
   v.TopLine = x

   x = v.VerticalLine
   x.OuterLineWidth = 2
   v.VerticalLine = x

   x = v.HorizontalLine
   x.OuterLineWidth = 0
   v.HorizontalLine = x

   x = v.BottomLine
   x.OuterLineWidth = 2
   v.BottomLine = x

   'v.Distance = 51

   oTable.TableBorder = v

   For nRow = 0 To oTable.getRows().getCount() - 1
     For nCol = 0 To oTable.getColumns().getCount() - 1
       oCell = oTable.getCellByPosition(nCol, nRow)
       If nRow = 0 Then
         oCell.BackColor = 128
         SetParStyle(oCell.getText(), "OOoTableHeader")
       Else
         SetParStyle(oCell.getText(), "OOoTableText")
         If nRow MOD 2 = 1 Then
           oCell.BackColor = -1
         Else
           REM color is (230, 230, 230)
           oCell.BackColor = 15132390
         End If
       End If
     Next
   Next
End Sub

I din't remember if this has already been mentioned in this thread, but you can do this simply with an Autoformat.

Just take 3 rows of your table (or create a special table for this). Then color the rows alternatively. Or you can color the header row with a different color if so desired. Then select these 3 rows and click "Table -> AutoFormat". Click "Add"" and give your autoformat a name.
Then select the table to be formatted and Autoformat it with the newly created AutoFormat. You can indicate if you want a header row.

You can also create the AutoFormat in a Calc spreadsheet, but there you need at least a 3x3 rectangle. In a Writer table you only need at least 3 rows.

It is also possible to do striped columns, then you would need at least 3 columns.

Some Caveats:
1. In Calc If you give the new AutoFormat a name that lexicographically sorts before "Default", e.g. when it starts with A, B or C, then it just disappears. In Writer that is not the case.

2. If you later insert or delete an odd number of rows from the table, they will no longer be alternatively colored. You will have to re-apply the format.

3. An autoformat has a column header (the first row) that you can disable when applying the AutoFormat. It also has a totals row at the bottom, and a rows header column and a totals column at the right that you cannot disable (This is a design error). If you don't color your columns differently the row header and Totals columns are no problem. But the Totals row always gets the color of the last row in the AutoFormat, and so if your AutoFormat has an even number of rows (excluding a possible header row), and your table has an odd number of rows, or vice versa, then the last row will get the wrong color. You can correct this by creating the table with one row more, and then deleting the last row. If you would have striped columns, I guess the same will happen in the other direction, but I haven't tried.

Aha: the perfect answer!

Brian Barker

Thank you Andrew. That looks like it's a full answer to the question. I have learned something from this. I might not use it often but I now have it in my bag of tools and who knows ....