zdavatz / spreadsheet

The Ruby Spreadsheet by ywesee GmbH
http://spreadsheet.ch
GNU General Public License v3.0
1.13k stars 240 forks source link

Ruby 2.3 support? #198

Closed LimeBlast closed 6 years ago

LimeBlast commented 7 years ago

I'm using the follow code to generate a spreadsheet, but the resulting file is coming out corrupted (see attached file). I'm wondering if this might be an issue with ruby 2.3?

require 'spreadsheet'

class LettsExportSpreadsheet

  HEADERS = {
    0 => 'Order Date',
    1 => 'Order No',
    2 => 'BrNo',
    3 => 'Branch',
    4 => 'Gideon Code',
    5 => 'Letts Code',
    6 => 'Description',
    7 => 'Qty',
    8 => 'DeliveryID',
    9 => 'Contact',
    10 => 'Add1',
    11 => 'Add2',
    12 => 'Add3',
    13 => 'Town',
    14 => 'County',
    15 => 'PostCode',
    16 => 'TelNo',
    17 => 'Notes',
    18 => 'Batch-Count',
    19 => 'DeliveryDate',
  }

  def initialize(supplier_export)
    @supplier_export = supplier_export
  end

  def generate(order_items)
    work_book = Spreadsheet::Workbook.new
    write_sheet = work_book.create_worksheet :name => 'Orders_Letts'
    write_sheet.row(0).replace headers.values

    if order_items.empty?
      write_sheet.row(1).replace(['No items'])
    else
      order_items.each_with_index do |item, i|
        row_number = i + 1

        write_sheet.row(row_number).replace [
          item.order.created_at.to_date.to_s(:en),
          item.order.id,
          item.order.branch_id,
          item.order.branch.name,
          item.product.id,
          item.product.supplier_item_code,
          item.product.name,
          item.amount,
          "#{item.order.branch_id}#{item.order.user.initials}", # deliveryID ???
          item.order.recipient_contact_name,
          item.order.shipping_line_1,
          item.order.shipping_line_2,
          nil, # no line 3
          item.order.shipping_city,
          item.order.shipping_county,
          item.order.shipping_postcode,
          item.order.recipient.default_phone,
          item_note(item),
          supplier_export.id,
          supplier_export.created_at.to_date + 1.week, # Monday next week.
        ]
      end
    end

    virtual_file = NamedStringIO.new('Orders_Letts.xls')
    work_book.write virtual_file

    virtual_file
  end

  private
  attr_reader :supplier_export

  def headers
    if supplier_export.badged?
      HEADERS.merge({17 => 'Corporate Over Blocking Edition'})
    else
      HEADERS
    end
  end

  def item_note(item)
    if supplier_export.badged?
      item.badge_name
    else
      ""
    end
  end
end

Orders_Letts.xls.zip

We're running Rails 3.2 (recently updated from 2.3)

zdavatz commented 7 years ago

This may be a raily issue. Can you support me with pure Ruby code so I can test it?

zdavatz commented 7 years ago

Above code runs on Ruby 2.4.0 but does not create any output file.

lmansur commented 7 years ago

I've been using Spreadsheet v1.0.7 with Ruby v2.3.4p301 and Rails 3.2.22.2 without any noticeable problem.