From 38e1638fa715398429797276058f2c18e9e21e9b Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Sat, 16 Mar 2013 11:40:54 +0900 Subject: First run at 'hidden' comments WIP The xml is genrated correcty, but the comment still shows until it is selected once in the excel ui - so I must be missing something. --- examples/example.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'examples') diff --git a/examples/example.rb b/examples/example.rb index c9b5564e..70f003fc 100755 --- a/examples/example.rb +++ b/examples/example.rb @@ -583,6 +583,8 @@ if examples.include? :comments wb.add_worksheet(:name => 'comments') do |sheet| sheet.add_row ['Can we build it?'] sheet.add_comment :ref => 'A1', :author => 'Bob', :text => 'Yes We Can!' + sheet.add_comment :ref => 'A2', :author => 'Bob', :text => 'Yes We Can! - but I dont think you need to know about it!', :visible => false + end end -- cgit v1.2.3 From ae75ef360e3e0da253188d408a3cbc81ed3897e6 Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Sun, 17 Mar 2013 15:38:55 +0900 Subject: Fixed LineChart and refactored chart axes management --- examples/example.rb | 11 +++++++- lib/axlsx/drawing/axes.rb | 40 ++++++++++++++++++++++++++++ lib/axlsx/drawing/axis.rb | 27 ++++++++++--------- lib/axlsx/drawing/bar_3D_chart.rb | 25 +++++++++--------- lib/axlsx/drawing/cat_axis.rb | 12 ++------- lib/axlsx/drawing/d_lbls.rb | 2 +- lib/axlsx/drawing/drawing.rb | 1 + lib/axlsx/drawing/line_3D_chart.rb | 54 ++++++++++++++++++++------------------ lib/axlsx/drawing/line_chart.rb | 51 +++++++++++++++++++---------------- lib/axlsx/drawing/line_series.rb | 1 + lib/axlsx/drawing/scatter_chart.rb | 41 +++++++++++++++++------------ lib/axlsx/drawing/ser_axis.rb | 31 +++++++++++----------- lib/axlsx/drawing/val_axis.rb | 27 ++++++++++--------- test/drawing/tc_axis.rb | 32 +++++++++++----------- test/drawing/tc_cat_axis.rb | 18 ++++++------- test/drawing/tc_line_chart.rb | 5 ++-- test/drawing/tc_ser_axis.rb | 25 +++++++++--------- test/drawing/tc_val_axis.rb | 12 ++++----- 18 files changed, 236 insertions(+), 179 deletions(-) create mode 100644 lib/axlsx/drawing/axes.rb (limited to 'examples') diff --git a/examples/example.rb b/examples/example.rb index 70f003fc..87814b97 100755 --- a/examples/example.rb +++ b/examples/example.rb @@ -455,7 +455,7 @@ if examples.include? :line_chart 4.times do sheet.add_row [ rand(24)+1, rand(24)+1] end - sheet.add_chart(Axlsx::Line3DChart, :title => "Simple Line Chart", :rotX => 30, :rotY => 20) do |chart| + sheet.add_chart(Axlsx::Line3DChart, :title => "Simple 3D Line Chart", :rotX => 30, :rotY => 20) do |chart| chart.start_at 0, 5 chart.end_at 10, 20 chart.add_series :data => sheet["A3:A6"], :title => sheet["A2"] @@ -463,6 +463,15 @@ if examples.include? :line_chart chart.catAxis.title = 'X Axis' chart.valAxis.title = 'Y Axis' end + sheet.add_chart(Axlsx::LineChart, :title => "Simple Line Chart", :rotX => 30, :rotY => 20) do |chart| + chart.start_at 0, 21 + chart.end_at 10, 41 + chart.add_series :data => sheet["A3:A6"], :title => sheet["A2"] + chart.add_series :data => sheet["B3:B6"], :title => sheet["B2"] + chart.catAxis.title = 'X Axis' + chart.valAxis.title = 'Y Axis' + end + end end #``` diff --git a/lib/axlsx/drawing/axes.rb b/lib/axlsx/drawing/axes.rb new file mode 100644 index 00000000..99565cb6 --- /dev/null +++ b/lib/axlsx/drawing/axes.rb @@ -0,0 +1,40 @@ +module Axlsx + + class Axes + + def initialize(options={}) + options.each do |name, axis_class| + add_axis(name, axis_class) + end + end + + def [](name) + axes.assoc(name)[1] + end + + def to_xml_string(str = '', options = {}) + if options[:ids] + axes.inject(str) { |string, axis| string << '' } + else + axes.each { |axis| axis[1].to_xml_string(str) } + end + end + + def add_axis(name, axis_class) + axis = axis_class.new + set_cross_axis(axis) + axes << [name, axis] + end + + private + + def axes + @axes ||= [] + end + + def set_cross_axis(axis) + axes.first[1].cross_axis = axis if axes.size == 1 + axis.cross_axis = axes.first[1] unless axes.empty? + end + end +end diff --git a/lib/axlsx/drawing/axis.rb b/lib/axlsx/drawing/axis.rb index 1b55bece..16c087a2 100644 --- a/lib/axlsx/drawing/axis.rb +++ b/lib/axlsx/drawing/axis.rb @@ -7,17 +7,13 @@ module Axlsx include Axlsx::OptionsParser # Creates an Axis object - # @param [Integer] ax_id the id of this axis - # @param [Integer] cross_ax the id of the perpendicular axis + # @param [Integer] cross_axis the perpendicular axis # @option options [Symbol] ax_pos # @option options [Symbol] crosses # @option options [Symbol] tick_lbl_pos # @raise [ArgumentError] If axi_id or cross_ax are not unsigned integers - def initialize(ax_id, cross_ax, options={}) - Axlsx::validate_unsigned_int(ax_id) - Axlsx::validate_unsigned_int(cross_ax) - @ax_id = ax_id - @cross_ax = cross_ax + def initialize(options={}) + @id = rand(8 ** 8) @format_code = "General" @delete = @label_rotation = 0 @scaling = Scaling.new(:orientation=>:minMax) @@ -37,13 +33,13 @@ module Axlsx # the id of the axis. # @return [Integer] - attr_reader :ax_id - alias :axID :ax_id + attr_reader :id + alias :axID :id # The perpendicular axis # @return [Integer] - attr_reader :cross_ax - alias :crossAx :cross_ax + attr_reader :cross_axis + alias :crossAx :cross_axis # The scaling of the axis # @see Scaling @@ -95,6 +91,11 @@ module Axlsx @color = color_rgb end + def cross_axis=(axis) + DataTypeValidator.validate "#{self.class}.cross_axis", [Axis], axis + @cross_axis = axis + end + # The position of the axis # must be one of [:l, :r, :t, :b] def ax_pos=(v) RestrictionValidator.validate "#{self.class}.ax_pos", [:l, :r, :b, :t], v; @ax_pos = v; end @@ -147,7 +148,7 @@ module Axlsx # @param [String] str # @return [String] def to_xml_string(str = '') - str << '' + str << '' @scaling.to_xml_string str str << '' str << '' @@ -175,7 +176,7 @@ module Axlsx end # some potential value in implementing this in full. Very detailed! str << '' - str << '' + str << '' str << '' end diff --git a/lib/axlsx/drawing/bar_3D_chart.rb b/lib/axlsx/drawing/bar_3D_chart.rb index 6f69f703..0e2b37bb 100644 --- a/lib/axlsx/drawing/bar_3D_chart.rb +++ b/lib/axlsx/drawing/bar_3D_chart.rb @@ -10,12 +10,16 @@ module Axlsx # the category axis # @return [CatAxis] - attr_reader :cat_axis + def cat_axis + axes[:cat_axis] + end alias :catAxis :cat_axis # the value axis # @return [ValAxis] - attr_reader :val_axis + def val_axis + axes[:val_axis] + end alias :valAxis :val_axis # The direction of the bars in the chart @@ -75,10 +79,6 @@ module Axlsx def initialize(frame, options={}) @vary_colors = true @gap_width, @gap_depth, @shape = nil, nil, nil - @cat_ax_id = rand(8 ** 8) - @val_ax_id = rand(8 ** 8) - @cat_axis = CatAxis.new(@cat_ax_id, @val_ax_id) - @val_axis = ValAxis.new(@val_ax_id, @cat_ax_id, :tick_lbl_pos => :low, :ax_pos => :l) super(frame, options) @series_type = BarSeries @view_3D = View3D.new({:r_ang_ax=>1}.merge(options)) @@ -131,17 +131,18 @@ module Axlsx str_inner << '' str_inner << '' @series.each { |ser| ser.to_xml_string(str_inner) } - @d_lbls.to_xml_string(str) if @d_lbls + @d_lbls.to_xml_string(str_inner) if @d_lbls str_inner << '' unless @gap_width.nil? str_inner << '' unless @gap_depth.nil? str_inner << '' unless @shape.nil? - str_inner << '' - str_inner << '' - str_inner << '' + axes.to_xml_string(str_inner, :ids => true) str_inner << '' - @cat_axis.to_xml_string str_inner - @val_axis.to_xml_string str_inner + axes.to_xml_string(str_inner) end end + + def axes + @axes ||= Axes.new(:val_axis => ValAxis, :cat_axis => CatAxis) + end end end diff --git a/lib/axlsx/drawing/cat_axis.rb b/lib/axlsx/drawing/cat_axis.rb index ba392ce6..f32c23c9 100644 --- a/lib/axlsx/drawing/cat_axis.rb +++ b/lib/axlsx/drawing/cat_axis.rb @@ -4,23 +4,15 @@ module Axlsx class CatAxis < Axis # Creates a new CatAxis object - # @param [Integer] ax_id the id of this axis. Inherited - # @param [Integer] cross_ax the id of the perpendicular axis. Inherited - # @option options [Symbol] ax_pos. Inherited - # @option options [Symbol] tick_lbl_pos. Inherited - # @option options [Symbol] crosses. Inherited - # @option options [Boolean] auto - # @option options [Symbol] lbl_algn - # @option options [Integer] lbl_offset # @option options [Integer] tick_lbl_skip # @option options [Integer] tick_mark_skip - def initialize(ax_id, cross_ax, options={}) + def initialize(options={}) @tick_lbl_skip = 1 @tick_mark_skip = 1 self.auto = 1 self.lbl_algn = :ctr self.lbl_offset = "100" - super(ax_id, cross_ax, options) + super(options) end # From the docs: This element specifies that this axis is a date or text axis based on the data that is used for the axis labels, not a specific choice. diff --git a/lib/axlsx/drawing/d_lbls.rb b/lib/axlsx/drawing/d_lbls.rb index 3685e7d2..74c01350 100644 --- a/lib/axlsx/drawing/d_lbls.rb +++ b/lib/axlsx/drawing/d_lbls.rb @@ -10,7 +10,7 @@ module Axlsx include Axlsx::OptionsParser # creates a new DLbls object def initialize(chart_type, options={}) - raise ArgumentError, 'chart_type must inherit from Chart' unless chart_type.superclass == Chart + raise ArgumentError, 'chart_type must inherit from Chart' unless [Chart, LineChart].include?(chart_type.superclass) @chart_type = chart_type initialize_defaults parse_options options diff --git a/lib/axlsx/drawing/drawing.rb b/lib/axlsx/drawing/drawing.rb index 7567dea1..58f0f81e 100644 --- a/lib/axlsx/drawing/drawing.rb +++ b/lib/axlsx/drawing/drawing.rb @@ -22,6 +22,7 @@ module Axlsx require 'axlsx/drawing/ser_axis.rb' require 'axlsx/drawing/cat_axis.rb' require 'axlsx/drawing/val_axis.rb' + require 'axlsx/drawing/axes.rb' require 'axlsx/drawing/marker.rb' diff --git a/lib/axlsx/drawing/line_3D_chart.rb b/lib/axlsx/drawing/line_3D_chart.rb index 627cfe83..183f3250 100644 --- a/lib/axlsx/drawing/line_3D_chart.rb +++ b/lib/axlsx/drawing/line_3D_chart.rb @@ -11,7 +11,7 @@ module Axlsx # ws = p.workbook.add_worksheet # ws.add_row ["This is a chart with no data in the sheet"] # - # chart = ws.add_chart(Axlsx::Line3DChart, :start_at=> [0,1], :end_at=>[0,6], :title=>"Most Popular Pets") + # chart = ws.add_chart(Axlsx::Line3DChart, :start_at=> [0,1], :end_at=>[0,6], :t#itle=>"Most Popular Pets") # chart.add_series :data => [1, 9, 10], :labels => ["Slimy Reptiles", "Fuzzy Bunnies", "Rottweiler"] # # @see Worksheet#add_chart @@ -19,48 +19,50 @@ module Axlsx # @see Chart#add_series # @see Series # @see Package#serialize - class Line3DChart < LineChart + class Line3DChart < Axlsx::LineChart # space between bar or column clusters, as a percentage of the bar or column width. # @return [String] - attr_reader :gapDepth + attr_reader :gap_depth + alias :gapDepth :gap_depth # validation regex for gap amount percent GAP_AMOUNT_PERCENT = /0*(([0-9])|([1-9][0-9])|([1-4][0-9][0-9])|500)%/ + # the category axis + # @return [Axis] + def ser_axis + axes[:ser_axis] + end + alias :serAxis :ser_axis + # Creates a new line chart object - # @param [GraphicFrame] frame The workbook that owns this chart. - # @option options [Cell, String] title - # @option options [Boolean] show_legend - # @option options [Symbol] grouping - # @option options [String] gapDepth - # @option options [Integer] rotX - # @option options [String] hPercent - # @option options [Integer] rotY - # @option options [String] depthPercent - # @option options [Boolean] rAngAx - # @option options [Integer] perspective + # @option options [String] gap_depth # @see Chart + # @see lineChart # @see View3D def initialize(frame, options={}) - @gapDepth = nil + @gap_depth = nil + @view_3D = View3D.new({:r_ang_ax=>1}.merge(options)) super(frame, options) - @view_3D = View3D.new({:perspective=>30}.merge(options)) + axes.add_axis :ser_axis, SerAxis end + # @see gapDepth - def gapDepth=(v) - RegexValidator.validate "Bar3DChart.gapWidth", GAP_AMOUNT_PERCENT, v - @gapDepth=(v) + def gap_depth=(v) + RegexValidator.validate "Line3DChart.gapWidth", GAP_AMOUNT_PERCENT, v + @gap_depth=(v) end + alias :gapDepth= :gap_depth= - # Serializes the object - # @param [String] str - # @return [String] - def to_xml_string(str = '') - super(str) do |str_inner| - str_inner << '' unless @gapDepth.nil? + # Serializes the object + # @param [String] str + # @return [String] + def to_xml_string(str = '') + super(str) do |str_inner| + str_inner << '' unless @gap_depth.nil? + end end - end end end diff --git a/lib/axlsx/drawing/line_chart.rb b/lib/axlsx/drawing/line_chart.rb index 7bf925e6..afe6154e 100644 --- a/lib/axlsx/drawing/line_chart.rb +++ b/lib/axlsx/drawing/line_chart.rb @@ -23,17 +23,19 @@ module Axlsx # the category axis # @return [CatAxis] - attr_reader :catAxis + def cat_axis + axes[:cat_axis] + end + alias :catAxis :cat_axis # the category axis # @return [ValAxis] - attr_reader :valAxis - - # the category axis - # @return [Axis] - attr_reader :serAxis + def val_axis + axes[:val_axis] + end + alias :valAxis :val_axis - # must be one of [:percentStacked, :clustered, :standard, :stacked] + # must be one of [:percentStacked, :clustered, :standard, :stacked] # @return [Symbol] attr_reader :grouping @@ -46,12 +48,6 @@ module Axlsx def initialize(frame, options={}) @vary_colors = false @grouping = :standard - @catAxId = rand(8 ** 8) - @valAxId = rand(8 ** 8) - @serAxId = rand(8 ** 8) - @catAxis = CatAxis.new(@catAxId, @valAxId) - @valAxis = ValAxis.new(@valAxId, @catAxId) - @serAxis = SerAxis.new(@serAxId, @valAxId) super(frame, options) @series_type = LineSeries @d_lbls = nil @@ -59,29 +55,38 @@ module Axlsx # @see grouping def grouping=(v) - RestrictionValidator.validate "Bar3DChart.grouping", [:percentStacked, :standard, :stacked], v + RestrictionValidator.validate "LineChart.grouping", [:percentStacked, :standard, :stacked], v @grouping = v end + def node_name + path = self.class.to_s + if i = path.rindex('::') + path = path[(i+2)..-1] + end + path[0] = path[0].chr.downcase + path + end + # Serializes the object # @param [String] str # @return [String] def to_xml_string(str = '') super(str) do |str_inner| - str_inner << "" + str_inner << "" str_inner << '' str_inner << '' @series.each { |ser| ser.to_xml_string(str_inner) } - @d_lbls.to_xml_string(str) if @d_lbls + @d_lbls.to_xml_string(str_inner) if @d_lbls yield str_inner if block_given? - str_inner << '' - str_inner << '' - str_inner << '' - str_inner << "" - @catAxis.to_xml_string str_inner - @valAxis.to_xml_string str_inner - @serAxis.to_xml_string str_inner + axes.to_xml_string(str_inner, :ids => true) + str_inner << "" + axes.to_xml_string(str_inner) end end + + def axes + @axes ||= Axes.new(:cat_axis => CatAxis, :val_axis => ValAxis) + end end end diff --git a/lib/axlsx/drawing/line_series.rb b/lib/axlsx/drawing/line_series.rb index f70bdb49..467dcc2d 100644 --- a/lib/axlsx/drawing/line_series.rb +++ b/lib/axlsx/drawing/line_series.rb @@ -28,6 +28,7 @@ module Axlsx # @option options [Array, SimpleTypedList] labels # @param [Chart] chart def initialize(chart, options={}) + @show_marker = false @labels, @data = nil, nil super(chart, options) @labels = AxDataSource.new(:data => options[:labels]) unless options[:labels].nil? diff --git a/lib/axlsx/drawing/scatter_chart.rb b/lib/axlsx/drawing/scatter_chart.rb index 2b801642..f069e346 100644 --- a/lib/axlsx/drawing/scatter_chart.rb +++ b/lib/axlsx/drawing/scatter_chart.rb @@ -12,35 +12,40 @@ module Axlsx # The Style for the scatter chart # must be one of :none | :line | :lineMarker | :marker | :smooth | :smoothMarker # return [Symbol] - attr_reader :scatterStyle + attr_reader :scatter_style + alias :scatterStyle :scatter_style # the x value axis # @return [ValAxis] - attr_reader :xValAxis + def x_val_axis + axes[:x_val_axis] + end + alias :xValAxis :x_val_axis # the y value axis # @return [ValAxis] - attr_reader :yValAxis + def y_val_axis + axes[:x_val_axis] + end + alias :yValAxis :y_val_axis # Creates a new scatter chart def initialize(frame, options={}) @vary_colors = 0 - @scatterStyle = :lineMarker - @xValAxId = rand(8 ** 8) - @yValAxId = rand(8 ** 8) - @xValAxis = ValAxis.new(@xValAxId, @yValAxId) - @yValAxis = ValAxis.new(@yValAxId, @xValAxId) - super(frame, options) + @scatter_style = :lineMarker + + super(frame, options) @series_type = ScatterSeries @d_lbls = nil parse_options options end # see #scatterStyle - def scatterStyle=(v) + def scatter_style=(v) Axlsx.validate_scatter_style(v) - @scatterStyle = v + @scatter_style = v end + alias :scatterStyle= :scatter_style= # Serializes the object # @param [String] str @@ -48,17 +53,19 @@ module Axlsx def to_xml_string(str = '') super(str) do |str_inner| str_inner << '' - str_inner << '' + str_inner << '' str_inner << '' @series.each { |ser| ser.to_xml_string(str_inner) } - d_lbls.to_xml_string(str) if @d_lbls - str_inner << '' - str_inner << '' + d_lbls.to_xml_string(str_inner) if @d_lbls + axes.to_xml_string(str_inner, :ids => true) str_inner << '' - @xValAxis.to_xml_string str_inner - @yValAxis.to_xml_string str_inner + axes.to_xml_string(str_inner) end str end + + def axes + @axes ||= Axes.new(:x_val_axis => ValAxis, :y_val_axis => ValAxis) + end end end diff --git a/lib/axlsx/drawing/ser_axis.rb b/lib/axlsx/drawing/ser_axis.rb index 00b04989..54e2c60e 100644 --- a/lib/axlsx/drawing/ser_axis.rb +++ b/lib/axlsx/drawing/ser_axis.rb @@ -5,30 +5,29 @@ module Axlsx # The number of tick lables to skip between labels # @return [Integer] - attr_reader :tickLblSkip + attr_reader :tick_lbl_skip + alias :tickLblSkip :tick_lbl_skip # The number of tickmarks to be skipped before the next one is rendered. # @return [Boolean] - attr_reader :tickMarkSkip + attr_reader :tick_mark_skip + alias :tickMarkSkip :tick_mark_skip # Creates a new SerAxis object - # @param [Integer] axId the id of this axis. Inherited - # @param [Integer] crossAx the id of the perpendicular axis. Inherited - # @option options [Symbol] axPos. Inherited - # @option options [Symbol] tickLblPos. Inherited - # @option options [Symbol] crosses. Inherited - # @option options [Integer] tickLblSkip - # @option options [Integer] tickMarkSkip - def initialize(axId, crossAx, options={}) - @tickLblSkip, @tickMarkSkip = 1, 1 - super(axId, crossAx, options) + # @option options [Integer] tick_lbl_skip + # @option options [Integer] tick_mark_skip + def initialize(options={}) + @tick_lbl_skip, @tick_mark_skip = 1, 1 + super(options) end # @see tickLblSkip - def tickLblSkip=(v) Axlsx::validate_unsigned_int(v); @tickLblSkip = v; end + def tick_lbl_skip=(v) Axlsx::validate_unsigned_int(v); @tick_lbl_skip = v; end + alias :tickLblSkip= :tick_lbl_skip= # @see tickMarkSkip - def tickMarkSkip=(v) Axlsx::validate_unsigned_int(v); @tickMarkSkip = v; end + def tick_mark_skip=(v) Axlsx::validate_unsigned_int(v); @tick_mark_skip = v; end + alias :tickMarkSkip= :tick_mark_skip= # Serializes the object # @param [String] str @@ -36,8 +35,8 @@ module Axlsx def to_xml_string(str = '') str << '' super(str) - str << '' unless @tickLblSkip.nil? - str << '' unless @tickMarkSkip.nil? + str << '' unless @tick_lbl_skip.nil? + str << '' unless @tick_mark_skip.nil? str << '' end end diff --git a/lib/axlsx/drawing/val_axis.rb b/lib/axlsx/drawing/val_axis.rb index 6e55c8ea..0e7a0800 100644 --- a/lib/axlsx/drawing/val_axis.rb +++ b/lib/axlsx/drawing/val_axis.rb @@ -6,21 +6,22 @@ module Axlsx # This element specifies how the value axis crosses the category axis. # must be one of [:between, :midCat] # @return [Symbol] - attr_reader :crossBetween + attr_reader :cross_between + alias :crossBetween :cross_between # Creates a new ValAxis object - # @param [Integer] axId the id of this axis - # @param [Integer] crossAx the id of the perpendicular axis - # @option options [Symbol] axPos - # @option options [Symbol] tickLblPos - # @option options [Symbol] crosses - # @option options [Symbol] crossesBetween - def initialize(axId, crossAx, options={}) - self.crossBetween = :between - super(axId, crossAx, options) + # @option options [Symbol] crosses_between + def initialize(options={}) + self.cross_between = :between + super(options) end - # @see crossBetween - def crossBetween=(v) RestrictionValidator.validate "ValAxis.crossBetween", [:between, :midCat], v; @crossBetween = v; end + + # @see cross_between + def cross_between=(v) + RestrictionValidator.validate "ValAxis.cross_between", [:between, :midCat], v + @cross_between = v + end + alias :crossBetween= :cross_between= # Serializes the object # @param [String] str @@ -28,7 +29,7 @@ module Axlsx def to_xml_string(str = '') str << '' super(str) - str << '' + str << '' str << '' end diff --git a/test/drawing/tc_axis.rb b/test/drawing/tc_axis.rb index 807655d7..3546f786 100644 --- a/test/drawing/tc_axis.rb +++ b/test/drawing/tc_axis.rb @@ -2,25 +2,22 @@ require 'tc_helper.rb' class TestAxis < Test::Unit::TestCase def setup - - @axis = Axlsx::Axis.new 12345, 54321, :gridlines => false, :title => 'Foo' + @axis = Axlsx::Axis.new :gridlines => false, :title => 'Foo' end - def teardown - end def test_initialization - assert_equal(@axis.axPos, :b, "axis position default incorrect") - assert_equal(@axis.tickLblPos, :nextTo, "tick label position default incorrect") - assert_equal(@axis.tickLblPos, :nextTo, "tick label position default incorrect") + assert_equal(@axis.ax_pos, :b, "axis position default incorrect") + assert_equal(@axis.tick_lbl_pos, :nextTo, "tick label position default incorrect") + assert_equal(@axis.tick_lbl_pos, :nextTo, "tick label position default incorrect") assert_equal(@axis.crosses, :autoZero, "tick label position default incorrect") assert(@axis.scaling.is_a?(Axlsx::Scaling) && @axis.scaling.orientation == :minMax, "scaling default incorrect") - assert_raise(ArgumentError) { Axlsx::Axis.new( -1234, 'abcd') } assert_equal('Foo', @axis.title.text) end def test_color @axis.color = "00FF00" + @axis.cross_axis = Axlsx::CatAxis.new str = '' str << '' doc = Nokogiri::XML(@axis.to_xml_string(str)) @@ -35,15 +32,15 @@ class TestAxis < Test::Unit::TestCase sheet.add_row ['cat', 7, 9, 10] sheet.add_chart(Axlsx::Line3DChart) do |chart| chart.add_series :data => sheet['B2:D2'], :labels => sheet['B1'] - chart.valAxis.title = sheet['A1'] - assert_equal('battle victories', chart.valAxis.title.text) + chart.val_axis.title = sheet['A1'] + assert_equal('battle victories', chart.val_axis.title.text) end end end def test_axis_position - assert_raise(ArgumentError, "requires valid axis position") { @axis.axPos = :nowhere } - assert_nothing_raised("accepts valid axis position") { @axis.axPos = :r } + assert_raise(ArgumentError, "requires valid axis position") { @axis.ax_pos = :nowhere } + assert_nothing_raised("accepts valid axis position") { @axis.ax_pos = :r } end def test_label_rotation @@ -55,13 +52,13 @@ class TestAxis < Test::Unit::TestCase end def test_tick_label_position - assert_raise(ArgumentError, "requires valid tick label position") { @axis.tickLblPos = :nowhere } - assert_nothing_raised("accepts valid tick label position") { @axis.tickLblPos = :high } + assert_raise(ArgumentError, "requires valid tick label position") { @axis.tick_lbl_pos = :nowhere } + assert_nothing_raised("accepts valid tick label position") { @axis.tick_lbl_pos = :high } end def test_format_code - assert_raise(ArgumentError, "requires valid format code") { @axis.format_code = 1 } - assert_nothing_raised("accepts valid format code") { @axis.tickLblPos = :high } + assert_raise(ArgumentError, "requires valid format code") { @axis.format_code = :high } + assert_nothing_raised("accepts valid format code") { @axis.format_code = "00.##" } end def test_crosses @@ -75,12 +72,13 @@ class TestAxis < Test::Unit::TestCase end def test_to_xml_string + @axis.cross_axis = Axlsx::CatAxis.new str = '' str << '' doc = Nokogiri::XML(@axis.to_xml_string(str)) assert(doc.xpath('//a:noFill')) assert(doc.xpath("//c:crosses[@val='#{@axis.crosses.to_s}']")) - assert(doc.xpath("//c:crossAx[@val='#{@axis.crossAx.to_s}']")) + assert(doc.xpath("//c:crossAx[@val='#{@axis.cross_axis.to_s}']")) assert(doc.xpath("//a:bodyPr[@rot='#{@axis.label_rotation.to_s}']")) assert(doc.xpath("//a:t[text()='Foo']")) end diff --git a/test/drawing/tc_cat_axis.rb b/test/drawing/tc_cat_axis.rb index 6529b45d..ac690336 100644 --- a/test/drawing/tc_cat_axis.rb +++ b/test/drawing/tc_cat_axis.rb @@ -2,15 +2,15 @@ require 'tc_helper.rb' class TestCatAxis < Test::Unit::TestCase def setup - @axis = Axlsx::CatAxis.new 12345, 54321 + @axis = Axlsx::CatAxis.new end def teardown end def test_initialization assert_equal(@axis.auto, 1, "axis auto default incorrect") - assert_equal(@axis.lblAlgn, :ctr, "label align default incorrect") - assert_equal(@axis.lblOffset, "100", "label offset default incorrect") + assert_equal(@axis.lbl_algn, :ctr, "label align default incorrect") + assert_equal(@axis.lbl_offset, "100", "label offset default incorrect") end def test_auto @@ -18,14 +18,14 @@ class TestCatAxis < Test::Unit::TestCase assert_nothing_raised("accepts valid auto") { @axis.auto = false } end - def test_lblAlgn - assert_raise(ArgumentError, "requires valid label alignment") { @axis.lblAlgn = :nowhere } - assert_nothing_raised("accepts valid label alignment") { @axis.lblAlgn = :r } + def test_lbl_algn + assert_raise(ArgumentError, "requires valid label alignment") { @axis.lbl_algn = :nowhere } + assert_nothing_raised("accepts valid label alignment") { @axis.lbl_algn = :r } end - def test_lblOffset - assert_raise(ArgumentError, "requires valid label offset") { @axis.lblOffset = 'foo' } - assert_nothing_raised("accepts valid label offset") { @axis.lblOffset = "20" } + def test_lbl_offset + assert_raise(ArgumentError, "requires valid label offset") { @axis.lbl_offset = 'foo' } + assert_nothing_raised("accepts valid label offset") { @axis.lbl_offset = "20" } end end diff --git a/test/drawing/tc_line_chart.rb b/test/drawing/tc_line_chart.rb index 07e23702..83ebef97 100644 --- a/test/drawing/tc_line_chart.rb +++ b/test/drawing/tc_line_chart.rb @@ -15,9 +15,8 @@ class TestLineChart < Test::Unit::TestCase def test_initialization assert_equal(@chart.grouping, :standard, "grouping defualt incorrect") assert_equal(@chart.series_type, Axlsx::LineSeries, "series type incorrect") - assert(@chart.catAxis.is_a?(Axlsx::CatAxis), "category axis not created") - assert(@chart.valAxis.is_a?(Axlsx::ValAxis), "value access not created") - assert(@chart.serAxis.is_a?(Axlsx::SerAxis), "value access not created") + assert(@chart.cat_axis.is_a?(Axlsx::CatAxis), "category axis not created") + assert(@chart.val_axis.is_a?(Axlsx::ValAxis), "value access not created") end def test_grouping diff --git a/test/drawing/tc_ser_axis.rb b/test/drawing/tc_ser_axis.rb index 2f30ceb8..7febafce 100644 --- a/test/drawing/tc_ser_axis.rb +++ b/test/drawing/tc_ser_axis.rb @@ -2,29 +2,30 @@ require 'tc_helper.rb' class TestSerAxis < Test::Unit::TestCase def setup - @axis = Axlsx::SerAxis.new 12345, 54321 + @axis = Axlsx::SerAxis.new end + def teardown end def test_options - a = Axlsx::SerAxis.new 12345, 54321, :tickLblSkip => 9, :tickMarkSkip => 7 - assert_equal(a.tickLblSkip, 9) - assert_equal(a.tickMarkSkip, 7) + a = Axlsx::SerAxis.new(:tick_lbl_skip => 9, :tick_mark_skip => 7) + assert_equal(a.tick_lbl_skip, 9) + assert_equal(a.tick_mark_skip, 7) end - def test_tickLblSkip - assert_raise(ArgumentError, "requires valid tickLblSkip") { @axis.tickLblSkip = -1 } - assert_nothing_raised("accepts valid tickLblSkip") { @axis.tickLblSkip = 1 } - assert_equal(@axis.tickLblSkip, 1) + def test_tick_lbl_skip + assert_raise(ArgumentError, "requires valid tick_lbl_skip") { @axis.tick_lbl_skip = -1 } + assert_nothing_raised("accepts valid tick_lbl_skip") { @axis.tick_lbl_skip = 1 } + assert_equal(@axis.tick_lbl_skip, 1) end - def test_tickMarkSkip - assert_raise(ArgumentError, "requires valid tickMarkSkip") { @axis.tickMarkSkip = :my_eyes } - assert_nothing_raised("accepts valid tickMarkSkip") { @axis.tickMarkSkip = 2 } - assert_equal(@axis.tickMarkSkip, 2) + def test_tick_mark_skip + assert_raise(ArgumentError, "requires valid tick_mark_skip") { @axis.tick_mark_skip = :my_eyes } + assert_nothing_raised("accepts valid tick_mark_skip") { @axis.tick_mark_skip = 2 } + assert_equal(@axis.tick_mark_skip, 2) end end diff --git a/test/drawing/tc_val_axis.rb b/test/drawing/tc_val_axis.rb index f3f55421..aa1cb23a 100644 --- a/test/drawing/tc_val_axis.rb +++ b/test/drawing/tc_val_axis.rb @@ -2,23 +2,23 @@ require 'tc_helper.rb' class TestValAxis < Test::Unit::TestCase def setup - @axis = Axlsx::ValAxis.new 12345, 54321 + @axis = Axlsx::ValAxis.new end def teardown end def test_initialization - assert_equal(@axis.crossBetween, :between, "axis crossBetween default incorrect") + assert_equal(@axis.cross_between, :between, "axis crossBetween default incorrect") end def test_options - a = Axlsx::ValAxis.new 2345, 4321, :crossBetween => :midCat - assert_equal(a.crossBetween, :midCat) + a = Axlsx::ValAxis.new(:cross_between => :midCat) + assert_equal(:midCat, a.cross_between) end def test_crossBetween - assert_raise(ArgumentError, "requires valid crossBetween") { @axis.crossBetween = :my_eyes } - assert_nothing_raised("accepts valid crossBetween") { @axis.crossBetween = :midCat } + assert_raise(ArgumentError, "requires valid crossBetween") { @axis.cross_between = :my_eyes } + assert_nothing_raised("accepts valid crossBetween") { @axis.cross_between = :midCat } end end -- cgit v1.2.3 From e56b36a74fadf2f1c1334ade8388c1e046dcad63 Mon Sep 17 00:00:00 2001 From: Noel Peden Date: Thu, 4 Apr 2013 10:29:54 -0700 Subject: Added support for specifying between/notBetween formula in an array. --- examples/conditional_formatting/example_conditional_formatting.rb | 6 ++++-- examples/example.rb | 6 ++++-- lib/axlsx/workbook/worksheet/conditional_formatting_rule.rb | 6 +++--- test/workbook/worksheet/tc_conditional_formatting.rb | 7 +++++++ 4 files changed, 18 insertions(+), 7 deletions(-) (limited to 'examples') diff --git a/examples/conditional_formatting/example_conditional_formatting.rb b/examples/conditional_formatting/example_conditional_formatting.rb index ab49d238..f5823ab4 100644 --- a/examples/conditional_formatting/example_conditional_formatting.rb +++ b/examples/conditional_formatting/example_conditional_formatting.rb @@ -11,8 +11,8 @@ percent = book.styles.add_style(:format_code => "0.00%", :border => Axlsx::STYLE money = book.styles.add_style(:format_code => '0,000', :border => Axlsx::STYLE_THIN_BORDER) # define the style for conditional formatting -profitable = book.styles.add_style( :fg_color=>"FF428751", - :type => :dxf) +profitable = book.styles.add_style( :fg_color => "FF428751", :type => :dxf ) +unprofitable = wb.styles.add_style( :fg_color => "FF0000", :type => :dxf ) book.add_worksheet(:name => "Cell Is") do |ws| @@ -27,6 +27,8 @@ book.add_worksheet(:name => "Cell Is") do |ws| # Apply conditional formatting to range B3:B100 in the worksheet ws.add_conditional_formatting("B3:B100", { :type => :cellIs, :operator => :greaterThan, :formula => "100000", :dxfId => profitable, :priority => 1 }) +# Apply conditional using the between operator; NOTE: supply an array to :formula for between/notBetween + sheet.add_conditional_formatting("C3:C100", { :type => :cellIs, :operator => :between, :formula => ["0.00%","100.00%"], :dxfId => unprofitable, :priority => 1 }) end book.add_worksheet(:name => "Color Scale") do |ws| diff --git a/examples/example.rb b/examples/example.rb index 87814b97..31706c3d 100755 --- a/examples/example.rb +++ b/examples/example.rb @@ -647,8 +647,8 @@ if examples.include? :conditional_formatting money = wb.styles.add_style(:format_code => '0,000', :border => Axlsx::STYLE_THIN_BORDER) # define the style for conditional formatting - profitable = wb.styles.add_style( :fg_color=>"FF428751", - :type => :dxf) + profitable = wb.styles.add_style( :fg_color => "FF428751", :type => :dxf ) + unprofitable = wb.styles.add_style( :fg_color => "FF0000", :type => :dxf ) wb.add_worksheet(:name => "Conditional Cell Is") do |sheet| @@ -663,6 +663,8 @@ if examples.include? :conditional_formatting # Apply conditional formatting to range B3:B100 in the worksheet sheet.add_conditional_formatting("B3:B100", { :type => :cellIs, :operator => :greaterThan, :formula => "100000", :dxfId => profitable, :priority => 1 }) + # Apply conditional using the between operator; NOTE: supply an array to :formula for between/notBetween + sheet.add_conditional_formatting("C3:C100", { :type => :cellIs, :operator => :between, :formula => ["0.00%","100.00%"], :dxfId => unprofitable, :priority => 1 }) end wb.add_worksheet(:name => "Conditional Color Scale") do |sheet| diff --git a/lib/axlsx/workbook/worksheet/conditional_formatting_rule.rb b/lib/axlsx/workbook/worksheet/conditional_formatting_rule.rb index a0ce6a41..39b9612b 100644 --- a/lib/axlsx/workbook/worksheet/conditional_formatting_rule.rb +++ b/lib/axlsx/workbook/worksheet/conditional_formatting_rule.rb @@ -25,7 +25,7 @@ module Axlsx # @option options [Integer] stdDev The number of standard deviations above or below the average to match # @option options [Boolean] stopIfTrue Stop evaluating rules after this rule matches # @option options [Symbol] timePeriod The time period in a date occuring... rule - # @option options [String] formula The formula to match against in i.e. an equal rule + # @option options [String] formula The formula to match against in i.e. an equal rule. Use a [minimum, maximum] array for cellIs between/notBetween conditionals. def initialize(options={}) @color_scale = @data_bar = @icon_set = @formula = nil parse_options options @@ -180,7 +180,7 @@ module Axlsx # @see timePeriod def timePeriod=(v); Axlsx::validate_time_period_type(v); @timePeriod = v end # @see formula - def formula=(v); Axlsx::validate_string(v); @formula = v end + def formula=(v); [*v].each {|x| Axlsx::validate_string(x) }; @formula = v end # @see color_scale def color_scale=(v) @@ -208,7 +208,7 @@ module Axlsx str << '' - str << '' << self.formula << '' if @formula + str << '' << [*self.formula].join('') << '' if @formula @color_scale.to_xml_string(str) if @color_scale && @type == :colorScale @data_bar.to_xml_string(str) if @data_bar && @type == :dataBar @icon_set.to_xml_string(str) if @icon_set && @type == :iconSet diff --git a/test/workbook/worksheet/tc_conditional_formatting.rb b/test/workbook/worksheet/tc_conditional_formatting.rb index 087fd40e..42e29fa6 100644 --- a/test/workbook/worksheet/tc_conditional_formatting.rb +++ b/test/workbook/worksheet/tc_conditional_formatting.rb @@ -130,6 +130,13 @@ class TestConditionalFormatting < Test::Unit::TestCase assert doc.xpath("//xmlns:worksheet/xmlns:conditionalFormatting//xmlns:cfRule[@type='cellIs'][@dxfId=0][@priority=1][@operator='greaterThan']//xmlns:formula='0.5'") end + def test_multiple_formulas + @ws.add_conditional_formatting "B3:B3", { :type => :cellIs, :dxfId => 0, :priority => 1, :operator => :between, :formula => ["1","5"] } + doc = Nokogiri::XML.parse(@ws.to_xml_string) + assert doc.xpath("//xmlns:worksheet/xmlns:conditionalFormatting//xmlns:cfRule[@type='cellIs'][@dxfId=0][@priority=1][@operator='between']//xmlns:formula='1'") + assert doc.xpath("//xmlns:worksheet/xmlns:conditionalFormatting//xmlns:cfRule[@type='cellIs'][@dxfId=0][@priority=1][@operator='between']//xmlns:formula='5'") + end + def test_sqref assert_raise(ArgumentError) { @cf.sqref = 10 } assert_nothing_raised { @cf.sqref = "A1:A1" } -- cgit v1.2.3 From 4a062ffd6aa5855c76a6f1537f1902f53262eaf0 Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Thu, 18 Apr 2013 08:28:50 +0900 Subject: Added colors to examples for charts Colors are required by OpenOffice, and LibreOffice, and syntactically correct regardless so we should be putting them in. --- examples/example.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'examples') diff --git a/examples/example.rb b/examples/example.rb index 31706c3d..2b17b5fa 100755 --- a/examples/example.rb +++ b/examples/example.rb @@ -408,7 +408,7 @@ if examples.include? :bar_chart sheet.add_row ["A Simple Bar Chart"] %w(first second third).each { |label| sheet.add_row [label, rand(24)+1] } sheet.add_chart(Axlsx::Bar3DChart, :start_at => "A6", :end_at => "F20") do |chart| - chart.add_series :data => sheet["B2:B4"], :labels => sheet["A2:A4"], :title => sheet["A1"] + chart.add_series :data => sheet["B2:B4"], :labels => sheet["A2:A4"], :title => sheet["A1"], :colors => ["00FF00", "0000FF"] end end end @@ -423,7 +423,7 @@ if examples.include? :chart_gridlines sheet.add_row ["Bar Chart without gridlines"] %w(first second third).each { |label| sheet.add_row [label, rand(24)+1] } sheet.add_chart(Axlsx::Bar3DChart, :start_at => "A6", :end_at => "F20") do |chart| - chart.add_series :data => sheet["B2:B4"], :labels => sheet["A2:A4"] + chart.add_series :data => sheet["B2:B4"], :labels => sheet["A2:A4"], :colors => ["00FF00", "FF0000"] chart.valAxis.gridlines = false chart.catAxis.gridlines = false end @@ -458,16 +458,16 @@ if examples.include? :line_chart sheet.add_chart(Axlsx::Line3DChart, :title => "Simple 3D Line Chart", :rotX => 30, :rotY => 20) do |chart| chart.start_at 0, 5 chart.end_at 10, 20 - chart.add_series :data => sheet["A3:A6"], :title => sheet["A2"] - chart.add_series :data => sheet["B3:B6"], :title => sheet["B2"] + chart.add_series :data => sheet["A3:A6"], :title => sheet["A2"], :color => "0000FF" + chart.add_series :data => sheet["B3:B6"], :title => sheet["B2"], :color => "FF0000" chart.catAxis.title = 'X Axis' chart.valAxis.title = 'Y Axis' end sheet.add_chart(Axlsx::LineChart, :title => "Simple Line Chart", :rotX => 30, :rotY => 20) do |chart| chart.start_at 0, 21 chart.end_at 10, 41 - chart.add_series :data => sheet["A3:A6"], :title => sheet["A2"] - chart.add_series :data => sheet["B3:B6"], :title => sheet["B2"] + chart.add_series :data => sheet["A3:A6"], :title => sheet["A2"], :color => "FF0000" + chart.add_series :data => sheet["B3:B6"], :title => sheet["B2"], :color => "00FF00" chart.catAxis.title = 'X Axis' chart.valAxis.title = 'Y Axis' end @@ -488,8 +488,8 @@ if examples.include? :scatter_chart sheet.add_chart(Axlsx::ScatterChart, :title => "example 7: Scatter Chart") do |chart| chart.start_at 0, 4 chart.end_at 10, 19 - chart.add_series :xData => sheet["B1:E1"], :yData => sheet["B2:E2"], :title => sheet["A1"] - chart.add_series :xData => sheet["B3:E3"], :yData => sheet["B4:E4"], :title => sheet["A3"] + chart.add_series :xData => sheet["B1:E1"], :yData => sheet["B2:E2"], :title => sheet["A1"], :color => "FF0000" + chart.add_series :xData => sheet["B3:E3"], :yData => sheet["B4:E4"], :title => sheet["A3"], :color => "00FF00" end end end -- cgit v1.2.3 From e49aee3e2f700569a519c5346746d239a05383cf Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Sun, 28 Apr 2013 12:55:46 +0900 Subject: Refactored and renamed space preservation preserve_spaces has been moved to the workbook and renamed xml_space as that provides a good reference for people trying to figure out what it does, and let's the author specify space preservation for serializations using the shared strings table as well as the default inline serialization in cells. --- README.md | 8 +++++++- examples/2010_comments.rb | 17 +++++++++++++++++ lib/axlsx/version.rb | 2 +- lib/axlsx/workbook/shared_strings_table.rb | 14 +++++++++++--- lib/axlsx/workbook/workbook.rb | 20 +++++++++++++++++++- lib/axlsx/workbook/worksheet/worksheet.rb | 11 +++++++---- lib/schema/sml.xsd | 3 +++ test/workbook/tc_shared_strings_table.rb | 6 ++++++ test/workbook/tc_workbook.rb | 17 +++++++++++++++++ test/workbook/worksheet/tc_worksheet.rb | 9 --------- 10 files changed, 88 insertions(+), 19 deletions(-) create mode 100644 examples/2010_comments.rb (limited to 'examples') diff --git a/README.md b/README.md index cb69e4fb..20f67088 100644 --- a/README.md +++ b/README.md @@ -160,6 +160,10 @@ This gem has 100% test coverage using test/unit. To execute tests for this gem, #Change log --------- +- **April.??.13**:1.37 + - Added space preservation for cell text. This will allow whitespace + in cell text both when using shared strings and when serializing + directly to the cell. - **April.24.13**:1.3.6 - Fixed LibreOffice/OpenOffice issue to properly apply colors to lines in charts. @@ -271,7 +275,9 @@ air and our feet on the ground. [nibus](https://github.com/nibus) - For patching sheet name uniqueness. -[scambra](https://github.com/scambra) - for keeping our lines in line! +[scambra](https://github.com/scambra) - For keeping our lines in line! + +[agardiner](https://github.com/agardiner) - For the preservation of space. #Copyright and License ---------- diff --git a/examples/2010_comments.rb b/examples/2010_comments.rb new file mode 100644 index 00000000..6a7bedf8 --- /dev/null +++ b/examples/2010_comments.rb @@ -0,0 +1,17 @@ +#!/usr/bin/env ruby -w -s +# -*- coding: utf-8 -*- +$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib" + +#```ruby +require 'axlsx' +p = Axlsx::Package.new +p.workbook.add_worksheet(:name => 'Excel 2010 comments') do |sheet| + sheet.add_row ['Cell with visible comment'] + sheet.add_row + sheet.add_row + sheet.add_row ['Cell with hidden comment'] + + sheet.add_comment :ref => 'A1', :author => 'XXX', :text => 'Visibile' + sheet.add_comment :ref => 'A4', :author => 'XXX', :text => 'Hidden', :visible => false +end +p.serialize('excel_2010_comment_test.xlsx') diff --git a/lib/axlsx/version.rb b/lib/axlsx/version.rb index 546f601d..6c9907ab 100644 --- a/lib/axlsx/version.rb +++ b/lib/axlsx/version.rb @@ -1,5 +1,5 @@ module Axlsx # The current version - VERSION = "1.3.6" + VERSION = "1.3.7" end diff --git a/lib/axlsx/workbook/shared_strings_table.rb b/lib/axlsx/workbook/shared_strings_table.rb index 43e8a1a8..f1bee3ae 100644 --- a/lib/axlsx/workbook/shared_strings_table.rb +++ b/lib/axlsx/workbook/shared_strings_table.rb @@ -26,10 +26,16 @@ module Axlsx # @see Cell#sharable attr_reader :unique_cells + # The xml:space attribute + # @see Workbook#xml_space + attr_reader :xml_space + # 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) + # @param [Symbol] xml_space The xml:space behavior for the shared string table. + def initialize(cells, xml_space=:preserve) @index = 0 + @xml_space = xml_space @unique_cells = {} @shared_xml_string = "" shareable_cells = cells.flatten.select{ |cell| cell.plain_string? } @@ -40,8 +46,10 @@ module Axlsx # Serializes the object # @param [String] str # @return [String] - def to_xml_string - '' << @shared_xml_string << '' + def to_xml_string(str='') + str << '" : - "") % [XML_NS, XML_NS_R] + "" % [XML_NS, XML_NS_R] end def sheet_data diff --git a/lib/schema/sml.xsd b/lib/schema/sml.xsd index fa396e80..f3994e0a 100644 --- a/lib/schema/sml.xsd +++ b/lib/schema/sml.xsd @@ -13,6 +13,8 @@ + + @@ -2213,6 +2215,7 @@ + diff --git a/test/workbook/tc_shared_strings_table.rb b/test/workbook/tc_shared_strings_table.rb index e3c9bf7b..7a333f4f 100644 --- a/test/workbook/tc_shared_strings_table.rb +++ b/test/workbook/tc_shared_strings_table.rb @@ -24,6 +24,12 @@ class TestSharedStringsTable < Test::Unit::TestCase assert_equal(sst.unique_count, 4) end + def test_uses_workbook_xml_space + assert_equal(@p.workbook.xml_space, @p.workbook.shared_strings.xml_space) + @p.workbook.xml_space = :default + assert_equal(:default, @p.workbook.shared_strings.xml_space) + end + def test_valid_document schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD)) doc = Nokogiri::XML(@p.workbook.shared_strings.to_xml_string) diff --git a/test/workbook/tc_workbook.rb b/test/workbook/tc_workbook.rb index 1a9b9669..591160f4 100644 --- a/test/workbook/tc_workbook.rb +++ b/test/workbook/tc_workbook.rb @@ -9,6 +9,23 @@ class TestWorkbook < Test::Unit::TestCase def teardown end + def test_worksheet_users_xml_space + sheet = @wb.add_worksheet(:name => 'foo') + ws_xml = Nokogiri::XML(sheet.to_xml_string) + assert(ws_xml.xpath("//xmlns:worksheet/@xml:space='preserve'")) + + @wb.xml_space = :default + ws_xml = Nokogiri::XML(sheet.to_xml_string) + assert(ws_xml.xpath("//xmlns:worksheet/@xml:space='default'")) + end + + def test_xml_space + assert_equal(:preserve, @wb.xml_space) + @wb.xml_space = :default + assert_equal(:default, @wb.xml_space) + assert_raise(ArgumentError) { @wb.xml_space = :none } + end + def test_no_autowidth assert_equal(@wb.use_autowidth, true) assert_raise(ArgumentError) {@wb.use_autowidth = 0.1} diff --git a/test/workbook/worksheet/tc_worksheet.rb b/test/workbook/worksheet/tc_worksheet.rb index c2bb0faa..ed8c7cab 100644 --- a/test/workbook/worksheet/tc_worksheet.rb +++ b/test/workbook/worksheet/tc_worksheet.rb @@ -352,15 +352,6 @@ class TestWorksheet < Test::Unit::TestCase assert(schema.validate(doc).map{ |e| puts e.message; e }.empty?, "error free validation") end - def test_to_xml_string_with_preserve_spaces - # Check that xml:space="preserve" has been added when preserve_spaces is set - ws_xml = Nokogiri::XML(@ws.to_xml_string) - assert(ws_xml.xpath("//xmlns:worksheet/@xml:space='preserve'")) - @ws.preserve_spaces = false - ws_xml = Nokogiri::XML(@ws.to_xml_string) - assert(!ws_xml.xpath("//xmlns:worksheet/@xml:space='preserve'")) - end - def test_styles assert(@ws.styles.is_a?(Axlsx::Styles), 'worksheet provides access to styles') end -- cgit v1.2.3 From eb660629e9dd2f4dbd5310dc885203f91056f956 Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Sun, 28 Apr 2013 13:11:22 +0900 Subject: Updated mbcs example to use Arial Unicode MS This font may not be avilalbe in all renderes. --- examples/example.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'examples') diff --git a/examples/example.rb b/examples/example.rb index 2b17b5fa..08488117 100755 --- a/examples/example.rb +++ b/examples/example.rb @@ -309,6 +309,7 @@ end #```ruby if examples.include? :mbcs + wb.styles.fonts.first.name = 'Arial Unicode MS' wb.add_worksheet(:name => "日本語でのシート名") do |sheet| sheet.add_row ["日本語"] sheet.add_row ["华语/華語"] -- cgit v1.2.3 From 0c9a48eef60d4d81a2c2bd5a4b0b87c981e1e062 Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Sun, 28 Apr 2013 13:18:39 +0900 Subject: Show whitespace preservation in basic worksheet example. --- examples/example.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'examples') diff --git a/examples/example.rb b/examples/example.rb index 08488117..04e49d8f 100755 --- a/examples/example.rb +++ b/examples/example.rb @@ -57,6 +57,7 @@ if examples.include? :basic wb.add_worksheet(:name => "Basic Worksheet") do |sheet| sheet.add_row ["First Column", "Second", "Third"] sheet.add_row [1, 2, 3] + sheet.add_row [' preserving whitespace'] end end #``` -- cgit v1.2.3 From 86180711acbb7f9c6a04688fc9e6ffbf27bacf19 Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Sun, 23 Jun 2013 10:11:23 +0100 Subject: added sparse array transposition with blocks for rows/cols switching and some docs updates for release prep --- README.md | 8 +++++--- cd | 1 + examples/underline.rb | 13 +++++++++++++ exclusive.rb | 32 +++++++++++++++++++++++++++++++ lib/axlsx/package.rb | 12 ++++++------ lib/axlsx/workbook/worksheet/worksheet.rb | 9 +++++++-- test/workbook/worksheet/tc_worksheet.rb | 7 +++++++ 7 files changed, 71 insertions(+), 11 deletions(-) create mode 100644 cd create mode 100644 examples/underline.rb create mode 100644 exclusive.rb (limited to 'examples') diff --git a/README.md b/README.md index 20f67088..ff3fe557 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ appreciation for the gem, please don't hesitate to make a donation. **License**: MIT License -**Latest Version**: 1.3.6 +**Latest Version**: 1.3.7 **Ruby Version**: 1.8.7 (soon to be depreciated!!!), 1.9.2, 1.9.3, 2.0.0 @@ -29,7 +29,7 @@ appreciation for the gem, please don't hesitate to make a donation. **Rubinius Version**: rubinius 2.0.0dev * lower versions may run, this gem always tests against head. -**Release Date**: April 24th 2013 +**Release Date**: June ? 2013 If you are working in rails, or with active record see: [acts_as_xlsx](http://github.com/randym/acts_as_xlsx) @@ -160,7 +160,9 @@ This gem has 100% test coverage using test/unit. To execute tests for this gem, #Change log --------- -- **April.??.13**:1.37 +- **June.?.13**:1.3.7 + - Bugfix: transposition of cells for Worksheet#cols now supports + incongruent column counts.counts - Added space preservation for cell text. This will allow whitespace in cell text both when using shared strings and when serializing directly to the cell. diff --git a/cd b/cd new file mode 100644 index 00000000..8588caed --- /dev/null +++ b/cd @@ -0,0 +1 @@ +/opt/boxen/rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/axlsx-1.3.6/lib/axlsx.rb diff --git a/examples/underline.rb b/examples/underline.rb new file mode 100644 index 00000000..ccd1b394 --- /dev/null +++ b/examples/underline.rb @@ -0,0 +1,13 @@ +$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib" +require 'axlsx' +p = Axlsx::Package.new +p.workbook do |wb| + wb.styles do |s| + no_underline = s.add_style :sz => 10, :b => true, :u => false, :alignment => { :horizontal=> :right } + wb.add_worksheet(:name => 'wunderlinen') do |sheet| + sheet.add_row %w{a b c really?}, :style => no_underline + end + end +end +p.serialize 'no_underline.xlsx' + diff --git a/exclusive.rb b/exclusive.rb new file mode 100644 index 00000000..71339146 --- /dev/null +++ b/exclusive.rb @@ -0,0 +1,32 @@ + +def exclusively_true(hash, key) + #clone = hash.clone + return false unless hash.delete(key) == true + !hash.has_value? true +end + +require 'test/unit' +class TestExclusive < Test::Unit::TestCase + def setup + @test_hash = {foo: true, bar: false, hoge: false} + end + def test_exclusive + assert_equal(true, exclusively_true(@test_hash, :foo)) + end + def test_inexclusive + @test_hash[:bar] = true + assert_equal(false, exclusively_true(@test_hash, :foo)) + end +end + +require 'benchmark' + +h = {foo: true} +999.times {|i| h["a#{i}"] = false} +Benchmark.bmbm(30) do |x| + x.report('exclusively_true') do + 1000.times do + exclusively_true(h, :foo) + end + end +end diff --git a/lib/axlsx/package.rb b/lib/axlsx/package.rb index df87ed12..13a3bd13 100644 --- a/lib/axlsx/package.rb +++ b/lib/axlsx/package.rb @@ -35,12 +35,6 @@ module Axlsx end - # Shortcut to specify that the workbook should use shared strings - # @see Workbook#use_shared_strings - def use_shared_strings=(v) - Axlsx::validate_boolean(v); - workbook.use_shared_strings = v - end # Shortcut to determine if the workbook is configured to use shared strings # @see Workbook#use_shared_strings @@ -48,6 +42,12 @@ module Axlsx workbook.use_shared_strings end + # Shortcut to specify that the workbook should use shared strings + # @see Workbook#use_shared_strings + def use_shared_strings=(v) + Axlsx::validate_boolean(v); + workbook.use_shared_strings = v + end # The workbook this package will serialize or validate. # @return [Workbook] If no workbook instance has been assigned with this package a new Workbook instance is returned. # @raise ArgumentError if workbook parameter is not a Workbook instance. diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb index 1187bf83..7324181a 100644 --- a/lib/axlsx/workbook/worksheet/worksheet.rb +++ b/lib/axlsx/workbook/worksheet/worksheet.rb @@ -115,8 +115,13 @@ module Axlsx end # returns the sheet data as columns - def cols - @rows.transpose + # If you pass a block, it will be evaluated whenever a row does not have a + # cell at a specific index. The block will be called with the row and column + # index in the missing cell was found. + # @example + # cols { |row_index, column_index| p "warn - row #{row_index} is does not have a cell at #{column_index} + def cols(&block) + @rows.transpose(&block) end # An range that excel will apply an auto-filter to "A1:B3" diff --git a/test/workbook/worksheet/tc_worksheet.rb b/test/workbook/worksheet/tc_worksheet.rb index b05a1c56..20bd0771 100644 --- a/test/workbook/worksheet/tc_worksheet.rb +++ b/test/workbook/worksheet/tc_worksheet.rb @@ -212,6 +212,13 @@ class TestWorksheet < Test::Unit::TestCase assert_equal(c[0].value, 2) end + def test_cols_with_block + @ws.add_row [1,2,3] + @ws.add_row [1] + cols = @ws.cols {|row, column| :foo } + asser_equal(:foo, cols[1][1]) + end + def test_row_style @ws.add_row [1,2,3,4] @ws.add_row [1,2,3,4] -- cgit v1.2.3 From 058aa68249878a878c30f9c7fa88c7b39ec07901 Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Fri, 12 Jul 2013 22:29:56 +0100 Subject: WIP single/dual anchors for images --- examples/example.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/example.rb b/examples/example.rb index 04e49d8f..218237a6 100755 --- a/examples/example.rb +++ b/examples/example.rb @@ -278,11 +278,12 @@ if examples.include? :images img = File.expand_path('../image1.jpeg', __FILE__) # specifying the :hyperlink option will add a hyper link to your image. # @note - Numbers does not support this part of the specification. - sheet.add_image(:image_src => img, :noSelect => true, :noMove => true, :hyperlink=>"http://axlsx.blogspot.com") do |image| + sheet.add_image(:image_src => img, :noSelect => true, end_at: true, :noMove => true, :hyperlink=>"http://axlsx.blogspot.com") do |image| image.width=720 image.height=666 image.hyperlink.tooltip = "Labeled Link" image.start_at 2, 2 + image.end_at 200, 200 end end end -- cgit v1.2.3