Spreadsheet reader fails to detect fill colour unless file is opened + saved in LibreOffice

177 Views Asked by At

I'm using the RubyXL gem to read background colours of cells of an xlsx spreadsheet generated in Excel. This works fine if the file is opened or saved in LibreOffice. The fill colours are all detected as white ('ffffff') on the first read if this is not done.

I have tried to use File.Open as an attempt to replicate the saving of the file before hand, but this was unsuccessful.

I access the background fill colour using cell && cell.fill_color within a loop of each cell in each row.

Currently I have tried to use RubyXL's workbook save to replicate the saving, as below.

book   = RubyXL::Parser.parse(path)
book.save
 book   = RubyXL::Parser.parse(path)
sheet  = book.worksheets[0]
sd     = sheet.sheet_data
i = 0 ; j = 0;
sheet.each { |row|
            row && row.cells.each { |cell|
                    val = cell && cell.value
                    j +=1
                    bg = cell && cell.fill_color
                    puts val.to_s + "|---|" + bg.to_s 
             }
            j = 0 
            i+=1
            }

Is there a way to re-saved the file silently (no GUI, no dialog boxes) programmatically?

1

There are 1 best solutions below

0
Alex Arthur On

Through more intense google-fu with some pointers from another forum I was able to find the soffice group of functions that allows file conversion through libreoffice.

I ran the following terminal command

soffice --headless --convert-to xlsx:"Calc MS Excel 2007 XML" file_path

which was successful in resolving the issue.