diff options
| author | Randy Morgan <[email protected]> | 2012-08-02 22:38:48 +0900 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2012-08-02 22:38:48 +0900 |
| commit | 93ba4b05c2e44724cfdf4c8a1352b098ea9c1482 (patch) | |
| tree | 5423ea5da40205bcbd8ce78c3912cad6f199232d | |
| parent | 1431d1bd0851ed29e188ad17c6a9b93c316fe983 (diff) | |
| download | caxlsx-93ba4b05c2e44724cfdf4c8a1352b098ea9c1482.tar.gz caxlsx-93ba4b05c2e44724cfdf4c8a1352b098ea9c1482.zip | |
bring coverage up to 100% for d_lbls and defined names
release prep!
| -rw-r--r-- | lib/axlsx/drawing/d_lbls.rb | 18 | ||||
| -rw-r--r-- | lib/axlsx/drawing/pie_3D_chart.rb | 3 | ||||
| -rw-r--r-- | lib/axlsx/drawing/scatter_chart.rb | 12 | ||||
| -rw-r--r-- | lib/axlsx/workbook/defined_name.rb | 20 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/auto_filter.rb | 6 | ||||
| -rw-r--r-- | test/tc_package.rb | 17 | ||||
| -rw-r--r-- | test/workbook/tc_defined_name.rb | 41 | ||||
| -rw-r--r-- | test/workbook/tc_workbook.rb | 6 |
8 files changed, 87 insertions, 36 deletions
diff --git a/lib/axlsx/drawing/d_lbls.rb b/lib/axlsx/drawing/d_lbls.rb index eb4bfea5..54f054fb 100644 --- a/lib/axlsx/drawing/d_lbls.rb +++ b/lib/axlsx/drawing/d_lbls.rb @@ -2,19 +2,9 @@ module Axlsx # There are more elements in the dLbls spec that allow for # customizations and formatting. For now, I am just implementing the # basics. - # - #<c:dLbls> - #<c:dLblPos val="outEnd"/> - #<c:showLegendKey val="0"/> - #<c:showVal val="0"/> - #<c:showCatName val="1"/> - #<c:showSerName val="0"/> - #<c:showPercent val="1"/> - #<c:showBubbleSize val="0"/> - #<c:showLeaderLines val="1"/> - #</c:dLbls> #The DLbls class manages serialization of data labels + # showLeaderLines and leaderLines are not currently implemented class DLbls # These attributes are all boolean so I'm doing a bit of a hand @@ -91,9 +81,9 @@ module Axlsx def to_xml_string(str = '') validate_attributes_for_chart_type str << '<c:dLbls>' - instance_values.each do |key, value| - next if key == :formula - str << "<c:#{Axlsx::camel(key, false)} val='#{value}' />" + %w(d_lbl_pos show_legend_key show_val show_cat_name show_ser_name show_percent show_bubble_size show_leader_lines).each do |key| + next unless instance_values.keys.include?(key) && instance_values[key] != nil + str << "<c:#{Axlsx::camel(key, false)} val='#{instance_values[key]}' />" end str << '</c:dLbls>' end diff --git a/lib/axlsx/drawing/pie_3D_chart.rb b/lib/axlsx/drawing/pie_3D_chart.rb index 059d4148..da51d08b 100644 --- a/lib/axlsx/drawing/pie_3D_chart.rb +++ b/lib/axlsx/drawing/pie_3D_chart.rb @@ -34,10 +34,11 @@ module Axlsx # @return [String] def to_xml_string(str = '') super(str) do |str_inner| + str_inner << '<c:pie3DChart>' str_inner << '<c:varyColors val="1"/>' @series.each { |ser| ser.to_xml_string(str_inner) } - @d_lbls.to_xml_string(str) if @d_lbls + d_lbls.to_xml_string(str) if @d_lbls str_inner << '</c:pie3DChart>' end end diff --git a/lib/axlsx/drawing/scatter_chart.rb b/lib/axlsx/drawing/scatter_chart.rb index b3cd8c29..92ef9cf2 100644 --- a/lib/axlsx/drawing/scatter_chart.rb +++ b/lib/axlsx/drawing/scatter_chart.rb @@ -50,17 +50,7 @@ module Axlsx str_inner << '<c:scatterStyle val="' << scatterStyle.to_s << '"/>' str_inner << '<c:varyColors val="1"/>' @series.each { |ser| ser.to_xml_string(str_inner) } - if @d_lbls - @d_lbls.to_xml_string(str) - end - str_inner << '<c:dLbls>' - str_inner << '<c:showLegendKey val="0"/>' - str_inner << '<c:showVal val="0"/>' - str_inner << '<c:showCatName val="0"/>' - str_inner << '<c:showSerName val="0"/>' - str_inner << '<c:showPercent val="0"/>' - str_inner << '<c:showBubbleSize val="0"/>' - str_inner << '</c:dLbls>' + d_lbls.to_xml_string(str) if @d_lbls str_inner << '<c:axId val="' << @xValAxId.to_s << '"/>' str_inner << '<c:axId val="' << @yValAxId.to_s << '"/>' str_inner << '</c:scatterChart>' diff --git a/lib/axlsx/workbook/defined_name.rb b/lib/axlsx/workbook/defined_name.rb index da558956..ed4774e8 100644 --- a/lib/axlsx/workbook/defined_name.rb +++ b/lib/axlsx/workbook/defined_name.rb @@ -52,6 +52,7 @@ module Axlsx # creates a new DefinedName. # @param [String] formula - the formula the defined name references # @param [Hash] options - A hash of key/value pairs that will be mapped to this instances attributes. + # # @option [String] name - Specifies the name that appears in the user interface for the defined name. # This attribute is required. # The following built-in names are defined in this SpreadsheetML specification: @@ -70,9 +71,9 @@ module Axlsx # _xlnm.Extract: this defined name refers to the range containing the filtered output # values resulting from applying an advanced filter criteria to a source range. # Miscellaneous - # _xlnm .Consolidate_Area: the defined name refers to a consolidation area. - # _xlnm .Database: the range specified in the defined name is from a database data source. - # _xlnm .Sheet_Title: the defined name refers to a sheet title. + # _xlnm.Consolidate_Area: the defined name refers to a consolidation area. + # _xlnm.Database: the range specified in the defined name is from a database data source. + # _xlnm.Sheet_Title: the defined name refers to a sheet title. # @option [String] comment - A comment to optionally associate with the name # @option [String] custom_menu - The menu text for the defined name # @option [String] description - An optional description for the defined name @@ -106,7 +107,7 @@ module Axlsx @local_sheet_id = value end # string attributes that will be added when this class is evaluated - STRING_ATTRIBUTES = [:short_cut_key, :status_bar, :help, :description, :custom_menu, :comment, :name] + STRING_ATTRIBUTES = [:short_cut_key, :status_bar, :help, :description, :custom_menu, :comment] # boolean attributes that will be added when this class is evaluated BOOLEAN_ATTRIBUTES = [:workbook_parameter, :publish_to_server, :xlm, :vb_proceedure, :function, :hidden] @@ -144,8 +145,19 @@ module Axlsx end } end + + attr_reader :name + # The name of this defined name. Please refer to the class documentation for more information + def name=(value) + Axlsx::RestrictionValidator.validate 'DefinedName.name', %w(_xlnm.Print_Area _xlnm.Print_Titles _xlnm.Criteria _xlnm._FilterDatabase _xlnm.Extract _xlnm.Consolidate_Area _xlnm.Database _xlnm.Sheet_Title), value + @name = value + end + + # The formula this defined name references + attr_reader :formula def to_xml_string(str='') + raise ArgumentError, 'you must specify the name for this defined name. Please read the documentation for Axlsx::DefinedName for more details' unless name str << '<definedName' instance_values.each do |name, value| unless name == 'formula' diff --git a/lib/axlsx/workbook/worksheet/auto_filter.rb b/lib/axlsx/workbook/worksheet/auto_filter.rb index 10873119..f2197c37 100644 --- a/lib/axlsx/workbook/worksheet/auto_filter.rb +++ b/lib/axlsx/workbook/worksheet/auto_filter.rb @@ -6,18 +6,18 @@ module Axlsx raise ArgumentError, 'you must provide a worksheet' unless worksheet.is_a?(Worksheet) @worksheet = worksheet end - + attr_reader :worksheet attr_accessor :range def defined_name return unless range Axlsx.cell_range(range.split(':').collect { |name| worksheet.name_to_cell(name)}) - end + end def to_xml_string(str='') str << "<autoFilter ref='#{range}'></autoFilter>" end - + end end diff --git a/test/tc_package.rb b/test/tc_package.rb index 3b5db288..df56b044 100644 --- a/test/tc_package.rb +++ b/test/tc_package.rb @@ -7,6 +7,7 @@ class TestPackage < Test::Unit::TestCase ws = @package.workbook.add_worksheet ws.add_row ['Can', 'we', 'build it?'] ws.add_row ['Yes!', 'We', 'can!'] + ws.workbook.add_defined_name("#{ws.name}!A1:C2", :name => '_xlnm.Print_Titles', :hidden => true) ws.protect_range('A1:C1') ws.protect_range(ws.rows.last.cells) ws.add_comment :author => 'alice', :text => 'Hi Bob', :ref => 'A12' @@ -28,11 +29,25 @@ class TestPackage < Test::Unit::TestCase ws.add_chart(Axlsx::Pie3DChart, :title => "これは?", :start_at => [0,3]) do |chart| chart.add_series :data=>[1,2,3], :labels=>["a", "b", "c"] + chart.d_lbls.show_val = true + chart.d_lbls.d_lbl_pos = :outEnd + chart.d_lbls.show_percent = true end ws.add_chart(Axlsx::Line3DChart, :title => "axis labels") do |chart| chart.valAxis.title = 'bob' + chart.d_lbls.show_val = true end + + ws.add_chart(Axlsx::Bar3DChart, :title => 'bar chart') do |chart| + chart.add_series :data => [1,4,5], :labels => %w(A B C) + chart.d_lbls.show_percent = true + end + + ws.add_chart(Axlsx::ScatterChart, :title => 'scat man') do |chart| + chart.add_series :xData => [1,2,3,4], :yData => [4,3,2,1] + chart.d_lbls.show_val = true + end @fname = 'axlsx_test_serialization.xlsx' img = File.expand_path('../../examples/image1.jpeg', __FILE__) @@ -122,7 +137,7 @@ class TestPackage < Test::Unit::TestCase #no mystery parts - assert_equal(p.size, 19) + assert_equal(p.size, 21) end diff --git a/test/workbook/tc_defined_name.rb b/test/workbook/tc_defined_name.rb new file mode 100644 index 00000000..51f5a484 --- /dev/null +++ b/test/workbook/tc_defined_name.rb @@ -0,0 +1,41 @@ +require 'tc_helper' + +class TestDefinedNames < Test::Unit::TestCase + def setup + @dn = Axlsx::DefinedName.new('Sheet1!A1:A1') + end + + def test_initialize + assert_equal('Sheet1!A1:A1', @dn.formula) + end + + def test_string_attributes + Axlsx::DefinedName::STRING_ATTRIBUTES.each do |attr| + assert_raise(ArgumentError, 'only strings allowed in string attributes') { @dn.send("#{attr}=", 1) } + assert_nothing_raised { @dn.send("#{attr}=", '_xlnm.Sheet_Title') } + end + end + + def test_boolean_attributes + Axlsx::DefinedName::BOOLEAN_ATTRIBUTES.each do |attr| + assert_raise(ArgumentError, 'only booleanish allowed in string attributes') { @dn.send("#{attr}=", 'foo') } + assert_nothing_raised { @dn.send("#{attr}=", 1) } + end + + end + + def test_local_sheet_id + assert_raise(ArgumentError, 'local_sheet_id must be an unsigned int') { @dn.local_sheet_id = -1 } + assert_nothing_raised { @dn.local_sheet_id = 1 } + end + + def test_to_xml_string + assert_raise(ArgumentError, 'name is required for serialization') { @dn.to_xml_string } + @dn.name = '_xlnm.Print_Titles' + @dn.hidden = true + doc = Nokogiri::XML(@dn.to_xml_string) + assert(doc.xpath("//definedName[@name='_xlnm.Print_Titles']")) + assert(doc.xpath("//definedName[@hidden='true']")) + assert_equal('Sheet1!A1:A1', doc.xpath('//definedName').text) + end +end diff --git a/test/workbook/tc_workbook.rb b/test/workbook/tc_workbook.rb index f6f38778..537f9074 100644 --- a/test/workbook/tc_workbook.rb +++ b/test/workbook/tc_workbook.rb @@ -24,7 +24,10 @@ class TestWorkbook < Test::Unit::TestCase assert_equal(Axlsx::Workbook.date1904, @wb.date1904) end - + def test_add_defined_name + @wb.add_defined_name 'Sheet1!1:1', :name => '_xlnm.Print_Titles', :hidden => true + assert_equal(1, @wb.defined_names.size) + end def test_shared_strings assert_equal(@wb.use_shared_strings, nil) @@ -72,7 +75,6 @@ class TestWorkbook < Test::Unit::TestCase assert(@wb.worksheets.size == 1) end - def test_to_xml_string_defined_names @wb.add_worksheet do |sheet| sheet.add_row [1, "two"] |
