Monday, November 18, 2013

Setting cell color using Ruby spreadsheet GEM

I've recently been working on a Rails project and needed to output some data in spreadsheet format.  My client wanted some simple styling of the spreadsheet but the standard colors didn't seem to fit the bill.  That's when I discovered that there are other options besides :red, :blue, :green, :cyan, etc. for the background style on a spreadsheet cell using the spreadsheet GEM.   Although I couldn't find any reference to these additional colors it is obvious from looking through the source code for the GEM that they exist.  Unfortunately, the symbol names used to represent these "additional" colors didn't lend themselves very well to visualize what color they represented.  They are :xls_color_0, :xls_color_1, etc...  and you can see them by looking at the source code here.

So I decided to output a quick spreadsheet with cell text representing the symbol name and then formatting the cell background with the color.   This makes a handy reference for those colors as well as providing some examples of how to format a cell.  Here's the source code:

The Gist is here.

The program  will output a file colors.xls that looks like the following:


No comments:

Post a Comment