class SuperSpreadsheet::Loader
Attributes
file_path[RW]
Public Class Methods
load!(file)
click to toggle source
# File lib/super_spreadsheet/loader.rb, line 10 def self.load!(file) new(file) end
new(file_path)
click to toggle source
# File lib/super_spreadsheet/loader.rb, line 14 def initialize(file_path) @file_path = file_path end
Public Instance Methods
extension()
click to toggle source
# File lib/super_spreadsheet/loader.rb, line 22 def extension ::File.extname(file_path).split('.').last.force_encoding('utf-8').downcase end
rows()
click to toggle source
# File lib/super_spreadsheet/loader.rb, line 34 def rows @rows ||= rows! end
valid_format()
click to toggle source
# File lib/super_spreadsheet/loader.rb, line 26 def valid_format if rows == false errors.add(:file, '檔案格式錯誤') elsif rows.flatten.empty? errors.add(:file, '檔案內容錯誤,空白檔案') end end
Protected Instance Methods
csv_content()
click to toggle source
# File lib/super_spreadsheet/loader.rb, line 67 def csv_content @csv_content ||= File.read(file_path) end
csv_content!()
click to toggle source
# File lib/super_spreadsheet/loader.rb, line 55 def csv_content! @decode_csv_content ||= begin csv_content_big5! rescue Iconv::IllegalSequence begin csv_content_big5_hkscs! rescue Iconv::IllegalSequence csv_content end end end
rows!()
click to toggle source
# File lib/super_spreadsheet/loader.rb, line 40 def rows! case extension when 'xls' ::Roo::Excel.new(file_path).map { |row| convert_float_to_integer(row) } when 'xlsx' ::Roo::Excelx.new(file_path).map { |row| convert_float_to_integer(row) } when 'csv' ::CSV.parse(csv_content!) else false end rescue false end
Private Instance Methods
convert_float_to_integer(row)
click to toggle source
# File lib/super_spreadsheet/loader.rb, line 81 def convert_float_to_integer(row) row.map do |cell| if cell.is_a?(Float) && cell = cell.to_i cell.to_i else cell end end end
csv_content_big5!()
click to toggle source
# File lib/super_spreadsheet/loader.rb, line 73 def csv_content_big5! Iconv.new("utf-8", "Big5//TRANSLIT//IGNORE").iconv(csv_content) end
csv_content_big5_hkscs!()
click to toggle source
# File lib/super_spreadsheet/loader.rb, line 77 def csv_content_big5_hkscs! Iconv.new("utf-8", "Big5-HKSCS//TRANSLIT//IGNORE").iconv(csv_content) end