rubyaxlsx

Formatting a cell as Text using the axlsx spreadsheet ruby gem?


I'm using the axlsx ruby gem to create Excel-compatible .xlsx files. I can't figure out how to override the cell type that is generated by it's automatic type detection. For Active Record model attributes of type string the gem is setting the Excel cell format to General, but I want it to use Text explicitly. That way I can avoid stripping leading zeros off of zip codes, etc.

Anybody know how to accomplish this?


Solution

  • You can override the type of data using the types option on add row.

    Something like:

    worksheet.add_row ['0012342'], :types => [:string]
    

    Grab me on irc (JST) if you need any help getting that to work.

    Best

    randym

    edit --

    I've added an example for this to examples/example.rb in the repo.

    wb.add_worksheet(:name => "Override Data Type") do |sheet|
      sheet.add_row ['dont eat my zeros!', '0088'] , :types => [nil, :string]
    end
    

    https://github.com/randym/axlsx/blob/master/examples/example.rb#L349