How to read .xlsx file data with active storage
Asked Answered
V

2

5

I have this class and I am using active storage

class DataSheet < ApplicationRecord
  belongs_to :admin

  has_one_attached :excel_file
  after_save :create_user
end

I know there are plenty gems creek, write_xlsx_rails, roo to read or write .xlsx file but they are all requires a physical location of a file. previously, I used creek gem to read .xlsx file but I have to save that file locally like.

class LocalFileUploader


attr_reader :file

    def initialize(file = nil)
        @file = file
    end

    def save
        file_path = Rails.root.join('storage', file.original_filename)
        IO.copy_stream(file.path, file_path)
      return file_path.to_s
    end

end

Now I'm using active storage which store .xlsx file as binary. I couldn't able to find a way to read that file as xlsx file. Although Active storage provides download method to get binary data of a file.

pry(#<DataSheet>)> self.excel_file
=> #<ActiveStorage::Attached::One:0x007f9db0607750
 @dependent=:purge_later,
 @name="excel_file",
 @record=#<DataSheet:0x007f9db811ba40 id: 3, admin_id: 1, created_at: Mon, 28 May 2018 01:33:06 UTC +00:00, updated_at: Mon, 28 May 2018 01:33:06 UTC +00:00>>

[3] pry(#<DataSheet>)> self.excel_file.blob
=> #<ActiveStorage::Blob:0x007f9db81125f8
 id: 6,
 key: "TBitz1dEzma2R2uTgtEoJ7X1",
 filename: "faizabad 1424.xlsx",
 content_type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
 metadata: {"identified"=>true},
 byte_size: 374211,
 checksum: "/shAXf0kEnYV4DGl0z3fng==",
 created_at: Mon, 28 May 2018 01:33:06 UTC +00:00>

[4] pry(#<DataSheet>)> SimpleXlsxReader.open self.excel_file.blob
TypeError: no implicit conversion of ActiveStorage::Blob into String
from /home/rotlu_crush/.rvm/gems/ruby-2.4.1/gems/rubyzip-1.2.1/lib/zip/file.rb:73:in `size?'
[5] pry(#<DataSheet>)> SimpleXlsxReader.open self.excel_file.blob.download
  Disk Storage (0.6ms) Downloaded file from key: TBitz1dEzma2R2uTgtEoJ7X1
ArgumentError: string contains null byte
from /home/rotlu_crush/.rvm/gems/ruby-2.4.1/gems/rubyzip-1.2.1/lib/zip/file.rb:73:in `size?'

I read somwhere a example read csv file data with active storage by parsing that binary data into csv like.

CSV.parse(materials_upload.csv_file.download, headers: true) do |row|
  # ...
end

Is there any way to read excel file with active storage?.

Various answered 28/5, 2018 at 1:48 Comment(0)
H
6

Rails 6 will add ActiveStorage::Blob#open, which downloads the blob to a tempfile:

data_sheet.excel_file.open do |file|
  # Operate on the file
end

You can bundle Rails from GitHub to use this feature today:

gem "rails", github: "rails/rails"
Hermes answered 28/5, 2018 at 14:49 Comment(1)
Okay, but how to do in rails 5?Therianthropic
C
1

You can use gem xsv and then just call

x = Xsv::Workbook.open(blob.download)

sheet = x.sheets[0]

# Iterate over rows
sheet.each_row do |row|
  row # => ["header1", "header2"], etc.
end
Chery answered 21/5, 2020 at 15:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.