Formatting a cell as text using rubs gs?

I use axlsx ruby ​​gem to create Excel .xlsx compatible files. I cannot figure out how to override the type of cell that is generated by automatic type detection. For string type Active Record model attributes, the gem sets the Excel cell format to General, but I want it to use text explicitly. This way, I can avoid removing the top zeros from zip codes, etc.

Does anyone know how to do this?

+6
source share
2 answers

You can override the data type using the types option in the add line.

Sort of:

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

Take me to irc (JST) if you need help getting this to work.

Best

randym

change -

I added an example for the /example.rb example 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

+26
source

format_code: '@' will work for you. Below is the code for reference.

 def default_data_type_as_string @xlsx_package = Axlsx::Package.new @workbook = @xlsx_package.workbook @worksheet = @workbook.add_worksheet(:name => "Introduction") default_style = @workbook.styles.add_style({ format_code: '@' }) row_data_array = ['1', '2%', '3$'] @worksheet.add_row row_data_array, :style => [nil, default_style, nil] @xlsx_package.serialize('default_data_type_as_string.xlsx') end 
+3
source

All Articles