Class: Princess::ExcelxReader
- Inherits:
-
Object
- Object
- Princess::ExcelxReader
- Defined in:
- lib/princess/excelx_reader.rb
Instance Method Summary collapse
-
#active_sheet(sheet_num) ⇒ Object
load a sheet for reading.
-
#cell(coord) ⇒ Object
return the specified cell content.
-
#each ⇒ Object
iterate through the spreadsheet rows.
-
#initialize(filename, default_sheet = 0) ⇒ ExcelxReader
constructor
A new instance of ExcelxReader.
-
#row(num) ⇒ Object
return the specified row.
Constructor Details
#initialize(filename, default_sheet = 0) ⇒ ExcelxReader
Returns a new instance of ExcelxReader.
4 5 6 7 |
# File 'lib/princess/excelx_reader.rb', line 4 def initialize(filename, default_sheet = 0) @filename = filename active_sheet(default_sheet) end |
Instance Method Details
#active_sheet(sheet_num) ⇒ Object
load a sheet for reading
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/princess/excelx_reader.rb', line 10 def active_sheet(sheet_num) raise 'invalid argument' if !sheet_num.is_a?(Integer) Zip::ZipFile.open(@filename) do |zipfile| # grab shared strings noko_shared_strings = Nokogiri::XML(zipfile.read('xl/sharedStrings.xml')) noko_shared_strings.remove_namespaces! @shared_strings = noko_shared_strings.xpath('//sst/si/t').map(&:content) # grab spreadsheet content doc = Nokogiri::XML(zipfile.read("xl/worksheets/sheet#{sheet_num + 1}.xml")) doc.remove_namespaces! ref = doc.xpath('//dimension').first.attr('ref') @rows = Array.new(ref.split(':').last.scan(/\d+/).first.to_i) doc.xpath('//sheetData/row').each do |row| row_num = row.attr('r').to_i - 1 @rows[row_num] = row end end end |
#cell(coord) ⇒ Object
return the specified cell content
52 53 54 |
# File 'lib/princess/excelx_reader.rb', line 52 def cell(coord) row(coord.scan(/\d+/).first.to_i).cell(coord.scan(/\D+/).first) end |