From 7e4ecdc247a519f61241cf9efce0889ebbaab04c Mon Sep 17 00:00:00 2001 From: sevk Date: Tue, 21 Jul 2020 09:28:06 +0800 Subject: [PATCH] support ruby2.x --- lib/parseexcel/format.rb | 11 +++++++---- lib/parseexcel/olestorage.rb | 1 + lib/parseexcel/parser.rb | 2 +- lib/parseexcel/worksheet.rb | 18 ++++++++++++++---- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/lib/parseexcel/format.rb b/lib/parseexcel/format.rb index 5beb2c0..244fad0 100644 --- a/lib/parseexcel/format.rb +++ b/lib/parseexcel/format.rb @@ -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 @@ -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 diff --git a/lib/parseexcel/olestorage.rb b/lib/parseexcel/olestorage.rb index f797247..989ea52 100644 --- a/lib/parseexcel/olestorage.rb +++ b/lib/parseexcel/olestorage.rb @@ -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 diff --git a/lib/parseexcel/parser.rb b/lib/parseexcel/parser.rb index 27d0c2c..980331f 100644 --- a/lib/parseexcel/parser.rb +++ b/lib/parseexcel/parser.rb @@ -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') diff --git a/lib/parseexcel/worksheet.rb b/lib/parseexcel/worksheet.rb index c02d59a..97d0c98 100644 --- a/lib/parseexcel/worksheet.rb +++ b/lib/parseexcel/worksheet.rb @@ -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 @@ -98,9 +108,9 @@ 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 @@ -108,7 +118,7 @@ def to_s(target_encoding=nil) end def font @book.font(@format.font_no) - end + end def type @format.cell_type(self) if @format end @@ -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