diff options
| -rw-r--r-- | lib/axlsx/drawing/scatter_series.rb | 6 | ||||
| -rw-r--r-- | lib/axlsx/package.rb | 17 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/cell_serializer.rb | 2 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/pivot_table.rb | 24 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/rich_text_run.rb | 13 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/worksheet.rb | 44 | ||||
| -rw-r--r-- | test/drawing/tc_pic.rb | 7 | ||||
| -rw-r--r-- | test/drawing/tc_scatter_series.rb | 21 | ||||
| -rw-r--r-- | test/drawing/tc_title.rb | 5 | ||||
| -rw-r--r-- | test/tc_package.rb | 7 | ||||
| -rw-r--r-- | test/workbook/tc_shared_strings_table.rb | 5 | ||||
| -rw-r--r-- | test/workbook/tc_workbook.rb | 19 | ||||
| -rw-r--r-- | test/workbook/worksheet/tc_cell.rb | 14 | ||||
| -rw-r--r-- | test/workbook/worksheet/tc_rich_text_run.rb | 22 |
14 files changed, 93 insertions, 113 deletions
diff --git a/lib/axlsx/drawing/scatter_series.rb b/lib/axlsx/drawing/scatter_series.rb index 6b71227c..b9ca2c1d 100644 --- a/lib/axlsx/drawing/scatter_series.rb +++ b/lib/axlsx/drawing/scatter_series.rb @@ -27,7 +27,7 @@ module Axlsx # Line smoothing between data points # @return [Boolean] attr_reader :smooth - + # Creates a new ScatterSeries def initialize(chart, options={}) @xData, @yData = nil @@ -55,7 +55,7 @@ module Axlsx Axlsx::validate_boolean(v) @smooth = v end - + # @see ln_width def ln_width=(v) @ln_width = v @@ -85,7 +85,7 @@ module Axlsx end if ln_width str << '<c:spPr>' - str << '<a:ln w="' << ln_width << '"/>' + str << '<a:ln w="' << ln_width.to_s << '"/>' str << '</c:spPr>' end @xData.to_xml_string(str) unless @xData.nil? diff --git a/lib/axlsx/package.rb b/lib/axlsx/package.rb index 5b5ea14a..da661b34 100644 --- a/lib/axlsx/package.rb +++ b/lib/axlsx/package.rb @@ -147,11 +147,7 @@ module Axlsx errors = [] parts.each do |part| unless part[:schema].nil? - if part[:doc].is_a? String - errors.concat validate_single_doc(part[:schema], part[:doc]) - else - errors.concat validate_single_doc(part[:schema], part[:doc].to_xml_string) - end + errors.concat validate_single_doc(part[:schema], part[:doc].to_xml_string) end end errors @@ -165,20 +161,13 @@ module Axlsx def write_parts(zip) p = parts p.each do |part| - #next unless part[:entry] == CORE_PN unless part[:doc].nil? zip.put_next_entry(zip_entry_for_part(part)) - if part[:doc].is_a? String - entry = ['1.9.2', '1.9.3'].include?(RUBY_VERSION) ? part[:doc].force_encoding('BINARY') : part[:doc] - zip.puts(entry) - else - part[:doc].to_xml_string(zip) - end + part[:doc].to_xml_string(zip) end unless part[:path].nil? zip.put_next_entry(zip_entry_for_part(part)) - # binread for 1.9.3 - zip.write IO.respond_to?(:binread) ? IO.binread(part[:path]) : IO.read(part[:path]) + zip.write IO.read(part[:path]) end end zip diff --git a/lib/axlsx/workbook/worksheet/cell_serializer.rb b/lib/axlsx/workbook/worksheet/cell_serializer.rb index 76a3c386..eb834b08 100644 --- a/lib/axlsx/workbook/worksheet/cell_serializer.rb +++ b/lib/axlsx/workbook/worksheet/cell_serializer.rb @@ -159,8 +159,6 @@ module Axlsx str << ('t="' << serialization_type.to_s << '"') if serialization_type str << ('><v>' << serialization_value.to_s << '</v>') end - - end end end diff --git a/lib/axlsx/workbook/worksheet/pivot_table.rb b/lib/axlsx/workbook/worksheet/pivot_table.rb index fffced0b..55174968 100644 --- a/lib/axlsx/workbook/worksheet/pivot_table.rb +++ b/lib/axlsx/workbook/worksheet/pivot_table.rb @@ -170,12 +170,12 @@ module Axlsx def to_xml_string(str = '') str << '<?xml version="1.0" encoding="UTF-8"?>' str << ('<pivotTableDefinition xmlns="' << XML_NS << '" name="' << name << '" cacheId="' << cache_definition.cache_id.to_s << '" dataOnRows="1" applyNumberFormats="0" applyBorderFormats="0" applyFontFormats="0" applyPatternFormats="0" applyAlignmentFormats="0" applyWidthHeightFormats="1" dataCaption="Data" showMultipleLabel="0" showMemberPropertyTips="0" useAutoFormatting="1" indent="0" compact="0" compactData="0" gridDropZones="1" multipleFieldFilters="0">') - str << ( '<location firstDataCol="1" firstDataRow="1" firstHeaderRow="1" ref="' << ref << '"/>') - str << ( '<pivotFields count="' << header_cells_count.to_s << '">') + str << ('<location firstDataCol="1" firstDataRow="1" firstHeaderRow="1" ref="' << ref << '"/>') + str << ('<pivotFields count="' << header_cells_count.to_s << '">') header_cell_values.each do |cell_value| - str << pivot_field_for(cell_value,!no_subtotals_on_headers.include?(cell_value)) + str << pivot_field_for(cell_value, !no_subtotals_on_headers.include?(cell_value)) end - str << '</pivotFields>' + str << '</pivotFields>' if rows.empty? str << '<rowFields count="1"><field x="-2"/></rowFields>' str << '<rowItems count="2"><i><x/></i> <i i="1"><x v="1"/></i></rowItems>' @@ -263,25 +263,25 @@ module Axlsx def pivot_field_for(cell_ref, subtotal=true) if rows.include? cell_ref if subtotal - '<pivotField axis="axisRow" compact="0" outline="0" subtotalTop="0" showAll="0" includeNewItemsInFilter="1">' + '<items count="1"><item t="default"/></items>' + '</pivotField>' + '<pivotField axis="axisRow" compact="0" outline="0" subtotalTop="0" showAll="0" includeNewItemsInFilter="1"><items count="1"><item t="default"/></items></pivotField>' else - '<pivotField axis="axisRow" compact="0" outline="0" subtotalTop="0" showAll="0" includeNewItemsInFilter="1" defaultSubtotal="0">' + '</pivotField>' + '<pivotField axis="axisRow" compact="0" outline="0" subtotalTop="0" showAll="0" includeNewItemsInFilter="1" defaultSubtotal="0"></pivotField>' end elsif columns.include? cell_ref - '<pivotField axis="axisCol" compact="0" outline="0" subtotalTop="0" showAll="0" includeNewItemsInFilter="1">' + '<items count="1"><item t="default"/></items>' + '</pivotField>' + '<pivotField axis="axisCol" compact="0" outline="0" subtotalTop="0" showAll="0" includeNewItemsInFilter="1"><items count="1"><item t="default"/></items></pivotField>' elsif pages.include? cell_ref - '<pivotField axis="axisPage" compact="0" outline="0" subtotalTop="0" showAll="0" includeNewItemsInFilter="1">' + '<items count="1"><item t="default"/></items>' + '</pivotField>' + '<pivotField axis="axisPage" compact="0" outline="0" subtotalTop="0" showAll="0" includeNewItemsInFilter="1"><items count="1"><item t="default"/></items></pivotField>' elsif data_refs.include? cell_ref - '<pivotField dataField="1" compact="0" outline="0" subtotalTop="0" showAll="0" includeNewItemsInFilter="1">' + '</pivotField>' + '<pivotField dataField="1" compact="0" outline="0" subtotalTop="0" showAll="0" includeNewItemsInFilter="1"></pivotField>' else - '<pivotField compact="0" outline="0" subtotalTop="0" showAll="0" includeNewItemsInFilter="1">' + '</pivotField>' + '<pivotField compact="0" outline="0" subtotalTop="0" showAll="0" includeNewItemsInFilter="1"></pivotField>' end end - + def data_refs data.map { |hash| hash[:ref] } end - + def header_range range.gsub(/^(\w+?)(\d+)\:(\w+?)\d+$/, '\1\2:\3\2') end diff --git a/lib/axlsx/workbook/worksheet/rich_text_run.rb b/lib/axlsx/workbook/worksheet/rich_text_run.rb index 7ee3bf3a..e709f265 100644 --- a/lib/axlsx/workbook/worksheet/rich_text_run.rb +++ b/lib/axlsx/workbook/worksheet/rich_text_run.rb @@ -154,19 +154,6 @@ module Axlsx set_run_style nil, :scheme, v end - # The Shared Strings Table index for this cell - # @return [Integer] - attr_reader :ssti - - # @return [Integer] The cellXfs item index applied to this cell. - # @raise [ArgumentError] Invalid cellXfs id if the value provided is not within cellXfs items range. - def style=(v) - Axlsx::validate_unsigned_int(v) - count = styles.cellXfs.size - raise ArgumentError, "Invalid cellXfs id" unless v < count - @style = v - end - # Tries to work out the width of the longest line in the run # @param [Array] widtharray this array is populated with the widths of each line in the run. # @return [Array] diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb index 1ad297e6..6fbd384e 100644 --- a/lib/axlsx/workbook/worksheet/worksheet.rb +++ b/lib/axlsx/workbook/worksheet/worksheet.rb @@ -305,50 +305,6 @@ module Axlsx @sheet_pr ||= SheetPr.new self end - # Indicates if gridlines should be shown in the sheet. - # This is true by default. - # @return [Boolean] - # @deprecated Use SheetView#show_grid_lines= instead. - def show_gridlines=(v) - warn('axlsx::DEPRECIATED: Worksheet#show_gridlines= has been depreciated. This value can be set over SheetView#show_grid_lines=.') - Axlsx::validate_boolean v - sheet_view.show_grid_lines = v - end - - # @see selected - # @return [Boolean] - # @deprecated Use SheetView#tab_selected= instead. - def selected=(v) - warn('axlsx::DEPRECIATED: Worksheet#selected= has been depreciated. This value can be set over SheetView#tab_selected=.') - Axlsx::validate_boolean v - sheet_view.tab_selected = v - end - - # Indicates if the worksheet should show gridlines or not - # @return Boolean - # @deprecated Use SheetView#show_grid_lines instead. - # def show_gridlines - # warn('axlsx::DEPRECIATED: Worksheet#show_gridlines has been depreciated. This value can get over SheetView#show_grid_lines.') - # sheet_view.show_grid_lines - # end - - # Indicates if the worksheet is selected in the workbook - # It is possible to have more than one worksheet selected, however it might cause issues - # in some older versions of excel when using copy and paste. - # @return Boolean - # @deprecated Use SheetView#tab_selected instead. - # def selected - # warn('axlsx::DEPRECIATED: Worksheet#selected has been depreciated. This value can get over SheetView#tab_selected.') - # sheet_view.tab_selected - # end - - # (see #fit_to_page) - # @return [Boolean] - # def fit_to_page=(v) - # warn('axlsx::DEPRECIATED: Worksheet#fit_to_page has been depreciated. This value will automatically be set for you when you use PageSetup#fit_to.') - # fit_to_page? - # end - # The name of the worksheet # The name of a worksheet must be unique in the workbook, and must not exceed 31 characters # @param [String] name diff --git a/test/drawing/tc_pic.rb b/test/drawing/tc_pic.rb index 2c58fcfd..67e6c416 100644 --- a/test/drawing/tc_pic.rb +++ b/test/drawing/tc_pic.rb @@ -9,10 +9,7 @@ class TestPic < Test::Unit::TestCase @test_img_png = File.dirname(__FILE__) + "/../../examples/image1.png" @test_img_gif = File.dirname(__FILE__) + "/../../examples/image1.gif" @test_img_fake = File.dirname(__FILE__) + "/../../examples/image1_fake.jpg" - @image = ws.add_image :image_src => @test_img, :hyperlink => 'https://github.com/randym', :tooltip => "What's up doc?" - end - - def teardown + @image = ws.add_image :image_src => @test_img, :hyperlink => 'https://github.com/randym', :tooltip => "What's up doc?", :opacity => 5 end def test_initialization @@ -40,6 +37,7 @@ class TestPic < Test::Unit::TestCase assert_equal(200, @image.width) end + def test_hyperlink assert_equal(@image.hyperlink.href, "https://github.com/randym") @image.hyperlink = "http://axlsx.blogspot.com" @@ -102,5 +100,4 @@ class TestPic < Test::Unit::TestCase doc = Nokogiri::XML(@image.anchor.drawing.to_xml_string) assert_equal r_id, doc.xpath("//a:blip").first["r:embed"] end - end diff --git a/test/drawing/tc_scatter_series.rb b/test/drawing/tc_scatter_series.rb index 62a338b1..ea3611ab 100644 --- a/test/drawing/tc_scatter_series.rb +++ b/test/drawing/tc_scatter_series.rb @@ -18,28 +18,39 @@ class TestScatterSeries < Test::Unit::TestCase @series = @chart.add_series :xData=>[1,2,4], :yData=>[1,3,9], :title=>"smoothed exponents" assert(@series.smooth, "series is smooth by default on smooth charts") end - + def test_unsmoothed_chart_default_smoothing @chart = @ws.add_chart Axlsx::ScatterChart, :title => "Unsmooth Chart", :scatter_style => :line @series = @chart.add_series :xData=>[1,2,4], :yData=>[1,3,9], :title=>"unsmoothed exponents" assert([email protected], "series is not smooth by default on non-smooth charts") end - + def test_explicit_smoothing @chart = @ws.add_chart Axlsx::ScatterChart, :title => "Unsmooth Chart, Smooth Series", :scatter_style => :line @series = @chart.add_series :xData=>[1,2,4], :yData=>[1,3,9], :title=>"smoothed exponents", :smooth => true assert(@series.smooth, "series is smooth when overriding chart default") - end - + end + def test_explicit_unsmoothing @chart = @ws.add_chart Axlsx::ScatterChart, :title => "Smooth Chart, Unsmooth Series", :scatter_style => :smoothMarker @series = @chart.add_series :xData=>[1,2,4], :yData=>[1,3,9], :title=>"unsmoothed exponents", :smooth => false assert([email protected], "series is not smooth when overriding chart default") - end + end + + def test_ln_width + @chart = @ws.add_chart Axlsx::ScatterChart, :title => "ln width", :scatter_style => :line + @series = @chart.add_series :xData=>[1,2,4], :yData=>[1,3,9], :title=>"ln_width" + @series.ln_width = 12700 + assert_equal(@series.ln_width, 12700, 'line width assigment is allowed') + end def test_to_xml_string + @chart.scatter_style = :line + @series.ln_width = 12700 doc = Nokogiri::XML(@chart.to_xml_string) + assert_equal(12700, @series.ln_width) assert_equal(doc.xpath("//a:srgbClr[@val='#{@series.color}']").size,4) + assert_equal(doc.xpath("//a:ln[@w='#{@series.ln_width}']").length, 1) end end diff --git a/test/drawing/tc_title.rb b/test/drawing/tc_title.rb index 84345150..95e44602 100644 --- a/test/drawing/tc_title.rb +++ b/test/drawing/tc_title.rb @@ -19,6 +19,11 @@ class TestTitle < Test::Unit::TestCase assert(@title.cell == nil) end + def test_initialize_title_size + title = Axlsx::Title.new 'bob', 90 + assert_equal "90", title.text_size + end + def test_text assert_raise(ArgumentError, "text must be a string") { @title.text = 123 } @title.cell = @row.cells.first diff --git a/test/tc_package.rb b/test/tc_package.rb index 58ff53b0..d89f35b0 100644 --- a/test/tc_package.rb +++ b/test/tc_package.rb @@ -7,6 +7,12 @@ class TestPackage < Test::Unit::TestCase ws = @package.workbook.add_worksheet ws.add_row ['Can', 'we', 'build it?'] ws.add_row ['Yes!', 'We', 'can!'] + @rt = Axlsx::RichText.new + @rt.add_run "run 1", :b => true, :i => false + ws.add_row [@rt] + + ws.rows.last.add_cell('b', :type => :text) + ws.outline_level_rows 0, 1 ws.outline_level_columns 0, 1 ws.add_hyperlink :ref => ws.rows.first.cells.last, :location => 'https://github.com/randym' @@ -193,6 +199,7 @@ class TestPackage < Test::Unit::TestCase def test_shared_strings_requires_part @package.use_shared_strings = true + @package.to_stream #ensure all cell_serializer paths are hit p = @package.send(:parts) assert_equal(p.select{ |part| part[:entry] =~/xl\/sharedStrings.xml/}.size, 1, "shared strings table missing") end diff --git a/test/workbook/tc_shared_strings_table.rb b/test/workbook/tc_shared_strings_table.rb index 25a0a0b2..b9347903 100644 --- a/test/workbook/tc_shared_strings_table.rb +++ b/test/workbook/tc_shared_strings_table.rb @@ -4,10 +4,12 @@ class TestSharedStringsTable < Test::Unit::TestCase def setup @p = Axlsx::Package.new :use_shared_strings=>true + ws = @p.workbook.add_worksheet ws.add_row ['a', 1, 'b'] ws.add_row ['b', 1, 'c'] ws.add_row ['c', 1, 'd'] + ws.rows.last.add_cell('b', :type => :text) end def test_workbook_has_shared_strings @@ -16,7 +18,7 @@ class TestSharedStringsTable < Test::Unit::TestCase def test_count sst = @p.workbook.shared_strings - assert_equal(sst.count, 6) + assert_equal(sst.count, 7) end def test_unique_count @@ -54,5 +56,4 @@ class TestSharedStringsTable < Test::Unit::TestCase # assert that the shared string was normalized to remove the control characters assert_not_nil @p.workbook.shared_strings.to_xml_string.index("helloworld") end - end diff --git a/test/workbook/tc_workbook.rb b/test/workbook/tc_workbook.rb index ab400db1..c2f8ebd6 100644 --- a/test/workbook/tc_workbook.rb +++ b/test/workbook/tc_workbook.rb @@ -33,6 +33,12 @@ class TestWorkbook < Test::Unit::TestCase assert_equal(@wb.use_autowidth, false) end + def test_is_reversed + assert_equal(@wb.is_reversed, nil) + assert_raise(ArgumentError) {@wb.is_reversed = 0.1} + assert_nothing_raised {@wb.is_reversed = true } + assert_equal(@wb.use_autowidth, true) + end def test_sheet_by_name_retrieval @wb.add_worksheet(:name=>'foo') @@ -98,7 +104,16 @@ class TestWorkbook < Test::Unit::TestCase end assert(errors.empty?, "error free validation") end - def test_range_requires__valid_sheet + + def test_to_xml_reversed + @wb.is_reversed = true + @wb.add_worksheet(:name => 'first') + second = @wb.add_worksheet(:name => 'second') + doc = Nokogiri::XML(@wb.to_xml_string) + assert_equal second.name, doc.xpath('//xmlns:workbook/xmlns:sheets/*[1]/@name').to_s + end + + def test_range_requires_valid_sheet ws = @wb.add_worksheet :name=>'fish' ws.add_row [1,2,3] ws.add_row [4,5,6] @@ -136,7 +151,7 @@ class TestWorkbook < Test::Unit::TestCase doc = Nokogiri::XML(@wb.to_xml_string) assert_equal pivot_table.cache_definition.rId, doc.xpath("//xmlns:pivotCache").first["r:id"] end - + def test_worksheet_name_is_intact_after_serialized_into_xml sheet = @wb.add_worksheet(:name => '_Example') wb_xml = Nokogiri::XML(@wb.to_xml_string) diff --git a/test/workbook/worksheet/tc_cell.rb b/test/workbook/worksheet/tc_cell.rb index f61a254f..11b54f44 100644 --- a/test/workbook/worksheet/tc_cell.rb +++ b/test/workbook/worksheet/tc_cell.rb @@ -4,6 +4,7 @@ class TestCell < Test::Unit::TestCase def setup p = Axlsx::Package.new + p.use_shared_strings = true @ws = p.workbook.add_worksheet :name=>"hmmm" p.workbook.styles.add_style :sz=>20 @row = @ws.add_row @@ -56,6 +57,19 @@ class TestCell < Test::Unit::TestCase assert_equal('foo', @ws.workbook.defined_names.last.name) end + def test_autowidth + style = @c.row.worksheet.workbook.styles.add_style({:alignment => {:horizontal => :center, :vertical => :center, :wrap_text => true}} ) + @c.style = style + assert_equal(@c.autowidth, 5.5) + end + + def test_time + @c.type = :time + now = DateTime.now + @c.value = now + assert_equal(@c.value, now.to_time) + end + def test_style assert_raise(ArgumentError, "must reject invalid style indexes") { @[email protected] } assert_nothing_raised("must allow valid style index changes") {@c.style=1} diff --git a/test/workbook/worksheet/tc_rich_text_run.rb b/test/workbook/worksheet/tc_rich_text_run.rb index f8b11f6c..e4a5ef80 100644 --- a/test/workbook/worksheet/tc_rich_text_run.rb +++ b/test/workbook/worksheet/tc_rich_text_run.rb @@ -7,7 +7,7 @@ class RichTextRun < Test::Unit::TestCase @p.workbook.styles.add_style :sz => 20 @rtr = Axlsx::RichTextRun.new('hihihi', b: true, i: false) @rtr2 = Axlsx::RichTextRun.new('hihi2hi2', b: false, i: true) - @rt = Axlsx::RichText.new + @rt = Axlsx::RichText.new @rt.runs << @rtr @rt.runs << @rtr2 @row = @ws.add_row [@rt] @@ -19,7 +19,7 @@ class RichTextRun < Test::Unit::TestCase assert_equal(@rtr.b, true) assert_equal(@rtr.i, false) end - + def test_font_size_with_custom_style_and_no_sz @c.style = @c.row.worksheet.workbook.styles.add_style :bg_color => 'FF00FF' sz = @rtr.send(:font_size) @@ -41,12 +41,12 @@ class RichTextRun < Test::Unit::TestCase sz2 = @rtr2.send(:font_size) assert_equal(sz2, 52) end - + def test_rtr_with_sz @rtr.sz = 25 assert_equal(25, @rtr.send(:font_size)) end - + def test_color assert_raise(ArgumentError) { @rtr.color = -1.1 } assert_nothing_raised { @rtr.color = "FF00FF00" } @@ -129,9 +129,9 @@ class RichTextRun < Test::Unit::TestCase end def test_family - assert_raise(ArgumentError) { @c.family = -1.1 } - assert_nothing_raised { @c.family = 5 } - assert_equal(@c.family, 5) + assert_raise(ArgumentError) { @rtr.family = 0 } + assert_nothing_raised { @rtr.family = 1 } + assert_equal(@rtr.family, 1) end def test_b @@ -139,11 +139,11 @@ class RichTextRun < Test::Unit::TestCase assert_nothing_raised { @c.b = false } assert_equal(@c.b, false) end - + def test_multiline_autowidth wrap = @p.workbook.styles.add_style({:alignment => {:wrap_text => true}}) awtr = Axlsx::RichTextRun.new('I\'m bold' + "\n", :b => true) - rt = Axlsx::RichText.new + rt = Axlsx::RichText.new rt.runs << awtr @ws.add_row [rt], :style => wrap ar = [0] @@ -151,7 +151,7 @@ class RichTextRun < Test::Unit::TestCase assert_equal(ar.length, 2) assert_equal(ar.last, 0) end - + def test_to_xml schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD)) doc = Nokogiri::XML(@ws.to_xml_string) @@ -161,7 +161,7 @@ class RichTextRun < Test::Unit::TestCase errors.push error end assert(errors.empty?, "error free validation") - + assert(doc.xpath('//rPr/b[@val=1]')) assert(doc.xpath('//rPr/i[@val=0]')) assert(doc.xpath('//rPr/b[@val=0]')) |
