rubyexcel 0.3.3 → 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fc15333e40633038ed0ed4d6c57fd67a978b3bb6
4
- data.tar.gz: c4ae59d243e7fb209247bf41f73d21e6ccad2c32
3
+ metadata.gz: 19940731a3d60be4f0f8f4304585f8c36b4855b9
4
+ data.tar.gz: 0745972f955570f622ebf25b0f89c2cfa253430d
5
5
  SHA512:
6
- metadata.gz: 4fcc164c88a1c3e8b17d8ce62434d891980b60ccd90fed2d4f040935d24c7d9e741364485d76a4efd272ebed93593691f58a264c724d53ea4d94f480c1bdb793
7
- data.tar.gz: 82ea96341ab519b5d6cc060502b5b142ffad6cc3cb222a08d80eb06267a514700c8429923b666f11706cea9143e4315c61893b7d3ff0d8652d6c5df8193b9b33
6
+ metadata.gz: 504b261f043af26e1fc4a055ab8c18d8f943809eaad2cea83e99271c415274bd88104558c9374832f43077bd8e2589e83bf35a53aa09c60bee5e8620872b12c2
7
+ data.tar.gz: 05ca9a4818c5d544cfaa6233d20e607192d1d132a221e33ebc4971a24d6aba0dd6f55b97d3679b9d3724eb2fc9086cf76eff288270c52cb9fba8616908a4ed45
data/README.md CHANGED
@@ -236,6 +236,20 @@ wb.delete( 'Sheet1' )
236
236
  wb.delete( sheet1 )
237
237
  wb.delete( /sheet1/i )
238
238
 
239
+ #Import a WIN32OLE Workbook or Sheet, either by passing the Object or a Filename
240
+ #Parameters: WIN32OLE Object or Filename, SheetName or nil for all Sheets, true to keep Excel Formulas or omit to import Values.
241
+ wb = RubyExcel.import( '/path/to/file.xlsx' )
242
+ wb = RubyExcel.import( '/path/to/file.xlsx', 'Sheet2' )
243
+ wb = RubyExcel.import( '/path/to/file.xlsx', 'Sheet2', true )
244
+ #Or:
245
+ require 'win32ole'
246
+ excel = WIN32OLE.new( 'excel.application' )
247
+ excel.visible = true #Optional
248
+ my_workbook = excel.workbooks.open( '/path/to/file.xlsx' )
249
+ wb.import( my_workbook )
250
+ #Or:
251
+ wb.import( my_workbook.sheets(1) )
252
+
239
253
  #Shortcut to create a sheet with a default name and fill it with data
240
254
  wb.load( data )
241
255
 
@@ -96,6 +96,70 @@ module RubyExcel
96
96
  wb
97
97
  end
98
98
 
99
+ #
100
+ # Import a WIN32OLE Object as a Workbook or Sheet
101
+ #
102
+ # @param [WIN32OLE::Workbook, WIN32OLE::Sheet, String] other The WIN32OLE Object, either Sheet or Workbook, to import, or a path to the file.
103
+ # @param [String] sheetname the name of a specific Sheet to import.
104
+ # @param [Boolean] keep_formulas Retain Excel formulas rather than importing their current values
105
+ # @return [self] self with the data and name(s) imported.
106
+ #
107
+
108
+ def import( other, sheetname=nil, keep_formulas=false )
109
+ operation = ( keep_formulas ? :formula : :value )
110
+
111
+ if other.is_a?( String )
112
+
113
+ # Filename
114
+ File.exists?( other ) || fail( ArgumentError, "Unable to find file: #{ other }" )
115
+
116
+ #Open the file with Excel
117
+ excel = WIN32OLE.new( 'excel.application' )
118
+ excel.displayalerts = false
119
+ wb = excel.workbooks.open({'filename'=> other, 'readOnly' => true})
120
+
121
+ # Only one sheet, or the entire Workbook?
122
+ if sheetname
123
+ add( sheetname ).load( wb.sheets( sheetname ).usedrange.send( operation ) )
124
+ else
125
+ self.name = File.basename( other, '.*' )
126
+ wb.sheets.each { |sh| add( sh.name ).load( sh.usedrange.send( operation ) ) }
127
+ end
128
+
129
+ # Cleanup
130
+ wb.close
131
+ excel.quit
132
+
133
+ elsif !other.respond_to?( :ole_respond_to? )
134
+
135
+ fail ArgumentError, "Invalid input: #{other.class}"
136
+
137
+ elsif other.ole_respond_to?( :sheets )
138
+
139
+ # Workbook
140
+
141
+ # Only one sheet, or the entire Workbook?
142
+ if sheetname
143
+ add( sheetname ).load( other.sheets( sheetname ).usedrange.send( operation ) )
144
+ else
145
+ self.name = File.basename( other.name, '.*' )
146
+ other.sheets.each { |sh| add( sh.name ).load( sh.usedrange.send( operation ) ) }
147
+ end
148
+
149
+ elsif other.ole_respond_to?( :usedrange )
150
+
151
+ # Sheet
152
+ add( other.name ).load( other.usedrange.send( operation ) )
153
+
154
+ else
155
+
156
+ fail ArgumentError, "Object not recognised as a WIN32OLE Workbook or Sheet.\n#{other.inspect}"
157
+
158
+ end
159
+
160
+ self
161
+ end
162
+
99
163
  #
100
164
  # Take an Excel Sheet and standardise some of the formatting
101
165
  #
@@ -60,4 +60,12 @@ module RubyExcel
60
60
  Workbook.new.load RubyExcel.sample_data
61
61
  end
62
62
 
63
+ #
64
+ # Shortcut to import a WIN32OLE Workbook or Sheet
65
+ #
66
+
67
+ def self.import( *args )
68
+ Workbook.new.import( *args )
69
+ end
70
+
63
71
  end
data/lib/rubyexcel.rb CHANGED
@@ -34,7 +34,7 @@ module RubyExcel
34
34
  include Enumerable
35
35
 
36
36
  # Names of methods which require win32ole
37
- ExcelToolsMethods = [ :disable_formulas!, :documents_path, :dump_to_sheet, :get_excel, :get_workbook, :make_sheet_pretty, :save_excel, :to_excel, :to_safe_format, :to_safe_format! ]
37
+ ExcelToolsMethods = [ :disable_formulas!, :documents_path, :dump_to_sheet, :get_excel, :get_workbook, :import, :make_sheet_pretty, :save_excel, :to_excel, :to_safe_format, :to_safe_format! ]
38
38
 
39
39
  # Get and set the Workbook name
40
40
  attr_accessor :name
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubyexcel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel Pearson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-26 00:00:00.000000000 Z
11
+ date: 2013-09-30 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A tabular data structure in Ruby, with header-based helper methods for
14
14
  analysis and editing, and some of Excel's API style. Can output as 2D Array, HTML,