Class: File_util
- Inherits:
-
Object
- Object
- File_util
- Defined in:
- lib/File_util.rb
Instance Attribute Summary collapse
-
#keys_hash ⇒ Object
Returns the value of attribute keys_hash.
Instance Method Summary collapse
-
#getkeyHash(row1, ios: true) ⇒ Object
根据第一行来获取当前cell的key return 1:selfkey,2:en,3:zh1:selfkey,2:en,3:zh.… 方便excel取 Command+Option+L 对齐快捷键.
- #getLangList ⇒ Object
-
#read_excel(filename) ⇒ Object
读取excel 返回StringElement数组.
Instance Attribute Details
#keys_hash ⇒ Object
Returns the value of attribute keys_hash.
10 11 12 |
# File 'lib/File_util.rb', line 10 def keys_hash @keys_hash end |
Instance Method Details
#getkeyHash(row1, ios: true) ⇒ Object
根据第一行来获取当前cell的key return File_util.1:selfkey,2:en,3:zh1:selfkey,2:en,3:zh.… 方便excel取Command+Option+L 对齐快捷键
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/File_util.rb', line 49 def getkeyHash(row1,ios:true) keys_hash = {} row1.cells.each_with_index do |cell, i| next unless cell if i > 1 result = cell.value.scan(/.*?\[(.*)\]/) key = result[0][0] case key when /zh_HK/ key="zh-HK" when /zh_TW/ key ="zh-Hant-TW" when /zh/ key ="zh-Hans" when /fr.*/ key ="fr" when /it.*/ key="it" when /ger.*/ key ="de" else key end keys_hash[i] = key elsif i == 1 keys_hash[i] = cell.value end end keys_hash end |
#getLangList ⇒ Object
80 81 82 83 84 |
# File 'lib/File_util.rb', line 80 def getLangList list = @key_hash.values list.delete_if{|item| item.downcase === "selfkey"} list end |
#read_excel(filename) ⇒ Object
读取excel 返回StringElement数组
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/File_util.rb', line 12 def read_excel(filename) unless filename != nil puts "读取的excel为空" return end workbook = RubyXL::Parser.parse filename worksheet = workbook[0] row1 = worksheet[0] @key_hash = getkeyHash(row1) lang_hash = {} worksheet.each_with_index do |row| ele = StringElement.new self_key = '' # 设置StringElement的值 row.cells.each_with_index do |cell, i| next unless cell case i when 0 when 1 self_key = cell.value else key = @key_hash[i] ele.langHash[key] = (cell.value || '') end end lang_hash[self_key] = ele end lang_hash end |