Class: OfficeCSV

Inherits:
Object
  • Object
show all
Defined in:
lib/office_csv.rb

Constant Summary collapse

BOM =
[0xff, 0xfe].pack('C*').force_encoding('UTF-16LE')

Class Method Summary collapse

Class Method Details

.generate(&block) ⇒ Object

Works just like CSV.generate. It adds a byte order mark to the beginning of the document and converts the contents from UTF-8 to UTF-16LE.



9
10
11
12
13
# File 'lib/office_csv.rb', line 9

def self.generate(&block)
  BOM + Iconv.iconv('UTF-16LE', 'UTF-8',
    CSV.generate(:col_sep => "\t", &block)
  ).join
end

.parse(csv) ⇒ Object

Just like CSV.parse. It removes the byte order mark and transcodes from UTF-16LE to UTF-8.



16
17
18
# File 'lib/office_csv.rb', line 16

def self.parse(csv)
  CSV.parse(Iconv.iconv('UTF-8', 'UTF-16LE', csv).first[1..-1], :col_sep => "\t")
end