Class: WrapExcel::Book

Inherits:
Object
  • Object
show all
Defined in:
lib/wrap_excel/book.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, options = { }, &block) ⇒ Book

Returns a new instance of Book.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/wrap_excel/book.rb', line 13

def initialize(file, options={ }, &block)
  unless caller[1] =~ /book.rb:\d+:in\s+`open'$/
    warn "DEPRECATION WARNING: WrapExcel::Book.new and WrapExcel::Book.open will be split. If you open existing file, please use WrapExcel::Book.open.(call from #{caller[1]})"
  end

  @options = {
    :read_only => true,
    :displayalerts => false,
    :visible => false
  }.merge(options)
  @winapp = WIN32OLE.new('Excel.Application')
  @winapp.DisplayAlerts = @options[:displayalerts]
  @winapp.Visible = @options[:visible]
  WIN32OLE.const_load(@winapp, WrapExcel) unless WrapExcel.const_defined?(:CONSTANTS)
  @book = @winapp.Workbooks.Open(absolute_path(file),{ 'ReadOnly' => @options[:read_only] })

  if block
    begin
      yield self
    ensure
      close
    end
  end

  @book
end

Instance Attribute Details

#bookObject (readonly)

Returns the value of attribute book.



5
6
7
# File 'lib/wrap_excel/book.rb', line 5

def book
  @book
end

Class Method Details

.open(file, options = { }, &block) ⇒ Object



8
9
10
# File 'lib/wrap_excel/book.rb', line 8

def open(file, options={ }, &block)
  new(file, options, &block)
end

Instance Method Details

#[](sheet) ⇒ Object



61
62
63
64
# File 'lib/wrap_excel/book.rb', line 61

def [] sheet
  sheet += 1 if sheet.is_a? Numeric
  WrapExcel::Sheet.new(@book.Worksheets.Item(sheet))
end

#add_sheet(sheet = nil, options = { }) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/wrap_excel/book.rb', line 72

def add_sheet(sheet = nil, options = { })
  if sheet.is_a? Hash
    options = sheet
    sheet = nil
  end

  new_sheet_name = options.delete(:as)

  after_or_before, base_sheet = options.first || [:after, WrapExcel::Sheet.new(@book.Worksheets.Item(@book.Worksheets.Count))]
  base_sheet = base_sheet.sheet
  sheet ? sheet.Copy({ after_or_before => base_sheet }) : @book.WorkSheets.Add({ after_or_before => base_sheet })

  new_sheet = WrapExcel::Sheet.new(@winapp.Activesheet)
  new_sheet.name = new_sheet_name if new_sheet_name
  new_sheet
end

#closeObject



40
41
42
43
# File 'lib/wrap_excel/book.rb', line 40

def close
  @winapp.Workbooks.Close
  @winapp.Quit
end

#eachObject



66
67
68
69
70
# File 'lib/wrap_excel/book.rb', line 66

def each
  @book.Worksheets.each do |sheet|
    yield WrapExcel::Sheet.new(sheet)
  end
end

#save(file = nil) ⇒ Object

Raises:

  • (IOError)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/wrap_excel/book.rb', line 45

def save(file = nil)
  raise IOError, "Not opened for writing(open with :read_only option)" if @options[:read_only]
  return @book.save unless file

  dirname, basename = File.split(file)
  extname = File.extname(basename)
  basename = File.basename(basename)
  case extname
  when '.xls'
    file_format = WrapExcel::XlExcel8
  when '.xlsx'
    file_format = WrapExcel::XlOpenXMLWorkbook
  end
  @book.SaveAs(absolute_path(File.join(dirname, basename)), file_format)
end