Class: File_util

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods-aqara-localzedLoader/File_util.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#keys_hashObject

Returns the value of attribute keys_hash.



10
11
12
# File 'lib/cocoapods-aqara-localzedLoader/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 对齐快捷键



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/cocoapods-aqara-localzedLoader/File_util.rb', line 72

def getkeyHash(row1,ios:true)
  keys_hash = {}
  key = ''
  row1.cells.each_with_index do |cell, i|
    next unless cell.value && i > 3
    if cell.value === "zh-CN"
      key = 'zh-Hans'
    elsif cell.value == "zh-TW"
      key = 'zh-Hant-TW'
    elsif cell.value == "zh-HK"
      key = 'zh-HK'
    else
      key = cell.value.split("-")[0]
    end
    # result = cell.value.scan(/.*?\[(.*)\]/)
    keys_hash[i] = key
  end
  keys_hash
end

#getLangListObject



92
93
94
95
96
# File 'lib/cocoapods-aqara-localzedLoader/File_util.rb', line 92

def getLangList
  list = @key_hash.values
  list.delete_if{|item| item.downcase === "keys"||item.downcase === "source" || item.downcase === "length limit" || item.downcase === "context"}
  list
end

#read_excel(filename, crowdin = false) ⇒ 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cocoapods-aqara-localzedLoader/File_util.rb', line 12

def read_excel(filename, crowdin = false)
  unless filename != nil
    puts "读取的excel为空"
    return
  end

  workbook = RubyXL::Parser.parse filename
  worksheet = workbook[0]

  if crowdin
    first_row = worksheet[0]
    zh_cn_col = first_row.cells.index { |c| c && c.value == 'zh-CN' }

    if zh_cn_col
      worksheet.each do |row|
        row&.cells&.delete_at(zh_cn_col)
      end
    end

    last_col_index = worksheet[0].cells.size # 当前最后一列索引+1

    # 遍历 B 列(索引 1)所有行
    worksheet.each_with_index do |row, row_index|
      b_value = row && row[1] ? row[1].value : nil
      worksheet.add_cell(row_index, last_col_index, b_value)
    end

    worksheet[0].cells[last_col_index] = worksheet.add_cell(0, last_col_index, 'zh-CN')
    workbook.write(filename)
  end

  row1 = worksheet[0]
  @key_hash = getkeyHash(row1)
  lang_hash = {}
  worksheet.each_with_index do |row,rowIndex|
    next unless rowIndex>0
    ele = StringElement.new
    self_key = ''
    # 设置StringElement的值
    row.cells.each_with_index do |cell, i|
      next unless cell.value
      case i
      when 0
        self_key = cell.value
      else
        key = @key_hash[i]
        if key
          ele.langHash[key] = (cell.value || '')
        end
      end
    end

    lang_hash[self_key] = ele
  end
  lang_hash
end