Class: WrapExcel::Book
- Inherits:
-
Object
- Object
- WrapExcel::Book
- Defined in:
- lib/wrap_excel/book.rb
Instance Attribute Summary collapse
-
#book ⇒ Object
readonly
Returns the value of attribute book.
Class Method Summary collapse
Instance Method Summary collapse
- #[](sheet) ⇒ Object
- #add_sheet(sheet = nil, options = { }) ⇒ Object
- #close ⇒ Object
- #each ⇒ Object
-
#initialize(file, options = { }, &block) ⇒ Book
constructor
A new instance of Book.
- #save(file = nil) ⇒ Object
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, ={ }, &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() @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
#book ⇒ Object (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, ={ }, &block) new(file, , &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, = { }) if sheet.is_a? Hash = sheet sheet = nil end new_sheet_name = .delete(:as) after_or_before, base_sheet = .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 |
#close ⇒ Object
40 41 42 43 |
# File 'lib/wrap_excel/book.rb', line 40 def close @winapp.Workbooks.Close @winapp.Quit end |
#each ⇒ Object
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
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 |