Skip to content

support ruby2.x #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions lib/parseexcel/format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,16 @@ class Format
0x31 => "@",
}
begin
require 'iconv'
iconv = Iconv.new('utf16le', 'latin1')
a= "UTF-16LE"
b="UTF-8"
#b="ascii"
@@fmt_strs = @@fmt_strs.inject({}) { |memo, (key, val)|
memo.store(key, iconv.iconv(val))
memo.store(key, val.code_a2b(a,b))
memo
}

rescue
p $!
warn("default formats are encoded in ISO-8859-1")
end
attr_accessor :font_no, :fmt_idx, :lock, :hidden, :style, :key_123
Expand Down Expand Up @@ -109,7 +112,7 @@ def text_format(str, code=:_native_)
def to_s(target_encoding=nil)
fmt_str = @@fmt_strs[@fmt_idx].to_s
if(target_encoding)
Iconv.new(target_encoding, @encoding).iconv(fmt_str)
fmt_str.code_a2b(@encoding,target_encoding)
else
fmt_str.dup
end
Expand Down
1 change: 1 addition & 0 deletions lib/parseexcel/olestorage.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
# -*- coding: ascii-8bit -*-
#
# Spreadsheet::ParseExcel -- Extract Data from an Excel File
# Copyright (C) 2003 ywesee -- intellectual capital connected
Expand Down
2 changes: 1 addition & 1 deletion lib/parseexcel/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ def format(op, len, work) # DK:P336
idx = work[0,2].unpack('v').first
@workbook.add_text_format(idx, fmt)
end

def formula(op, len, work) # DK:P336
row, col, fmt = work.unpack('v3')
flag = work[12,2].unpack('v')
Expand Down
18 changes: 14 additions & 4 deletions lib/parseexcel/worksheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@

require 'parseexcel/olestorage'
require 'iconv'
class String
def code_a2b(a,b)
if RUBY_VERSION > '1.9' and defined? Encoding::Converter
tmp = Encoding::Converter.new(a,b, :universal_newline => true)
tmp.convert self
else
Iconv.conv("#{b}//IGNORE","#{a}//IGNORE",self)
end
end
end

module Spreadsheet
module ParseExcel
Expand Down Expand Up @@ -98,17 +108,17 @@ def to_f
def to_s(target_encoding=nil)
if(target_encoding)
begin
Iconv.new(target_encoding, @encoding).iconv(@value)
@value.code_a2b(@encoding, target_encoding)
rescue
Iconv.new(target_encoding, 'ascii').iconv(@value.to_s)
@value.code_a2b('ascii', target_encoding)
end
else
@value.to_s
end
end
def font
@book.font(@format.font_no)
end
end
def type
@format.cell_type(self) if @format
end
Expand Down Expand Up @@ -147,7 +157,7 @@ def each(skip=0, &block)
end
def name(target_encoding=nil)
if(target_encoding)
Iconv.new(target_encoding, 'UTF-16LE').iconv(@name.to_s)
@name.to_s.code_a2b('UTF-16LE',target_encoding)
else
@name
end
Expand Down