0

So I am using this gem axlsx_rails for exporting some data in excel file. But sometimes I get error like forbidden character as name or name exceed 31 character size. I don't understand how to handle these errors. All I want is to render index again if such error occurs and show a flash message. My current code is below

Class CustomersController
def index
    @customers = Customer.all
    respond_to do |format|
      format.html # index.html.erb
      format.xls
      format.xlsx
    end
  end
end

and index.xlsx.axlsx

 wb = xlsx_package.workbook
    @customers = Customer.all
    @customers.each do |customer|
        wb.add_worksheet(name: customer.name) do |sheet|
        sheet.add_row ["Name", ...... ]
        .
        .
        .
   end
asdfkjasdfjk
  • 3,784
  • 18
  • 64
  • 104

1 Answers1

0

It has to do with customer.name and your sheet name. It is limited to 31 characters, and occasionally you have customer names that are longer. Use customer.name[0,31] or some other method to ensure length.

The spreadsheet spec also limits what characters can go in a sheet name. I've not been able to find a specific example this morning. But if you strip the name to a-zA-Z1-9 and - you should get by.

noel
  • 2,095
  • 14
  • 14