# Can attributes such as background color be encoded in a raw .csv file?

I know about conditional formatting, where field attributes can depend on contents of the field or other fields. What I'd like to do is colorize certain fields as determined programmatically while generating a raw separator-separated list, so that when importing into LibreOffice, those fields will be colorized.

Of course, I can add a new column with a flag indicating whether the field should be colorized, but this still requires me to do the conditional formatting after importing the .csv file. I want the .csv file to be portable so that anyone who just imports it will see the colorized fields.

I'm envisioning syntax something like this to cause the third column in this row to be red:
data 1~data 2~<red>data3</red>~data4~

edit retag close merge delete

Sort by » oldest newest most voted

By definition, a CSV file doesn't support text formatting. See, for example, http://www.computerhope.com/jargon/c/... and http://www.computerhope.com/jargon/p/....

So... Nope. At least anything along those lines wouldn't be portable.

You can, however, write your program to do this using HTML. For example, if you save the following in a .html file and open it in Calc, three items will be colored as indicated.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body>
<table>
<tr><td><font color="red">data1</font></td><td>data1</td><td>data1</td></tr>
<tr><td>data4</td><td><font color="green">data5</font></td><td>data6</td></tr>
<tr><td>data7</td><td>data8</td><td><font color="blue">data9</font></td></tr>
</table>
</body>
</html>


You can also save a Calc document in HTML, and it'll retain font formatting in the same way.

If you want to set the background color of a cell instead of the font color, you'd use <td bgcolor="red">data1</td> rather than using font tags as above.

more

Working with Calc it is bad practise to code relevant information using attributes only. Creating explicit data to give this information would also allow to save it in a csv, a "B(111,222,333)" e.g. telling there is a BckColor described by the specific RGB triple.

To do so will require using custom code. It's a complication - but not a prohibitive one. I can provide an approptiate function if you are interested.

(Irrelevant information needn't be transmitted.)

===Edit 2019-05-10:25 UTC+02:00 reagrding the comment below===
This thread is very old, and I do not exactly remember what function I had in mind when I first answered.
I posted related code herelater, and in the very well structured forum https://forum.openoffice,org/en (branch "Code Snippets e.g.) you find more specialised related routines by others, mostly under the terms "introspection" or "introspective".
I don't think a link would be sufficient, and I will continue to prefer explaining answers if you kindly accept.
===Edit same day 13:53 UTC+02:00===
This may have been the function I had in mind years ago:

Function cellBackColor(pZ As Long, pCellName As String, Optional pDummy)
'pDummy shall only provide a way to trigger execution based on any reference or volatile function in Calc.
'pZ is the 1-based index iof the sheet as returned in Calc by the SHEET() function.
Dim theCell As Object, backColor As Long
cellBackColor = 4294967295
On Error GoTo errorExit
theCell       = ThisComponent.Sheets(pZ - 1).getCellRangeByName(pCellName)
backColor     = theCell.CellBackColor
cellBackColor = backColor
errorExit:
End Function

more