Class: ExcelParser::Sheet
- Inherits:
-
Object
- Object
- ExcelParser::Sheet
- Defined in:
- lib/excel_parser.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#workbook ⇒ Object
readonly
Returns the value of attribute workbook.
Class Method Summary collapse
- .column_name_to_index(name) ⇒ Object
-
.column_names ⇒ Object
Returns A to ZZZ.
Instance Method Summary collapse
-
#initialize(workbook, name, id, index) ⇒ Sheet
constructor
A new instance of Sheet.
- #parse_time(float) ⇒ Object
- #process_row(cell_map) ⇒ Object
- #row_count ⇒ Object
- #rows ⇒ Object
- #string_lookup(i) ⇒ Object
Constructor Details
#initialize(workbook, name, id, index) ⇒ Sheet
Returns a new instance of Sheet.
163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/excel_parser.rb', line 163 def initialize(workbook, name, id, index) @workbook = workbook @name = name @id = id @index = index begin @file = @workbook.zipfs.file.open(path) if @workbook.zipfs.file.exist?(path) rescue Zip::Error => e raise ExcelParser::Error, "Couldn't open sheet #{index}: #{e.}" end end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
162 163 164 |
# File 'lib/excel_parser.rb', line 162 def name @name end |
#workbook ⇒ Object (readonly)
Returns the value of attribute workbook.
162 163 164 |
# File 'lib/excel_parser.rb', line 162 def workbook @workbook end |
Class Method Details
.column_name_to_index(name) ⇒ Object
273 274 275 276 277 278 279 280 281 |
# File 'lib/excel_parser.rb', line 273 def self.column_name_to_index(name) if not @column_names_to_indices @column_names_to_indices = {} self.column_names.each_with_index do |name, i| @column_names_to_indices[name] = i end end @column_names_to_indices[name] end |
.column_names ⇒ Object
Returns A to ZZZ.
259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/excel_parser.rb', line 259 def self.column_names if @column_names @column_names else proc = Proc.new do |prev| ("#{prev}A".."#{prev}Z").to_a end x = proc.call("") y = x.map(&proc).flatten z = y.map(&proc).flatten @column_names = x + y + z end end |
Instance Method Details
#parse_time(float) ⇒ Object
236 237 238 239 240 241 |
# File 'lib/excel_parser.rb', line 236 def parse_time(float) hours = (float * 24).floor minutes = (float * 24 * 60).floor % 60 seconds = (float * 24 * 60 * 60).floor % 60 ExcelParser::Time.new(hours, minutes, seconds) end |
#process_row(cell_map) ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/excel_parser.rb', line 243 def process_row(cell_map) max = cell_map.keys.map {|c| self.class.column_name_to_index c }.max row = [] self.class.column_names[0..max].each do |col| if self.class.column_name_to_index(col) > max break else row << cell_map[col] end end row end |
#row_count ⇒ Object
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/excel_parser.rb', line 283 def row_count if defined? @row_count @row_count elsif @file @file.rewind Nokogiri::XML::Reader(@file).each do |node| if node.node_type == Nokogiri::XML::Reader::TYPE_ELEMENT case node.name when 'dimension' if ref = node.attributes["ref"] break @row_count = ref.scan(/\d+$/).first.to_i end when 'sheetData' break @row_count = nil end end end end end |
#rows ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/excel_parser.rb', line 179 def rows Enumerator.new(row_count) do |y| next unless @file @file.rewind shared = false row = nil cell_map = nil # Map of column letter to cell value for a row column = nil cell_type = nil Nokogiri::XML::Reader(@file).each do |node| case node.node_type when Nokogiri::XML::Reader::TYPE_ELEMENT case node.name when "row" cell_map = {} next when 'c' node_type = node.attributes['t'] node_style = node.attributes['s'] cell_index = node.attributes['r'] if !cell_index raise ExcelParser::Error, 'Invalid spreadsheet XML.' end column = cell_index.delete('0-9') cell_type = @workbook.attribute_to_type(node_type, node_style) shared = (node_type == 's') next end when Nokogiri::XML::Reader::TYPE_END_ELEMENT if node.name == 'row' y << process_row(cell_map) end next end if node.value value = case cell_type when :shared string_lookup(node.value.to_i) when :boolean node.value.to_i != 0 when :datetime, :date ExcelParser::OOXMLEpoch + node.value.to_f when :time parse_time(node.value.to_f) when :float node.value.to_f else # leave as string node.value end cell_map[column] = value end end end end |
#string_lookup(i) ⇒ Object
175 176 177 |
# File 'lib/excel_parser.rb', line 175 def string_lookup(i) @workbook.string_table[i] || (raise ExcelParser::Error, 'File invalid, invalid string table.') end |