Permalink
Browse files

reading support complete for els, xlsx and ods

  • Loading branch information...
1 parent 237fab4 commit 5a78218feadc8ff57b31ca0e802b3f4f4e395688 @zenkay committed Feb 19, 2012
View
@@ -2,4 +2,5 @@ utils.txt
spec/.DS_Store
objects/
logs/
-lib/.DS_Store
+lib/.DS_Store
+.DS_Store
View
@@ -1,10 +1,10 @@
PATH
remote: .
specs:
- simple_spreadsheet (0.0.1.alpha)
- roo
- rubyXL
- spreadsheet
+ simple_spreadsheet (0.0.3.alpha)
+ roo (= 1.10.1)
+ rubyXL (= 1.2.5)
+ spreadsheet (= 0.6.5.9)
GEM
remote: http://rubygems.org/
View
@@ -2,7 +2,11 @@
Recently a wrote a module to import raw data from Excel-like files to database and export it in various formats. To accomplish this i have to use different gems with different usage patterns so I decide to wrap them in a single simple gem. This is a spreadsheet reader and writer that (will) supports common formats: CSV (.csv), Excel (.xls, .xlsx), Open-office (.ods) and Google (online).
-Used gems: Roo (http://roo.rubyforge.org/), Spreadsheet (http://spreadsheet.ch/), RubyXL (https://github.com/gilt/rubyXL)
+Used gems:
+
+- Roo (http://roo.rubyforge.org/)
+- Spreadsheet (http://spreadsheet.ch/)
+- RubyXL (https://github.com/gilt/rubyXL)
## Installing
@@ -14,17 +18,14 @@ Add to your Gemfile and run the `bundle` command to install it.
**N.B. Requires Ruby 1.9.2 or later.**
-## Reading Spreadsheet
+## Basic functionality
+
+### Reading Spreadsheet
Example:
```ruby
s = SimpleSpreadsheet::SimpleSheet.read("my_spreadsheets_file.xls")
-s.select_worksheet = s.sheets.first
-s.first_row.upto(@workbook.last_row) do |line|
- data1 = s.cell(line, 1)
- data2 = s.cell(line, 3)
-end
```
Supported formats:
@@ -38,17 +39,15 @@ Supported formats:
<th>Google</th>
</tr>
<tr>
- <td>Yes (using Roo)</td>
+ <td>Not yet</td>
<td>Yes (using Roo)</td>
<td>Yes (using Roo)</td>
<td>Yes (using Roo)</td>
<td>Not yet</td>
</tr>
</table>
-
-
-## Writing Spreadsheet
+### Writing Spreadsheet
Example:
@@ -75,7 +74,7 @@ Supported formats:
</tr>
</table>
-## Editing Spreadsheet
+### Editing Spreadsheet
Example:
@@ -102,3 +101,33 @@ Supported formats:
</tr>
</table>
+## Recipes
+
+### Get all data from some cols of first sheet of a XLS file
+
+```ruby
+s = SimpleSpreadsheet::SimpleSheet.read("my_spreadsheets_file.xls")
+s.select_worksheet = s.sheets.first
+s.first_row.upto(@workbook.last_row) do |line|
+ data1 = s.cell(line, 1)
+ data2 = s.cell(line, 3)
+end
+```
+
+### Get all data from some cols of first sheet of a XLS file (option 2)
+
+```ruby
+s = SimpleSpreadsheet::SimpleSheet.read("my_spreadsheets_file.xls")
+s.first_row.upto(@workbook.last_row) do |line|
+ data1 = s.cell(line, 1, 1)
+ data2 = s.cell(line, 3, 1)
+end
+```
+
+## Future plans
+
+- Reading support for all formats
+- More efficent reading for big spreadsheets (extending Roo)
+- Writing support for all formats
+- More reading functionalities
+- Edit support fo all formats
@@ -4,8 +4,12 @@ def sheets
@engine.sheets
end
- def selected_sheet=(sheet)
- @engine.default_sheet = sheet
+ def default_sheet=(sheet)
+ if sheet.is_a? Integer
+ @engine.default_sheet = @engine.sheets[sheet - 1]
+ else
+ @engine.default_sheet = sheet
+ end
end
def first_row
@@ -16,8 +20,20 @@ def last_row
@engine.last_row
end
- def cell(row, col)
- @engine.cell(row, col)
+ def cell(row, col, sheet=nil)
+ if sheet.is_a? Integer
+ @engine.cell(row, col, @engine.sheets[sheet - 1])
+ else
+ @engine.cell(row, col, sheet)
+ end
+ end
+
+ def celltype(row, col, sheet=nil)
+ if sheet.is_a? Integer
+ @engine.celltype(row, col, @engine.sheets[sheet - 1])
+ else
+ @engine.celltype(row, col, sheet)
+ end
end
end
@@ -1,3 +1,3 @@
module SimpleSpreadsheet
- VERSION = "0.0.2.alpha"
+ VERSION = "0.0.3.alpha"
end
@@ -4,7 +4,7 @@ require File.expand_path('../lib/simple_spreadsheet/version', __FILE__)
Gem::Specification.new do |gem|
gem.authors = ["Andrea Mostosi"]
gem.email = ["andrea.mostosi@zenkay.net"]
- gem.description = %q{Simple spreadsheet reader and writer for common formats: Excel (.xls, .xlsx and .xml), Open-office (.ods) and Google (online).}
+ gem.description = %q{Simple spreadsheet reader for common formats: Excel (.xls, .xlsx) and Open-office (.ods). Future versions: writing and more formats.}
gem.summary = %q{Read and write most common spreadsheet format}
gem.homepage = "https://github.com/zenkay/simple_spreadsheet"
@@ -17,7 +17,7 @@ Gem::Specification.new do |gem|
# specify any dependencies here; for example:
gem.add_development_dependency "rspec"
- gem.add_runtime_dependency "roo"
- gem.add_runtime_dependency "spreadsheet"
- gem.add_runtime_dependency "rubyXL"
+ gem.add_runtime_dependency "roo", "1.10.1"
+ gem.add_runtime_dependency "spreadsheet", "0.6.5.9"
+ gem.add_runtime_dependency "rubyXL", "1.2.5"
end
Binary file not shown.
Binary file not shown.
View
@@ -0,0 +1 @@
+String1;1
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
@@ -4,19 +4,129 @@
describe SimpleSpreadsheet do
describe "Open Excel (.xls) file read-only mode" do
+ before do
+ @workbook = SimpleSpreadsheet::SimpleSheet.read(File.join(File.dirname(__FILE__), "fixtures/file.xls"))
+ end
it "should can open the file" do
- @workbook = SimpleSpreadsheet::SimpleSheet.read(File.join(File.dirname(__FILE__), "fixtures/empty_file.xls"))
@workbook.should_not be_nil
+ end
+ it "should use right class" do
@workbook.class.to_s.should eq("ExcelReader")
end
+ it "should see the right number of sheets" do
+ @workbook.sheets.count.should eq(2)
+ end
+ it "should read strings from first sheets" do
+ @workbook.cell(1, 1).should eq("String1")
+ end
+ it "should read integer from first sheets" do
+ @workbook.cell(1, 2).should eq(1)
+ end
+ it "should read strings from other sheets" do
+ @workbook.cell(1, 1, 2).should eq("String2")
+ end
+ it "should read integer from other sheets" do
+ @workbook.cell(1, 2, 2).should eq(2)
+ end
+ it "should read strings from other sheets (way 2)" do
+ @workbook.default_sheet = 2
+ @workbook.cell(1, 1).should eq("String2")
+ end
+ it "should read integer from other sheets (way 2)" do
+ @workbook.default_sheet = 2
+ @workbook.cell(1, 2).should eq(2)
+ end
end
describe "Open Excelx (.xlsx) file read-only mode" do
+ before do
+ @workbook = SimpleSpreadsheet::SimpleSheet.read(File.join(File.dirname(__FILE__), "fixtures/file.xlsx"))
+ end
it "should can open the file" do
- @workbook = SimpleSpreadsheet::SimpleSheet.read(File.join(File.dirname(__FILE__), "fixtures/empty_file.xlsx"))
@workbook.should_not be_nil
+ end
+ it "should use right class" do
@workbook.class.to_s.should eq("ExcelxReader")
end
+ it "should see the right number of sheets" do
+ @workbook.sheets.count.should eq(2)
+ end
+ it "should read strings from first sheets" do
+ @workbook.cell(1,1).should eq("String1")
+ end
+ it "should read integer from first sheets" do
+ @workbook.cell(1,2).should eq(1)
+ end
+ it "should read strings from other sheets" do
+ @workbook.cell(1, 1, 2).should eq("String2")
+ end
+ it "should read integer from other sheets" do
+ @workbook.cell(1, 2, 2).should eq(2)
+ end
+ it "should read strings from other sheets (way 2)" do
+ @workbook.default_sheet = 2
+ @workbook.cell(1, 1).should eq("String2")
+ end
+ it "should read integer from other sheets (way 2)" do
+ @workbook.default_sheet = 2
+ @workbook.cell(1, 2).should eq(2)
+ end
+ end
+
+ describe "Open Openoffice (.ods) file read-only mode" do
+ before do
+ @workbook = SimpleSpreadsheet::SimpleSheet.read(File.join(File.dirname(__FILE__), "fixtures/file.ods"))
+ end
+ it "should can open the file" do
+ @workbook.should_not be_nil
+ end
+ it "should use right class" do
+ @workbook.class.to_s.should eq("OpenofficeReader")
+ end
+ it "should see the right number of sheets" do
+ @workbook.sheets.count.should eq(2)
+ end
+ it "should read strings from first sheets" do
+ @workbook.cell(1,1).should eq("String1")
+ end
+ it "should read integer from first sheets" do
+ @workbook.cell(1,2).should eq(1)
+ end
+ it "should read strings from other sheets" do
+ @workbook.cell(1, 1, 2).should eq("String2")
+ end
+ it "should read integer from other sheets" do
+ @workbook.cell(1, 2, 2).should eq(2)
+ end
+ it "should read strings from other sheets (way 2)" do
+ @workbook.default_sheet = 2
+ @workbook.cell(1, 1).should eq("String2")
+ end
+ it "should read integer from other sheets (way 2)" do
+ @workbook.default_sheet = 2
+ @workbook.cell(1, 2).should eq(2)
+ end
end
+ # describe "Open CSV (.csv) file read-only mode" do
+ # before do
+ # @workbook = SimpleSpreadsheet::SimpleSheet.read(File.join(File.dirname(__FILE__), "fixtures/file.csv"))
+ # end
+ # it "should can open the file" do
+ # @workbook.should_not be_nil
+ # end
+ # it "should use right class" do
+ # @workbook.class.to_s.should eq("CsvReader")
+ # end
+ # it "should see the right number of sheets" do
+ # @workbook.sheets.count.should eq(1)
+ # end
+ # it "should read strings from first sheets" do
+ # @workbook.cell(1,1).should eq("String1")
+ # end
+ # it "should read integer from first sheets" do
+ # @workbook.cell(1,2).should eq(1)
+ # end
+ # end
+
end

0 comments on commit 5a78218

Please sign in to comment.