Class: Applocale::ParseXLSX

Inherits:
Object
  • Object
show all
Defined in:
lib/applocale/Core/ParseXLSX/parse_xlsx.rb

Instance Method Summary collapse

Constructor Details

#initialize(platfrom, xlsxpath, langlist, sheetobj_list) ⇒ ParseXLSX

Returns a new instance of ParseXLSX.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/applocale/Core/ParseXLSX/parse_xlsx.rb', line 27

def initialize(platfrom, xlsxpath, langlist, sheetobj_list)
  @platform = platfrom
  @xlsxpath = xlsxpath
  @langlist = langlist
  @sheetobj_list = sheetobj_list
  puts "Start to Parse XLSX: \"#{@xlsxpath}\" ...".green
  @sheetcontent_list = Array.new
  @allkey_dict = {}
  @all_error = Array.new
  self.parse
end

Instance Method Details

#convert_contect(cell_value) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/applocale/Core/ParseXLSX/parse_xlsx.rb', line 154

def convert_contect(cell_value)
  if cell_value.nil?
    return ''
  else
    return ContentUtil.from_excel(cell_value)
  end
end

#find_header(sheetinfoobj, cells) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/applocale/Core/ParseXLSX/parse_xlsx.rb', line 162

def find_header(sheetinfoobj, cells)
  keystr_with_colno = nil
  lang_with_colno_list = Array.new
  k_header_lang_dict = []
  colno = 1
  cells.each{ |cell|
    value = cell && cell.value
    unless value.nil?
      if value == sheetinfoobj.key_header && keystr_with_colno.nil?
        keystr_with_colno = ParseModelModule::KeyStrWithColNo.new(value, colno)
      else
        sheetinfoobj.lang_headers.each do |lang, keyforlang|
          if value == keyforlang && k_header_lang_dict.index(lang).nil?
            lang_with_colno_list.push(ParseModelModule::LangWithColNo.new(keyforlang, lang, colno))
            k_header_lang_dict.push(lang)
          end
        end
      end
    end
    colno += 1
  }
  if !keystr_with_colno.nil? && lang_with_colno_list.length == sheetinfoobj.lang_headers.length
    return {:keystr_colno => keystr_with_colno, :lang_colno => lang_with_colno_list}
  end
end

#parseObject



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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/applocale/Core/ParseXLSX/parse_xlsx.rb', line 43

def parse
  begin
    workbook = RubyXL::Parser.parse(@xlsxpath)
  rescue
    ErrorUtil::CannotOpenXlsxFile.new(@xlsxpath).raise
  end
  sheetnamelist = Applocale::Config::Sheet.get_sheetlist(@sheetobj_list)
  worksheets = workbook.worksheets
  sorted_worksheets = sheetnamelist
    .map { |sheet_name| worksheets.find { |worksheet| worksheet.sheet_name == sheet_name } }
    .compact

  sorted_worksheets.each do |worksheet|
    sheetname = worksheet.sheet_name
    sheetinfoobj = Applocale::Config::Sheet.get_sheetobj_by_sheetname(@sheetobj_list, sheetname)
    if sheetinfoobj.nil?
      next
    end
    sheetnamelist.delete(sheetname)

    sheetcontent = ParseModelModule::SheetContent.new(sheetname)
    if sheetinfoobj.is_a? Applocale::Config::SheetInfoByRow
      keycolno = Applocale::ParseXLSXModule::Helper.collabel_to_colno(sheetinfoobj.key_col)
      sheetinfoobj.to_keyStrWithColNo(sheetcontent)
    end

    rowno = 0
    worksheet.each {|row|
      rowno += 1
      # colno = 0
      if sheetcontent.header_rowno.nil?
        if sheetinfoobj.is_a? Applocale::Config::SheetInfoByHeader
          next if row.nil?
          cells = row && row.cells
          headerinfo = find_header(sheetinfoobj, cells)
          unless headerinfo.nil?
            sheetcontent.header_rowno = rowno
            sheetcontent.keyStr_with_colno = headerinfo[:keystr_colno]
            sheetcontent.lang_with_colno_list = headerinfo[:lang_colno]
          end
        end
      elsif sheetcontent.header_rowno > rowno
        next
      else
        next if row.nil?
        cells = row && row.cells
        begin
          rowcontent = parse_row(sheetname, rowno, cells, sheetcontent.keyStr_with_colno, sheetcontent.lang_with_colno_list)
          unless rowcontent.nil?
            prev_rowcontent = @allkey_dict[rowcontent.key_str.downcase]
            if prev_rowcontent.nil?
              @allkey_dict[rowcontent.key_str.downcase] = rowcontent
              sheetcontent.rowinfo_list.push(rowcontent)
            else
              error = ErrorUtil::ParseXlsxError::DuplicateKey.new(rowcontent, prev_rowcontent.sheetname, prev_rowcontent.rowno)
              @all_error.push(error)
            end
          end
        rescue ErrorUtil::ParseXlsxError::ParseError => e
          @all_error.push(e)

        end
      end
    }
    if sheetcontent.header_rowno.nil?
      ErrorUtil::ParseXlsxError::HeadeNotFound.new(sheetname).to_warn
    end
    @sheetcontent_list.push(sheetcontent)
  end
  if @all_error.length > 0
    ErrorUtil::ParseXlsxError::ParseError.raiseArr(@all_error)
  end

end

#parse_row(sheetname, rowno, cells, keystr_with_colno, lang_with_colno_list) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/applocale/Core/ParseXLSX/parse_xlsx.rb', line 118

def parse_row(sheetname, rowno, cells, keystr_with_colno, lang_with_colno_list)
  begin
    cell = cells[keystr_with_colno.colno - 1 ]
    val = cell && cell.value
    keystr = to_value_key(val)
  rescue ErrorUtil::ParseXlsxError::InValidKey => e
    e.rowinfo.sheetname = sheetname
    e.rowinfo.rowno = rowno
    raise e
  end

  unless keystr.nil?
    rowinfo = ParseModelModule::RowInfo.new(sheetname, rowno, keystr)
    lang_with_colno_list.each do |lang_with_colno|
      cell = cells[lang_with_colno.colno - 1]
      val = cell && cell.value
      cell_value = val.to_s
      lang_name = lang_with_colno.lang
      rowinfo.content_dict[lang_name] = convert_contect(cell_value)
    end
    return rowinfo
  end
end

#resultObject



39
40
41
# File 'lib/applocale/Core/ParseXLSX/parse_xlsx.rb', line 39

def result
  return @sheetcontent_list
end

#to_value_key(value) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/applocale/Core/ParseXLSX/parse_xlsx.rb', line 142

def to_value_key(value)
  if !value.nil? && value != ''
    new_value = value.to_s
    if ValidKey.is_validkey(@platform, new_value)
      return new_value
    else
      rowinfo = ParseModelModule::RowInfo.new(nil, nil, value)
      raise ErrorUtil::ParseXlsxError::InValidKey.new(rowinfo)
    end
  end
end