diff options
| author | Randy Morgan <[email protected]> | 2012-04-02 21:41:37 -0700 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2012-04-02 21:41:37 -0700 |
| commit | de475afa8d91fedf84d1d90e1407cc404ee4b5df (patch) | |
| tree | dfc5c4fe5f043a00981e89bdd6a6e53d4cf7f6d8 | |
| parent | 02341a99e6dcc9f4bb1c4273fac9c3d3cf6c642d (diff) | |
| parent | d2b1274f7bf6058e484158cfd78b4ba7212f91b2 (diff) | |
| download | caxlsx-de475afa8d91fedf84d1d90e1407cc404ee4b5df.tar.gz caxlsx-de475afa8d91fedf84d1d90e1407cc404ee4b5df.zip | |
Merge pull request #73 from jurriaan/empty-cell
Support for empty cells like Excel
| -rw-r--r-- | examples/example.rb | 3 | ||||
| -rw-r--r-- | lib/axlsx/workbook/shared_strings_table.rb | 2 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/cell.rb | 4 | ||||
| -rw-r--r-- | test/workbook/worksheet/tc_cell.rb | 4 | ||||
| -rw-r--r-- | test/workbook/worksheet/tc_row.rb | 13 |
5 files changed, 22 insertions, 4 deletions
diff --git a/examples/example.rb b/examples/example.rb index 04ad6f4c..d93f5dcc 100644 --- a/examples/example.rb +++ b/examples/example.rb @@ -266,7 +266,8 @@ end wb.add_worksheet(:name => "custom column widths") do |sheet| sheet.add_row ["I use autowidth and am very wide", "I use a custom width and am narrow"] - sheet.column_widths nil, 3 + sheet.add_row ['abcdefg', 'This is a very long text and should flow into the right cell', nil, 'xxx' ] + sheet.column_widths nil, 3, 5, nil end ##Fit to page printing diff --git a/lib/axlsx/workbook/shared_strings_table.rb b/lib/axlsx/workbook/shared_strings_table.rb index d9a22432..45402f5b 100644 --- a/lib/axlsx/workbook/shared_strings_table.rb +++ b/lib/axlsx/workbook/shared_strings_table.rb @@ -29,7 +29,7 @@ module Axlsx # Creates a new Shared Strings Table agains an array of cells # @param [Array] cells This is an array of all of the cells in the workbook def initialize(cells) - cells = cells.flatten.reject { |c| c.type != :string || c.value.start_with?('=') } + cells = cells.flatten.reject { |c| c.type != :string || c.value.nil? || c.value.start_with?('=') } @count = cells.size @unique_cells = [] @shared_xml_string = "" diff --git a/lib/axlsx/workbook/worksheet/cell.rb b/lib/axlsx/workbook/worksheet/cell.rb index 3557a89b..f4c6ff09 100644 --- a/lib/axlsx/workbook/worksheet/cell.rb +++ b/lib/axlsx/workbook/worksheet/cell.rb @@ -307,12 +307,13 @@ module Axlsx # @param [String] str The string index the cell content will be appended to. Defaults to empty string. # @return [String] xml text for the cell def to_xml_string(r_index, c_index, str = '') + return str if @value.nil? str << '<c r="' << Axlsx::cell_r(c_index, r_index) << '" s="' << @style.to_s << '" ' case @type when :string #parse formula if @value.start_with?('=') - str << 't="str"><f>' << value.to_s.gsub('=', '') << '</f>' + str << 't="str"><f>' << @value.to_s.gsub('=', '') << '</f>' else #parse shared if @ssti @@ -382,6 +383,7 @@ module Axlsx # About Time - Time in OOXML is *different* from what you might expect. The history as to why is interesting, but you can safely assume that if you are generating docs on a mac, you will want to specify Workbook.1904 as true when using time typed values. # @see Axlsx#date1904 def cast_value(v) + return nil if v.nil? if @type == :date self.style = STYLE_DATE if self.style == 0 v diff --git a/test/workbook/worksheet/tc_cell.rb b/test/workbook/worksheet/tc_cell.rb index 2313a8ba..1cfd6169 100644 --- a/test/workbook/worksheet/tc_cell.rb +++ b/test/workbook/worksheet/tc_cell.rb @@ -98,7 +98,9 @@ class TestCell < Test::Unit::TestCase @c.type = :float assert_equal(@c.send(:cast_value, "1.0"), 1.0) @c.type = :string - assert_equal(@c.send(:cast_value, nil), "") + assert_equal(@c.send(:cast_value, nil), nil) + @c.type = :float + assert_equal(@c.send(:cast_value, nil), nil) @c.type = :boolean assert_equal(@c.send(:cast_value, true), 1) assert_equal(@c.send(:cast_value, false), 0) diff --git a/test/workbook/worksheet/tc_row.rb b/test/workbook/worksheet/tc_row.rb index d1507aa1..a953566e 100644 --- a/test/workbook/worksheet/tc_row.rb +++ b/test/workbook/worksheet/tc_row.rb @@ -27,6 +27,19 @@ class TestRow < Test::Unit::TestCase r.cells.each { |c| assert_equal(c.style,1) } end + def test_nil_cells + row = @ws.add_row([nil,1,2,nil,4,5,nil]) + r_s_xml = Nokogiri::XML(row.to_xml_string(0, '')) + assert_equal(r_s_xml.xpath(".//row/c").size, 4) + end + + def test_nil_cell_r + row = @ws.add_row([nil,1,2,nil,4,5,nil]) + r_s_xml = Nokogiri::XML(row.to_xml_string(0, '')) + assert_equal(r_s_xml.xpath(".//row/c").first['r'], 'B1') + assert_equal(r_s_xml.xpath(".//row/c").last['r'], 'F1') + end + def test_index assert_equal(@row.index, @row.worksheet.rows.index(@row)) end |
