diff options
| author | Randy Morgan <[email protected]> | 2013-05-12 22:31:23 +0100 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2013-05-12 22:31:23 +0100 |
| commit | dc9e0523fb92f67002bc474f28f47e7255c33d3c (patch) | |
| tree | a0e2e6df0cb98e85fcbb17333cdc892b1ce7318e | |
| parent | 0c9a48eef60d4d81a2c2bd5a4b0b87c981e1e062 (diff) | |
| download | caxlsx-dc9e0523fb92f67002bc474f28f47e7255c33d3c.tar.gz caxlsx-dc9e0523fb92f67002bc474f28f47e7255c33d3c.zip | |
implement transpose for SimpleTypedList that supports sparse data.
| -rw-r--r-- | lib/axlsx/util/simple_typed_list.rb | 46 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/worksheet.rb | 7 | ||||
| -rw-r--r-- | test/workbook/worksheet/tc_worksheet.rb | 2 |
3 files changed, 37 insertions, 18 deletions
diff --git a/lib/axlsx/util/simple_typed_list.rb b/lib/axlsx/util/simple_typed_list.rb index 4d188ffd..9a5f2e3d 100644 --- a/lib/axlsx/util/simple_typed_list.rb +++ b/lib/axlsx/util/simple_typed_list.rb @@ -1,21 +1,9 @@ # encoding: UTF-8 module Axlsx + # A SimpleTypedList is a type restrictive collection that allows some of the methods from Array and supports basic xml serialization. # @private class SimpleTypedList - # The class constants of allowed types - # @return [Array] - attr_reader :allowed_types - - # The index below which items cannot be removed - # @return [Integer] - attr_reader :locked_at - - # The tag name to use when serializing this object - # by default the parent node for all items in the list is the classname of the first allowed type with the first letter in lowercase. - # @return [String] - attr_reader :serialize_as - # Creats a new typed list # @param [Array, Class] type An array of Class objects or a single Class object # @param [String] serialize_as The tag name to use in serialization @@ -33,6 +21,38 @@ module Axlsx @serialize_as = serialize_as end + # The class constants of allowed types + # @return [Array] + attr_reader :allowed_types + + # The index below which items cannot be removed + # @return [Integer] + attr_reader :locked_at + + # The tag name to use when serializing this object + # by default the parent node for all items in the list is the classname of the first allowed type with the first letter in lowercase. + # @return [String] + attr_reader :serialize_as + + # Transposes the list (without blowing up like ruby does) + # any non populated cell in the matrix will be a nil value + def transpose + return @list.clone if @list.size == 0 + row_count = @list.size + max_column_count = @list.map{|row| row.cells.size}.max + result = Array.new(max_column_count) { Array.new(row_count) } + 0..row_count.times do |row_index| + 0..max_column_count.times do |column_index| + datum = if @list[row_index].cells.size >= max_column_count + @list[row_index].cells[column_index] + elsif block_given? + yield(column_index, row_index) + end + result[column_index][row_index] = datum + end + end + result + end # Lock this list at the current size # @return [self] def lock diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb index 32e616e2..1187bf83 100644 --- a/lib/axlsx/workbook/worksheet/worksheet.rb +++ b/lib/axlsx/workbook/worksheet/worksheet.rb @@ -39,7 +39,6 @@ module Axlsx @header_footer = HeaderFooter.new options[:header_footer] if options[:header_footer] end - # The name of the worksheet # @return [String] def name @@ -115,14 +114,14 @@ module Axlsx @rows ||= SimpleTypedList.new Row end - # returns the sheet data as columnw + # returns the sheet data as columns def cols @rows.transpose end - # An range that excel will apply an autfilter to "A1:B3" + # An range that excel will apply an auto-filter to "A1:B3" # This will turn filtering on for the cells in the range. - # The first row is considered the header, while subsequent rows are considerd to be data. + # The first row is considered the header, while subsequent rows are considered to be data. # @return String def auto_filter @auto_filter ||= AutoFilter.new self diff --git a/test/workbook/worksheet/tc_worksheet.rb b/test/workbook/worksheet/tc_worksheet.rb index ed8c7cab..b05a1c56 100644 --- a/test/workbook/worksheet/tc_worksheet.rb +++ b/test/workbook/worksheet/tc_worksheet.rb @@ -205,7 +205,7 @@ class TestWorksheet < Test::Unit::TestCase def test_cols @ws.add_row [1,2,3,4] @ws.add_row [1,2,3,4] - @ws.add_row [1,2,3,4] + @ws.add_row [1,2,3] @ws.add_row [1,2,3,4] c = @ws.cols[1] assert_equal(c.size, 4) |
