| CODENOTIFIER | HelpYou are not signed inSign in |
Project: Ruport-util
Revision: 39
Author: wes
Date: 04 Jan 2008 14:31:27
Changes:Added the start_row option so that the ods file loaded would not have to start at the first row.
Files:| ... | ...@@ -32,6 +32,10 @@ | |
| 32 | 32 | # # Select sheet - default is the first sheet. |
| 33 | 33 | # table = Table.load_ods('myspreadsheet.ods', {:select_sheet => 1}) |
| 34 | 34 | # |
| 35 | # # Start row - default is the first row. Use this to override where | |
| 36 | # the first row should start. | |
| 37 | # table = Table.load_ods('myspreadsheet.ods', {:start_row => 1}) | |
| 38 | # | |
| 35 | 39 | def load_ods(ods_file, options={}) |
| 36 | 40 | get_table_from_ods_file(ods_file, options) |
| 37 | 41 | end |
| ... | ...@@ -49,6 +53,10 @@ | |
| 49 | 53 | # # Select sheet - default is the first sheet. |
| 50 | 54 | # table = Table.parse_ods(openoffice_object, {:select_sheet => 1}) |
| 51 | 55 | # |
| 56 | # # Start row - default is the first row. Use this to override where | |
| 57 | # the first row should start. | |
| 58 | # table = Table.parse_ods('myspreadsheet.ods', {:start_row => 1}) | |
| 59 | # | |
| 52 | 60 | def parse_ods(ods_object, options={}) |
| 53 | 61 | get_table_from_ods(ods_object, options) |
| 54 | 62 | end |
| ... | ...@@ -62,20 +70,28 @@ | |
| 62 | 70 | end |
| 63 | 71 | |
| 64 | 72 | def get_table_from_ods(oo, options) #:nodoc: |
| 65 | # Don't need to require 'roo' here because | |
| 66 | options = {:has_column_names => true, :select_sheet => oo.sheets.first}.merge(options) | |
| 73 | options = {:has_column_names => true, | |
| 74 | :select_sheet => oo.sheets.first, | |
| 75 | :start_row => 0}.merge(options) | |
| 67 | 76 | oo.default_sheet = options[:select_sheet] |
| 68 | start_row = options[:has_column_names] == true ? 2 : 1 | |
| 69 | ||
| 77 | ||
| 78 | options[:start_row] = options[:start_row].to_i + 1 unless options[:start_row].nil? | |
| 79 | start_row = options[:start_row] | |
| 80 | ||
| 81 | raise 'start_row must be greater than or equal to zero' if options[:start_row].to_i < 0 | |
| 82 | ||
| 83 | last_row_index_zero = oo.last_row - 1 | |
| 84 | raise "start_row must be less than or equal to #{last_row_index_zero}" if !oo.last_row.nil? and | |
| 85 | (options[:start_row].to_i > oo.last_row) | |
| 86 | ||
| 70 | 87 | table = self.new(options) do |feeder| |
| 71 | 88 | |
| 72 | # This is fine because they should all be strings | |
| 73 | feeder.data.column_names = oo.row(1) if options[:has_column_names] == true | |
| 89 | if options[:has_column_names] == true | |
| 90 | feeder.data.column_names = oo.row(start_row) | |
| 91 | start_row = start_row + 1 | |
| 92 | end | |
| 74 | 93 | |
| 75 | # don't loop through the rows if they are none | |
| 76 | 94 | unless oo.last_row.nil? |
| 77 | # Loop through and grab each cell that way the data types | |
| 78 | # are captured as well. | |
| 79 | 95 | start_row.upto(oo.last_row) do |row| |
| 80 | 96 | tempArr = [] |
| 81 | 97 | 1.upto(oo.last_column) do |col| |
| ... | ...@@ -87,19 +103,18 @@ | |
| 87 | 103 | |
| 88 | 104 | end |
| 89 | 105 | |
| 90 | return table | |
| 106 | return table | |
| 91 | 107 | end |
| 92 | 108 | |
| 93 | end # End FromODS | |
| 109 | end | |
| 94 | 110 | |
| 95 | 111 | extend FromODS |
| 96 | 112 | |
| 97 | end # End class Table | |
| 113 | end | |
| 98 | 114 | |
| 99 | 115 | |
| 100 | 116 | module Kernel |
| 101 | 117 | |
| 102 | # Use Ruport's original Table method if this Table method is not needed. | |
| 103 | 118 | alias :RuportTableMethod :Table |
| 104 | 119 | |
| 105 | 120 | # Updates the Ruport interface for creating Data::Tables with |
| ... | ...@@ -51,7 +51,7 @@ | |
| 51 | 51 | table.should_not be_nil |
| 52 | 52 | end |
| 53 | 53 | |
| 54 | it "shouldn't be nil if a Ruport::Data::Ruport::Data::Table parameter is passed" do | |
| 54 | it "shouldn't be nil if a Ruport::Data::Table parameter is passed" do | |
| 55 | 55 | table = Table(@csv_file) # Pass cs file |
| 56 | 56 | table.should_not be_nil |
| 57 | 57 | end |
| ... | ...@@ -70,13 +70,76 @@ | |
| 70 | 70 | table.should_not be_nil |
| 71 | 71 | end |
| 72 | 72 | |
| 73 | it "shouldn't be nil if a Ruport::Data::Ruport::Data::Table parameter is passed with options params" do | |
| 73 | it "shouldn't be nil if a Ruport::Data::Table parameter is passed with options params" do | |
| 74 | 74 | table = Table(@csv_file, {:has_column_names => false}) # Pass cs file |
| 75 | 75 | table.should_not be_nil |
| 76 | 76 | end |
| 77 | 77 | |
| 78 | it "should raise if start_row is less than zero" do | |
| 79 | lambda do | |
| 80 | Table(@ods_file, {:start_row => -2}) | |
| 81 | end.should raise_error | |
| 82 | end | |
| 83 | ||
| 84 | it "should raise if start_row is greater than the number of rows (starting at 0) in the spreadsheet" do | |
| 85 | lambda do | |
| 86 | Table(@ods_file, {:start_row => 20}) | |
| 87 | end.should raise_error | |
| 88 | end | |
| 89 | ||
| 90 | ||
| 91 | # ==== Table load check ==== | |
| 92 | ||
| 93 | # Output: | |
| 94 | # +-----------------------------+ | |
| 95 | # | Name | Age | DOB | | |
| 96 | # | Andy | 27.0 | 1980-01-20 | | |
| 97 | # | Bob | 26.0 | 1981-02-11 | | |
| 98 | # | Charlie | 20.0 | 1987-03-14 | | |
| 99 | # | David | 73.0 | 1997-04-26 | | |
| 100 | # +-----------------------------+ | |
| 101 | it "table should be valid without column names loaded from ods file starting at the row 1 (index 0) - column names will be data" do | |
| 102 | # Load data from ods file but do not load column headers. | |
| 103 | table = Table(@ods_file, {:has_column_names => false, :start_row => 0}) | |
| 104 | table.should_not be_nil | |
| 105 | table.column_names.should == [] | |
| 106 | ||
| 107 | # Add headers to the first position | |
| 108 | @rows.insert(0, @ods_file_column_names) | |
| 109 | ||
| 110 | table.each { |r| r.to_a.should == @rows.shift | |
| 111 | r.attributes.should == [0, 1, 2] } | |
| 112 | end | |
| 78 | 113 | |
| 79 | # ==== Ruport::Data::Table load check ==== | |
| 114 | # Output: | |
| 115 | # +-----------------------------+ | |
| 116 | # | Bob | 26.0 | 1981-02-11 | | |
| 117 | # | Charlie | 20.0 | 1987-03-14 | | |
| 118 | # | David | 73.0 | 1997-04-26 | | |
| 119 | # +-----------------------------+ | |
| 120 | it "table should be valid without column names loaded from ods file starting at row 3 (index 2)" do | |
| 121 | # Load data from ods file but do not load column headers. | |
| 122 | # Will start at Row 3 (index 2): ['Bob', 26.0, Date.parse('02/11/1981')] | |
| 123 | table = Table(@ods_file, {:has_column_names => false, :start_row => 2}) | |
| 124 | table.should_not be_nil | |
| 125 | table.column_names.should == [] | |
| 126 | ||
| 127 | # The header row has not been included yet so don't worry about that one | |
| 128 | # just delete the first row in @rows. | |
| 129 | @rows.delete_at(0) # delete ['Andy', 27.0, Date.parse('01/20/1980')] | |
| 130 | ||
| 131 | table.each { |r| r.to_a.should == @rows.shift | |
| 132 | r.attributes.should == [0, 1, 2] } | |
| 133 | end | |
| 134 | ||
| 135 | # Output: | |
| 136 | # +-----------------------------+ | |
| 137 | # | Name | Age | DOB | | |
| 138 | # | Andy | 27.0 | 1980-01-20 | | |
| 139 | # | Bob | 26.0 | 1981-02-11 | | |
| 140 | # | Charlie | 20.0 | 1987-03-14 | | |
| 141 | # | David | 73.0 | 1997-04-26 | | |
| 142 | # +-----------------------------+ | |
| 80 | 143 | it "table should be valid without column names loaded from ods file" do |
| 81 | 144 | # Load data from ods file but do not load column headers. |
| 82 | 145 | table = Table(@ods_file, {:has_column_names => false}) |
| ... | ...@@ -90,6 +153,15 @@ | |
| 90 | 153 | r.attributes.should == [0, 1, 2] } |
| 91 | 154 | end |
| 92 | 155 | |
| 156 | # Output: | |
| 157 | # +-----------------------------+ | |
| 158 | # | Name | Age | DOB | | |
| 159 | # +-----------------------------+ | |
| 160 | # | Andy | 27.0 | 1980-01-20 | | |
| 161 | # | Bob | 26.0 | 1981-02-11 | | |
| 162 | # | Charlie | 20.0 | 1987-03-14 | | |
| 163 | # | David | 73.0 | 1997-04-26 | | |
| 164 | # +-----------------------------+ | |
| 93 | 165 | it "table should be valid with column names loaded from ods file" do |
| 94 | 166 | # Load data from ods file but do not load column headers. |
| 95 | 167 | table = Table(@ods_file) |
| ... | ...@@ -100,6 +172,15 @@ | |
| 100 | 172 | r.attributes.should == @ods_file_column_names } |
| 101 | 173 | end |
| 102 | 174 | |
| 175 | # Output: | |
| 176 | # +--------------------------+ | |
| 177 | # | Name | Age | Pet_Type | | |
| 178 | # +--------------------------+ | |
| 179 | # | Tigger | 3.0 | Cat | | |
| 180 | # | Chai | 4.0 | Dog | | |
| 181 | # | Rusky | 6.0 | Dog | | |
| 182 | # | Sam | 13.0 | Dog | | |
| 183 | # +--------------------------+ | |
| 103 | 184 | it "table should be valid with column names loaded from ods file using Sheet2" do |
| 104 | 185 | # Load data from ods file but do not load column headers. |
| 105 | 186 | table = Table(@ods_file, {:select_sheet => 'Sheet2'}) |
| ... | ...@@ -110,6 +191,15 @@ | |
| 110 | 191 | r.attributes.should == @ods_file_column_names2 } |
| 111 | 192 | end |
| 112 | 193 | |
| 194 | # Output: | |
| 195 | # +--------------------------+ | |
| 196 | # | Name | Age | Pet_Type | | |
| 197 | # +--------------------------+ | |
| 198 | # | Tigger | 3.0 | Cat | | |
| 199 | # | Chai | 4.0 | Dog | | |
| 200 | # | Rusky | 6.0 | Dog | | |
| 201 | # | Sam | 13.0 | Dog | | |
| 202 | # +--------------------------+ | |
| 113 | 203 | it "should be valid if an Openoffice object is passed using parse_ods method" do |
| 114 | 204 | oo = Openoffice.new(@ods_file) |
| 115 | 205 | oo.default_sheet = oo.sheets.first |
| ... | ...@@ -120,7 +210,7 @@ | |
| 120 | 210 | |
| 121 | 211 | table.each { |r| r.to_a.should == @rows.shift |
| 122 | 212 | r.attributes.should == @ods_file_column_names } |
| 123 | end | |
| 213 | end | |
| 124 | 214 | |
| 125 | 215 | end |
| 126 | 216 |