Class: Rasta::Spreadsheet::Book

Inherits:
Object
  • Object
show all
Defined in:
lib/rasta/spreadsheet.rb

Overview

This object is a container for the Excel workbook and any options passed into the library.

The book object can be parsed to find Records which in turn can be parsed to locate Cells.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Book

Returns a new instance of Book.

Raises:

  • (ArgumentError)


160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/rasta/spreadsheet.rb', line 160

def initialize(filename)
  @excel = Excel.instance
  @filename = filename
  raise ArgumentError, 'XLS file required for Book.new()' if !@filename
  raise IOError, "Unable to find file: '#{@filename}'" if ! File.file?(@filename)
  
  # Create a bookmark
  @bookmark = Bookmark.new(@excel.continue)
  
  # Open the workbook and get the ole reference to the workbook object
  @o = @excel.open(@filename)
end

Instance Attribute Details

#bookmarkObject

Returns the value of attribute bookmark.



158
159
160
# File 'lib/rasta/spreadsheet.rb', line 158

def bookmark
  @bookmark
end

#filenameObject (readonly)

Returns the value of attribute filename.



157
158
159
# File 'lib/rasta/spreadsheet.rb', line 157

def filename
  @filename
end

Instance Method Details

#[](name) ⇒ Object



173
174
175
176
177
178
179
180
# File 'lib/rasta/spreadsheet.rb', line 173

def [](name)
  worksheet = @o.Worksheets(name)
  if valid_worksheet(worksheet)
    Sheet.new(self, worksheet)
  else
    raise SheetNotFound, name
  end
end

#closeObject



208
209
210
# File 'lib/rasta/spreadsheet.rb', line 208

def close
   @o.Close(0) if @excel
end

#eachObject



182
183
184
185
186
187
188
189
190
# File 'lib/rasta/spreadsheet.rb', line 182

def each
  @o.Worksheets.each do |worksheet| 
    next if !valid_worksheet(worksheet)
    Excel.instance.currentpage += 1
    return if (@excel.pagecount > 0 && @excel.currentpage > @excel.pagecount)
    return if (@excel.recordcount > 0 && @excel.currentrecord > @excel.recordcount)
    yield Sheet.new(self, worksheet)
  end
end

#ole_objectObject



200
201
202
# File 'lib/rasta/spreadsheet.rb', line 200

def ole_object 
  @o
end

#saveObject



204
205
206
# File 'lib/rasta/spreadsheet.rb', line 204

def save
   @o.Save if @excel
end

#valid_worksheet(worksheet) ⇒ Object



192
193
194
195
196
197
198
# File 'lib/rasta/spreadsheet.rb', line 192

def valid_worksheet(worksheet)
  return false if worksheet.Visible == 0                                      # Skip hidden worksheets
  return false if worksheet.name =~ /^#/                                      # Skip commented sheets
  return false if worksheet.Tab.ColorIndex != ExcelConst::XlColorIndexNone    # Skip worksheets with colored tabs 
  return false if (@excel.continue && @bookmark.do_not_execute?(:sheet, worksheet.name))
  return true
end