Class: Roo::Excel
- Inherits:
-
Base
- Object
- Base
- Roo::Excel
- Defined in:
- lib/roo/xls/excel.rb
Overview
Class for handling Excel-Spreadsheets
Defined Under Namespace
Modules: ExcelFontExtensions
Constant Summary collapse
- FORMULAS_MESSAGE =
'the spreadsheet gem does not support forumulas, so roo can not.'- CHARGUESS =
begin require 'charguess' true rescue LoadError false end
Instance Attribute Summary collapse
-
#workbook ⇒ Object
readonly
Returns the value of attribute workbook.
Instance Method Summary collapse
-
#cell(row, col, sheet = default_sheet) ⇒ Object
returns the content of a cell.
-
#celltype(row, col, sheet = default_sheet) ⇒ Object
returns the type of a cell: * :float * :string, * :date * :percentage * :formula * :time * :datetime.
- #encoding=(codepage) ⇒ Object
-
#font(row, col, sheet = default_sheet) ⇒ Object
Given a cell, return the cell’s font.
-
#formula(_row, _col, _sheet = nil) ⇒ Object
(also: #formula?)
returns NO formula in excel spreadsheets.
-
#formulas(_sheet = nil) ⇒ Object
returns NO formulas in excel spreadsheets.
-
#initialize(filename, options = {}) ⇒ Excel
constructor
Creates a new Excel spreadsheet object.
-
#longest_sheet ⇒ Object
this method lets you find the worksheet with the most data.
-
#sheets ⇒ Object
returns an array of sheet names in the spreadsheet.
-
#to_s(sheet = default_sheet) ⇒ Object
shows the internal representation of all cells mainly for debugging purposes.
- #worksheets ⇒ Object
Constructor Details
#initialize(filename, options = {}) ⇒ Excel
Creates a new Excel spreadsheet object. Parameter packed: :zip - File is a zip-file
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/roo/xls/excel.rb', line 21 def initialize(filename, = {}) packed = [:packed] file_warning = [:file_warning] || :error mode = [:mode] || 'rb+' file_type_check(filename, '.xls', 'an Excel', file_warning, packed) make_tmpdir do |tmpdir| filename = download_uri(filename, tmpdir) if uri?(filename) filename = open_from_stream(filename[7..-1], tmpdir) if filename.is_a?(::String) && filename[0, 7] == 'stream:' filename = unzip(filename, tmpdir) if packed == :zip @filename = filename unless File.file?(@filename) fail IOError, "file #{@filename} does not exist" end @workbook = ::Spreadsheet.open(filename, mode) end super(filename, ) @formula = {} @fonts = {} end |
Instance Attribute Details
#workbook ⇒ Object (readonly)
Returns the value of attribute workbook.
17 18 19 |
# File 'lib/roo/xls/excel.rb', line 17 def workbook @workbook end |
Instance Method Details
#cell(row, col, sheet = default_sheet) ⇒ Object
returns the content of a cell. The upper left corner is (1,1) or (‘A’,1)
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/roo/xls/excel.rb', line 66 def cell(row, col, sheet = default_sheet) validate_sheet!(sheet) read_cells(sheet) fail 'should be read' unless @cells_read[sheet] row, col = normalize(row, col) if celltype(row, col, sheet) == :date yyyy, mm, dd = @cell[sheet][[row, col]].split('-') return Date.new(yyyy.to_i, mm.to_i, dd.to_i) end if celltype(row, col, sheet) == :string return platform_specific_encoding(@cell[sheet][[row, col]]) else if @cell[sheet] && @cell[sheet][[row, col]] return @cell[sheet][[row, col]] else return nil end end end |
#celltype(row, col, sheet = default_sheet) ⇒ Object
returns the type of a cell:
-
:float
-
:string,
-
:date
-
:percentage
-
:formula
-
:time
-
:datetime
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/roo/xls/excel.rb', line 95 def celltype(row, col, sheet = default_sheet) read_cells(sheet) row, col = normalize(row, col) begin if @formula[sheet] && @formula[sheet][[row, col]] :formula elsif @cell_type[sheet] @cell_type[sheet][[row, col]] end rescue puts "Error in sheet #{sheet}, row #{row}, col #{col}" raise end end |
#encoding=(codepage) ⇒ Object
49 50 51 |
# File 'lib/roo/xls/excel.rb', line 49 def encoding=(codepage) @workbook.encoding = codepage end |
#font(row, col, sheet = default_sheet) ⇒ Object
Given a cell, return the cell’s font
122 123 124 125 126 |
# File 'lib/roo/xls/excel.rb', line 122 def font(row, col, sheet = default_sheet) read_cells(sheet) row, col = normalize(row, col) @fonts[sheet][[row, col]] end |
#formula(_row, _col, _sheet = nil) ⇒ Object Also known as: formula?
returns NO formula in excel spreadsheets
111 112 113 |
# File 'lib/roo/xls/excel.rb', line 111 def formula(_row, _col, _sheet = nil) fail NotImplementedError, FORMULAS_MESSAGE end |
#formulas(_sheet = nil) ⇒ Object
returns NO formulas in excel spreadsheets
117 118 119 |
# File 'lib/roo/xls/excel.rb', line 117 def formulas(_sheet = nil) fail NotImplementedError, FORMULAS_MESSAGE end |
#longest_sheet ⇒ Object
this method lets you find the worksheet with the most data
59 60 61 62 63 |
# File 'lib/roo/xls/excel.rb', line 59 def longest_sheet sheet(worksheets.inject do|m, o| o.row_count > m.row_count ? o : m end.name) end |
#sheets ⇒ Object
returns an array of sheet names in the spreadsheet
54 55 56 |
# File 'lib/roo/xls/excel.rb', line 54 def sheets @sheets ||= worksheets.collect { |worksheet| normalize_string(worksheet.name) } end |
#to_s(sheet = default_sheet) ⇒ Object
shows the internal representation of all cells mainly for debugging purposes
130 131 132 133 |
# File 'lib/roo/xls/excel.rb', line 130 def to_s(sheet = default_sheet) read_cells(sheet) @cell[sheet].inspect end |
#worksheets ⇒ Object
45 46 47 |
# File 'lib/roo/xls/excel.rb', line 45 def worksheets @worksheets ||= workbook.worksheets end |